Skip to content

Commit

Permalink
Create unit test for contour to geojson
Browse files Browse the repository at this point in the history
  • Loading branch information
bartromgens committed May 2, 2016
1 parent c9ece14 commit 05297ca
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions geojsoncontour/tests/benchmark_test1.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"features": [{"geometry": {"coordinates": [[-4.0, -22.085], [-10.18, -20.0], [-15.0, -16.693], [-19.0, -11.946], [-21.627, -6.0], [-22.444, 0.0], [-21.627, 6.0], [-19.563, 11.0], [-16.0, 15.737], [-11.946, 19.0], [-6.0, 21.627], [0.0, 22.444], [6.0, 21.627], [11.0, 19.563], [15.737, 16.0], [19.0, 11.946], [21.627, 6.0], [22.444, 0.0], [21.627, -6.0], [19.563, -11.0], [16.0, -15.737], [11.946, -19.0], [6.0, -21.627], [0.0, -22.444], [-4.0, -22.085]], "type": "LineString"}, "properties": {"label": 22.444444444444443, "stroke": "#0000ff", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[-8.0, -44.17], [-20.0, -40.187], [-29.307, -34.0], [-37.282, -25.0], [-42.65, -14.0], [-44.789, -3.0], [-44.0, 8.883], [-40.187, 20.0], [-34.0, 29.307], [-25.0, 37.282], [-14.0, 42.65], [-3.0, 44.789], [8.883, 44.0], [20.0, 40.187], [29.307, 34.0], [37.282, 25.0], [42.65, 14.0], [44.789, 3.0], [44.0, -8.883], [40.187, -20.0], [34.0, -29.307], [25.0, -37.282], [14.0, -42.65], [3.0, -44.789], [-8.0, -44.17]], "type": "LineString"}, "properties": {"label": 44.888888888888886, "stroke": "#0060ff", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[-6.0, -67.065], [-24.0, -62.911], [-39.0, -54.888], [-52.0, -42.775], [-61.235, -28.0], [-66.429, -11.0], [-67.0, 6.675], [-62.911, 24.0], [-54.888, 39.0], [-42.775, 52.0], [-28.0, 61.235], [-11.0, 66.429], [6.675, 67.0], [24.0, 62.911], [39.0, 54.888], [52.0, 42.775], [61.235, 28.0], [66.429, 11.0], [67.0, -6.675], [62.911, -24.0], [54.888, -39.0], [42.775, -52.0], [28.0, -61.235], [11.0, -66.429], [-6.0, -67.065]], "type": "LineString"}, "properties": {"label": 67.33333333333333, "stroke": "#00d4ff", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[11.785, 89.0], [34.218, 83.0], [55.0, 70.958], [71.722, 54.0], [83.091, 34.0], [89.0, 11.785], [88.972, -12.0], [83.0, -34.218], [70.958, -55.0], [54.0, -71.722], [34.0, -83.091], [11.785, -89.0], [-12.0, -88.972], [-34.218, -83.0], [-55.0, -70.958], [-71.722, -54.0], [-83.091, -34.0], [-89.0, -11.785], [-88.972, 12.0], [-83.0, 34.218], [-70.958, 55.0], [-54.0, 71.722], [-34.0, 83.091], [-11.785, 89.0]], "type": "LineString"}, "properties": {"label": 89.77777777777777, "stroke": "#4dffaa", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[-67.036, -90.0], [-88.503, -69.0], [-103.237, -44.0], [-111.076, -16.0], [-111.467, 13.0], [-104.464, 41.0], [-90.762, 66.0], [-70.886, 87.0], [-68.357, 89.0]], "type": "LineString"}, "properties": {"label": 112.22222222222221, "stroke": "#aaff4d", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[68.357, 89.0], [89.273, 68.0], [104.0, 42.163], [111.346, 14.0], [111.215, -15.0], [103.657, -43.0], [89.0, -68.357], [67.036, -90.0]], "type": "LineString"}, "properties": {"label": 112.22222222222221, "stroke": "#aaff4d", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[-100.175, -90.0], [-120.561, -60.0], [-131.932, -27.0], [-134.429, 8.0], [-127.95, 42.0], [-113.0, 73.253], [-101.065, 89.0]], "type": "LineString"}, "properties": {"label": 134.66666666666666, "stroke": "#ffe600", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[101.065, 89.0], [121.054, 59.0], [132.133, 26.0], [134.366, -9.0], [127.617, -43.0], [112.0, -74.773], [100.175, -90.0]], "type": "LineString"}, "properties": {"label": 134.66666666666666, "stroke": "#ffe600", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[-128.778, -90.0], [-147.902, -53.0], [-156.572, -13.0], [-154.596, 28.0], [-142.109, 67.0], [-129.471, 89.0]], "type": "LineString"}, "properties": {"label": 157.1111111111111, "stroke": "#ff7a00", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[129.471, 89.0], [148.256, 52.0], [156.652, 12.0], [154.411, -29.0], [141.633, -68.0], [128.778, -90.0]], "type": "LineString"}, "properties": {"label": 157.1111111111111, "stroke": "#ff7a00", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[-155.371, -90.0], [-173.563, -46.0], [-179.556, 0.0], [-173.563, 46.0], [-155.946, 89.0]], "type": "LineString"}, "properties": {"label": 179.55555555555554, "stroke": "#ff1300", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[179.0, -14.11], [169.0, -60.655], [155.371, -90.0]], "type": "LineString"}, "properties": {"label": 179.55555555555554, "stroke": "#ff1300", "stroke-width": 10, "unit": ""}, "type": "Feature"}, {"geometry": {"coordinates": [[155.946, 89.0], [173.825, 45.0], [179.0, 14.11]], "type": "LineString"}, "properties": {"label": 179.55555555555554, "stroke": "#ff1300", "stroke-width": 10, "unit": ""}, "type": "Feature"}], "type": "FeatureCollection"}
72 changes: 72 additions & 0 deletions geojsoncontour/tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import os
import unittest
import filecmp

