From 57275640dfd95d566581fa0ac94f0f671a439090 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=20R=C3=B6mgens?= Date: Tue, 3 May 2016 12:57:59 +0200 Subject: [PATCH] Replace custom geojson props with single title --- README.md | 2 +- geojsoncontour/contour.py | 10 +++++----- geojsoncontour/examples/example1.py | 2 +- geojsoncontour/tests/benchmark_test1.geojson | 2 +- geojsoncontour/tests/test.py | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 825ab76..ecd59ca 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ contour = ax.contour(lon_range, lat_range, Z, levels=levels, cmap=plt.cm.jet) geojsoncontour.contour_to_geojson( contour=contour, geojson_filepath='out.geojson', - contour_labels=levels, + contour_levels=levels, ndigits=3, unit='m' ) diff --git a/geojsoncontour/contour.py b/geojsoncontour/contour.py index dc2bc41..d611732 100644 --- a/geojsoncontour/contour.py +++ b/geojsoncontour/contour.py @@ -23,12 +23,13 @@ def angle(v1, v2): return math.acos(cos_angle) -def contour_to_geojson(contour, geojson_filepath, contour_labels, min_angle_deg=2, ndigits=5, unit=''): +def contour_to_geojson(contour, geojson_filepath, contour_levels, min_angle_deg=2, + ndigits=5, unit='', stroke_width=5): collections = contour.collections total_points = 0 total_points_original = 0 contour_index = 0 - assert len(contour_labels) == len(collections) + assert len(contour_levels) == len(collections) line_features = [] for collection in collections: paths = collection.get_paths() @@ -57,10 +58,9 @@ def contour_to_geojson(contour, geojson_filepath, contour_labels, min_angle_deg= total_points_original += len(v) line = LineString(coordinates) properties = { - "stroke-width": 10, + "stroke-width": stroke_width, "stroke": rgb2hex(color[0]), - "label": contour_labels[contour_index], - "unit": unit + "title": "%.2f" % contour_levels[contour_index] + ' ' + unit, } line_features.append(Feature(geometry=line, properties=properties)) contour_index += 1 diff --git a/geojsoncontour/examples/example1.py b/geojsoncontour/examples/example1.py index 762680d..0084a54 100644 --- a/geojsoncontour/examples/example1.py +++ b/geojsoncontour/examples/example1.py @@ -21,7 +21,7 @@ geojsoncontour.contour_to_geojson( contour=contour, geojson_filepath='out.geojson', - contour_labels=levels, + contour_levels=levels, min_angle_deg=10.0, ndigits=3, unit='m' diff --git a/geojsoncontour/tests/benchmark_test1.geojson b/geojsoncontour/tests/benchmark_test1.geojson index 0eba6ba..8d32347 100644 --- a/geojsoncontour/tests/benchmark_test1.geojson +++ b/geojsoncontour/tests/benchmark_test1.geojson @@ -1 +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"} \ No newline at end of file +{"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": {"stroke": "#0000ff", "stroke-width": 5, "title": "22.44 [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": {"stroke": "#0060ff", "stroke-width": 5, "title": "44.89 [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": {"stroke": "#00d4ff", "stroke-width": 5, "title": "67.33 [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": {"stroke": "#4dffaa", "stroke-width": 5, "title": "89.78 [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": {"stroke": "#aaff4d", "stroke-width": 5, "title": "112.22 [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": {"stroke": "#aaff4d", "stroke-width": 5, "title": "112.22 [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": {"stroke": "#ffe600", "stroke-width": 5, "title": "134.67 [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": {"stroke": "#ffe600", "stroke-width": 5, "title": "134.67 [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": {"stroke": "#ff7a00", "stroke-width": 5, "title": "157.11 [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": {"stroke": "#ff7a00", "stroke-width": 5, "title": "157.11 [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": {"stroke": "#ff1300", "stroke-width": 5, "title": "179.56 [unit]"}, "type": "Feature"}, {"geometry": {"coordinates": [[179.0, -14.11], [169.0, -60.655], [155.371, -90.0]], "type": "LineString"}, "properties": {"stroke": "#ff1300", "stroke-width": 5, "title": "179.56 [unit]"}, "type": "Feature"}, {"geometry": {"coordinates": [[155.946, 89.0], [173.825, 45.0], [179.0, 14.11]], "type": "LineString"}, "properties": {"stroke": "#ff1300", "stroke-width": 5, "title": "179.56 [unit]"}, "type": "Feature"}], "type": "FeatureCollection"} \ No newline at end of file diff --git a/geojsoncontour/tests/test.py b/geojsoncontour/tests/test.py index e7ded60..a2ef47f 100644 --- a/geojsoncontour/tests/test.py +++ b/geojsoncontour/tests/test.py @@ -21,7 +21,7 @@ def setUp(self): def test_matplotlib_contour_to_geojson(self): latrange, lonrange, Z = self.create_grid_data() - config = ContourPlotConfig(level_lower=0, level_upper=202) + config = ContourPlotConfig(level_lower=0, level_upper=202, unit='[unit]') figure = plt.figure() ax = figure.add_subplot(111) @@ -35,7 +35,7 @@ def test_matplotlib_contour_to_geojson(self): geojsoncontour.contour_to_geojson( contour=contours, geojson_filepath=self.geojson_file, - contour_labels=config.levels, + contour_levels=config.levels, min_angle_deg=config.min_angle_between_segments, ndigits=ndigits, unit=config.unit