From 52167b0c0d5dd482bb1b0d1037845fa8e72067f4 Mon Sep 17 00:00:00 2001 From: dnomadb Date: Tue, 16 Feb 2021 11:12:13 -0800 Subject: [PATCH 1/2] test to show property preservation --- tests/test_mod.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/test_mod.py b/tests/test_mod.py index 0b38284..13d38e5 100644 --- a/tests/test_mod.py +++ b/tests/test_mod.py @@ -137,6 +137,42 @@ def test_filter_features_multi_polygon(): assert list(sutils.filter_features(features)) == expected +def test_filter_features_multi_polygon_preserve_properties(): + """MultiPolygons should be turned into multiple Polygons""" + features = [ + { + "type": "Feature", + "geometry": { + "type": "MultiPolygon", + "coordinates": [ + [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]]], + [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]]], + ], + }, + "properties": {"a": "property"}, + } + ] + expected = [ + { + "type": "Feature", + "geometry": { + "type": "Polygon", + "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]]], + }, + "properties": {"a": "property"}, + }, + { + "type": "Feature", + "geometry": { + "type": "Polygon", + "coordinates": [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]]], + }, + "properties": {"a": "property"}, + }, + ] + assert list(sutils.filter_features(features)) == expected + + def test_filter_features_multi_point(): """MultiPoints should be turned into multiple Points""" features = [ From 6958ab89288a49d5fb653f99462997a25a16e8d6 Mon Sep 17 00:00:00 2001 From: dnomadb Date: Tue, 16 Feb 2021 12:43:03 -0800 Subject: [PATCH 2/2] handling for properties --- supermercado/super_utils.py | 30 +++++++++++++++---- tests/test_mod.py | 60 +++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 6 deletions(-) diff --git a/supermercado/super_utils.py b/supermercado/super_utils.py index c4e1df4..6862c62 100644 --- a/supermercado/super_utils.py +++ b/supermercado/super_utils.py @@ -64,22 +64,40 @@ def filter_features(features): elif f["geometry"]["type"] == "LineString": yield f elif f["geometry"]["type"] == "MultiPolygon": + base_feature = {} + if "properties" in f: + base_feature.update(properties=f["properties"]) for part in f["geometry"]["coordinates"]: yield { - "type": "Feature", - "geometry": {"type": "Polygon", "coordinates": part}, + **{ + "type": "Feature", + "geometry": {"type": "Polygon", "coordinates": part}, + }, + **base_feature, } elif f["geometry"]["type"] == "MultiPoint": + base_feature = {} + if "properties" in f: + base_feature.update(properties=f["properties"]) for part in f["geometry"]["coordinates"]: yield { - "type": "Feature", - "geometry": {"type": "Point", "coordinates": part}, + **{ + "type": "Feature", + "geometry": {"type": "Point", "coordinates": part}, + }, + **base_feature, } elif f["geometry"]["type"] == "MultiLineString": + base_feature = {} + if "properties" in f: + base_feature.update(properties=f["properties"]) for part in f["geometry"]["coordinates"]: yield { - "type": "Feature", - "geometry": {"type": "LineString", "coordinates": part}, + **{ + "type": "Feature", + "geometry": {"type": "LineString", "coordinates": part}, + }, + **base_feature, } diff --git a/tests/test_mod.py b/tests/test_mod.py index 13d38e5..e0ae990 100644 --- a/tests/test_mod.py +++ b/tests/test_mod.py @@ -188,6 +188,30 @@ def test_filter_features_multi_point(): assert list(sutils.filter_features(features)) == expected +def test_filter_features_multi_point_properties(): + """MultiPoints should be turned into multiple Points""" + features = [ + { + "type": "Feature", + "geometry": {"type": "MultiPoint", "coordinates": [[0, 0], [1, 0]]}, + "properties": {"a": "property"}, + } + ] + expected = [ + { + "type": "Feature", + "geometry": {"type": "Point", "coordinates": [0, 0]}, + "properties": {"a": "property"}, + }, + { + "type": "Feature", + "geometry": {"type": "Point", "coordinates": [1, 0]}, + "properties": {"a": "property"}, + }, + ] + assert list(sutils.filter_features(features)) == expected + + def test_filter_features_multi_linstrings(): """MultiLineStrings should be turned into multiple LineStrings""" features = [ @@ -221,6 +245,42 @@ def test_filter_features_multi_linstrings(): assert list(sutils.filter_features(features)) == expected +def test_filter_features_multi_linstrings_properties(): + """MultiLineStrings should be turned into multiple LineStrings""" + features = [ + { + "type": "Feature", + "geometry": { + "type": "MultiLineString", + "coordinates": [ + [[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]], + [[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]], + ], + }, + "properties": {"a": "property"}, + } + ] + expected = [ + { + "type": "Feature", + "geometry": { + "type": "LineString", + "coordinates": [[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]], + }, + "properties": {"a": "property"}, + }, + { + "type": "Feature", + "geometry": { + "type": "LineString", + "coordinates": [[0, 0], [1, 0], [1, 1], [0, 1], [0, 1]], + }, + "properties": {"a": "property"}, + }, + ] + assert list(sutils.filter_features(features)) == expected + + def test_find_extrema(): """Extrema should be calculated correctly""" features = [