Skip to content

Commit

Permalink
Replace custom geojson props with single title
Browse files Browse the repository at this point in the history
  • Loading branch information
bartromgens committed May 3, 2016
1 parent 790c952 commit 5727564
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
)
Expand Down
10 changes: 5 additions & 5 deletions geojsoncontour/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion geojsoncontour/examples/example1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion geojsoncontour/tests/benchmark_test1.geojson
Original file line number Diff line number Diff line change
@@ -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"}
{"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"}
4 changes: 2 additions & 2 deletions geojsoncontour/tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 5727564

Please sign in to comment.