From 64719864556f9fba3b837549712cd416baab379a Mon Sep 17 00:00:00 2001 From: James B Date: Fri, 10 Feb 2023 09:16:03 +0000 Subject: [PATCH] geojsontojson: Fix crash https://github.com/Open-Telecoms-Data/lib-cove-ofds/issues/69 --- CHANGELOG.md | 6 ++ libcoveofds/geojson.py | 6 +- make_expected_test_data.sh | 1 + .../geojson_to_json/bad_1.expected.json | 98 +++++++++++++++++++ .../geojson_to_json/bad_1.meta.expected.json | 61 ++++++++++++ .../geojson_to_json/bad_1.nodes.geo.json | 42 ++++++++ .../geojson_to_json/bad_1.spans.geo.json | 86 ++++++++++++++++ tests/test_geojson_to_json.py | 2 + 8 files changed, 300 insertions(+), 2 deletions(-) create mode 100644 tests/fixtures/geojson_to_json/bad_1.expected.json create mode 100644 tests/fixtures/geojson_to_json/bad_1.meta.expected.json create mode 100644 tests/fixtures/geojson_to_json/bad_1.nodes.geo.json create mode 100644 tests/fixtures/geojson_to_json/bad_1.spans.geo.json diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b30762..86f2aee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Fixed + +- GeoJSON to JSON: + - [#69](https://github.com/Open-Telecoms-Data/lib-cove-ofds/issues/69) AttributeError: 'str' object has no attribute 'get' + + ## [0.7.0] - 2023-01-11 ### Added diff --git a/libcoveofds/geojson.py b/libcoveofds/geojson.py index 9f23828..63b990b 100644 --- a/libcoveofds/geojson.py +++ b/libcoveofds/geojson.py @@ -381,8 +381,10 @@ def _process_span(self, geojson_feature_span: dict) -> None: if geojson_feature_span.get("geometry"): span["route"] = geojson_feature_span["geometry"] - span["start"] = span.get("start", {}).get("id") - span["end"] = span.get("end", {}).get("id") + if isinstance(span.get("start"), dict): + span["start"] = span["start"].get("id") + if isinstance(span.get("end"), dict): + span["end"] = span["end"].get("id") self._networks[network_id]["spans"].append(span) diff --git a/make_expected_test_data.sh b/make_expected_test_data.sh index 117471e..790cb97 100755 --- a/make_expected_test_data.sh +++ b/make_expected_test_data.sh @@ -20,6 +20,7 @@ libcoveofds gjtoj --outputmetafilename tests/fixtures/geojson_to_json/phase_fund libcoveofds gjtoj --outputmetafilename tests/fixtures/geojson_to_json/phases_inconsistent_1.meta.expected.json tests/fixtures/geojson_to_json/phases_inconsistent_1.nodes.geo.json tests/fixtures/geojson_to_json/phases_inconsistent_1.spans.geo.json tests/fixtures/geojson_to_json/phases_inconsistent_1.expected.json libcoveofds gjtoj --outputmetafilename tests/fixtures/geojson_to_json/organisations_inconsistent_1.meta.expected.json tests/fixtures/geojson_to_json/organisations_inconsistent_1.nodes.geo.json tests/fixtures/geojson_to_json/organisations_inconsistent_1.spans.geo.json tests/fixtures/geojson_to_json/organisations_inconsistent_1.expected.json libcoveofds gjtoj --outputmetafilename tests/fixtures/geojson_to_json/network_inconsistent_1.meta.expected.json tests/fixtures/geojson_to_json/network_inconsistent_1.nodes.geo.json tests/fixtures/geojson_to_json/network_inconsistent_1.spans.geo.json tests/fixtures/geojson_to_json/network_inconsistent_1.expected.json +libcoveofds gjtoj --outputmetafilename tests/fixtures/geojson_to_json/bad_1.meta.expected.json tests/fixtures/geojson_to_json/bad_1.nodes.geo.json tests/fixtures/geojson_to_json/bad_1.spans.geo.json tests/fixtures/geojson_to_json/bad_1.expected.json # JSON to GeoJSON libcoveofds jtogj --outputmetafilename tests/fixtures/json_to_geojson/basic_1.expected.meta.json tests/fixtures/json_to_geojson/basic_1.json tests/fixtures/json_to_geojson/basic_1.expected.nodes.geo.json tests/fixtures/json_to_geojson/basic_1.expected.spans.geo.json diff --git a/tests/fixtures/geojson_to_json/bad_1.expected.json b/tests/fixtures/geojson_to_json/bad_1.expected.json new file mode 100644 index 0000000..60dc392 --- /dev/null +++ b/tests/fixtures/geojson_to_json/bad_1.expected.json @@ -0,0 +1,98 @@ +{ + "networks": [ + { + "id": "a096d627-72e1-4f9b-b129-951b1737bff4", + "name": "Ghana Fibre Network", + "nodes": [ + { + "id": "1", + "name": "Accra", + "location": { + "type": "Point", + "coordinates": [ + -0.174, + 5.625 + ] + } + }, + { + "id": "2", + "name": "Kumasi", + "status": "operational", + "location": { + "type": "Point", + "coordinates": [ + -1.628, + 6.711 + ] + } + } + ], + "spans": [ + { + "id": "1", + "name": "Accra to Kumasi", + "start": "BAD - this should be a dict", + "end": "2", + "route": { + "type": "LineString", + "coordinates": [ + [ + -0.173, + 5.626 + ], + [ + -0.178, + 5.807 + ], + [ + -0.112, + 5.971 + ], + [ + -0.211, + 5.963 + ], + [ + -0.321, + 6.17 + ], + [ + -0.488, + 6.29 + ], + [ + -0.56, + 6.421 + ], + [ + -0.752, + 6.533 + ], + [ + -0.867, + 6.607 + ], + [ + -1.101, + 6.585 + ], + [ + -1.304, + 6.623 + ], + [ + -1.461, + 6.727 + ], + [ + -1.628, + 6.713 + ] + ] + } + } + ] + } + ] +} \ No newline at end of file diff --git a/tests/fixtures/geojson_to_json/bad_1.meta.expected.json b/tests/fixtures/geojson_to_json/bad_1.meta.expected.json new file mode 100644 index 0000000..5fc29c6 --- /dev/null +++ b/tests/fixtures/geojson_to_json/bad_1.meta.expected.json @@ -0,0 +1,61 @@ +{ + "output_field_coverage": { + "/networks": { + "count": 1 + }, + "/networks/id": { + "count": 1 + }, + "/networks/name": { + "count": 1 + }, + "/networks/nodes": { + "count": 1 + }, + "/networks/nodes/id": { + "count": 2 + }, + "/networks/nodes/name": { + "count": 2 + }, + "/networks/nodes/location": { + "count": 2 + }, + "/networks/nodes/location/type": { + "count": 2 + }, + "/networks/nodes/location/coordinates": { + "count": 2 + }, + "/networks/nodes/status": { + "count": 1 + }, + "/networks/spans": { + "count": 1 + }, + "/networks/spans/id": { + "count": 1 + }, + "/networks/spans/name": { + "count": 1 + }, + "/networks/spans/start": { + "count": 1 + }, + "/networks/spans/end": { + "count": 1 + }, + "/networks/spans/route": { + "count": 1 + }, + "/networks/spans/route/type": { + "count": 1 + }, + "/networks/spans/route/coordinates": { + "count": 1 + } + }, + "inconsistent_phases_by_network_id": {}, + "inconsistent_organisations_by_network_id": {}, + "inconsistent_network_ids": [] +} \ No newline at end of file diff --git a/tests/fixtures/geojson_to_json/bad_1.nodes.geo.json b/tests/fixtures/geojson_to_json/bad_1.nodes.geo.json new file mode 100644 index 0000000..d2cd7ce --- /dev/null +++ b/tests/fixtures/geojson_to_json/bad_1.nodes.geo.json @@ -0,0 +1,42 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -0.174, + 5.625 + ] + }, + "properties": { + "id": "1", + "name": "Accra", + "network": { + "id": "a096d627-72e1-4f9b-b129-951b1737bff4", + "name": "Ghana Fibre Network" + } + } + }, + { + "type": "Feature", + "geometry": { + "type": "Point", + "coordinates": [ + -1.628, + 6.711 + ] + }, + "properties": { + "id": "2", + "name": "Kumasi", + "status": "operational", + "network": { + "id": "a096d627-72e1-4f9b-b129-951b1737bff4", + "name": "Ghana Fibre Network" + } + } + } + ] +} \ No newline at end of file diff --git a/tests/fixtures/geojson_to_json/bad_1.spans.geo.json b/tests/fixtures/geojson_to_json/bad_1.spans.geo.json new file mode 100644 index 0000000..2bf768e --- /dev/null +++ b/tests/fixtures/geojson_to_json/bad_1.spans.geo.json @@ -0,0 +1,86 @@ +{ + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "geometry": { + "type": "LineString", + "coordinates": [ + [ + -0.173, + 5.626 + ], + [ + -0.178, + 5.807 + ], + [ + -0.112, + 5.971 + ], + [ + -0.211, + 5.963 + ], + [ + -0.321, + 6.17 + ], + [ + -0.488, + 6.29 + ], + [ + -0.56, + 6.421 + ], + [ + -0.752, + 6.533 + ], + [ + -0.867, + 6.607 + ], + [ + -1.101, + 6.585 + ], + [ + -1.304, + 6.623 + ], + [ + -1.461, + 6.727 + ], + [ + -1.628, + 6.713 + ] + ] + }, + "properties": { + "id": "1", + "name": "Accra to Kumasi", + "start": "BAD - this should be a dict", + "end": { + "id": "2", + "name": "Kumasi", + "status": "operational", + "location": { + "type": "Point", + "coordinates": [ + -1.628, + 6.711 + ] + } + }, + "network": { + "id": "a096d627-72e1-4f9b-b129-951b1737bff4", + "name": "Ghana Fibre Network" + } + } + } + ] +} \ No newline at end of file diff --git a/tests/test_geojson_to_json.py b/tests/test_geojson_to_json.py index 1263e8d..75606bc 100644 --- a/tests/test_geojson_to_json.py +++ b/tests/test_geojson_to_json.py @@ -18,6 +18,8 @@ ("phases_inconsistent_1"), ("organisations_inconsistent_1"), ("network_inconsistent_1"), + # bad example + ("bad_1"), ]