import numpy
import matplotlib.pyplot as plt

import geojsoncontour


class TestContourToGeoJson(unittest.TestCase):
dirname = os.path.dirname(__file__)
geojson_file = os.path.join(dirname, 'test1.geojson')
benchmark_geojson_file = os.path.join(dirname, 'benchmark_test1.geojson')

def setUp(self):
if os.path.exists(self.geojson_file):
os.remove(self.geojson_file)

def test_matplotlib_contour_to_geojson(self):
latrange, lonrange, Z = self.create_grid_data()
config = ContourPlotConfig(level_lower=0, level_upper=202)

figure = plt.figure()
ax = figure.add_subplot(111)
contours = ax.contour(
lonrange, latrange, Z,
levels=config.levels,
cmap=config.colormap,
norm=config.norm
)

ndigits = 3
geojsoncontour.contour_to_geojson(
contours,
self.geojson_file,
config.levels,
config.min_angle_between_segments,
ndigits,
config.unit
)
self.assertTrue(os.path.exists(self.geojson_file))
self.assertTrue(filecmp.cmp(self.benchmark_geojson_file, self.geojson_file))
os.remove(self.geojson_file)

def create_grid_data(self):
grid_size = 1.0
lat_min = -90.0
lat_max = 90.0
lon_min = -180.0
lon_max = 180.0
latrange = numpy.arange(lat_min, lat_max, grid_size)
lonrange = numpy.arange(lon_min, lon_max, grid_size)
X, Y = numpy.meshgrid(lonrange, latrange)
Z = numpy.sqrt(X*X + Y*Y)
return latrange, lonrange, Z


class ContourPlotConfig(object):
def __init__(self, level_lower=0, level_upper=100, colormap=plt.cm.jet, unit=''): # jet, jet_r, YlOrRd, gist_rainbow
self.n_contours = 10
self.min_angle_between_segments = 15
self.level_lower = level_lower
self.level_upper = level_upper
self.colormap = colormap
self.unit = unit
self.norm = None
self.levels = numpy.linspace(
start=self.level_lower,
stop=self.level_upper,
num=self.n_contours
)

0 comments on commit 05297ca

Please sign in to comment.