From ecb68d70ed8444abfeb3202ba1819d47e5fb39f8 Mon Sep 17 00:00:00 2001 From: RoryPTB <47696929+RoryPTB@users.noreply.github.com> Date: Mon, 9 Sep 2024 15:14:23 +0200 Subject: [PATCH] fix: return JSON format, unstrinigified --- src/cap2geojson/__init__.py | 2 +- src/cap2geojson/convert.py | 31 +++++++++++++++---------------- tests/test_cap2geojson.py | 2 +- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/src/cap2geojson/__init__.py b/src/cap2geojson/__init__.py index ffc946e..9c2d673 100644 --- a/src/cap2geojson/__init__.py +++ b/src/cap2geojson/__init__.py @@ -32,5 +32,5 @@ ) -def transform(xml: str) -> str: +def transform(xml: str) -> dict: return to_geojson(xml) diff --git a/src/cap2geojson/convert.py b/src/cap2geojson/convert.py index cf26213..39bb61a 100644 --- a/src/cap2geojson/convert.py +++ b/src/cap2geojson/convert.py @@ -137,7 +137,9 @@ def get_polygon_coordinates(single_area: dict) -> list: if "polygon" in single_area: # Takes form "x,y x,y x,y" but with newlines that need to be removed polygon_str = single_area["polygon"].replace("\n", "").split() - polygon_list = [list(map(float, coord.split(","))) for coord in polygon_str] # noqa + polygon_list = [ + list(map(float, coord.split(","))) for coord in polygon_str + ] # noqa return ensure_counter_clockwise(polygon_list) return [] @@ -179,14 +181,14 @@ def preprocess_alert(xml: str) -> str: return re.sub(r"<(/?)cap:(\w+)", r"<\1\2", xml) -def to_geojson(xml: str) -> str: +def to_geojson(xml: str) -> dict: """Takes the CAP alert XML and converts it to a GeoJSON. Args: xml (str): The CAP XML string. Returns: - str: The final GeoJSON object stringified. + str: The final GeoJSON object. """ processed_xml = preprocess_alert(xml) @@ -210,19 +212,16 @@ def to_geojson(xml: str) -> str: alert_geometry = get_geometry(area) - result = json.dumps( - { - "type": "FeatureCollection", - "features": [ - { - "type": "Feature", - "properties": alert_properties, - "geometry": alert_geometry, - } - ], - }, - indent=4, - ) + result = { + "type": "FeatureCollection", + "features": [ + { + "type": "Feature", + "properties": alert_properties, + "geometry": alert_geometry, + } + ], + } try: geojson.loads(result) diff --git a/tests/test_cap2geojson.py b/tests/test_cap2geojson.py index 7d1abeb..ae399d8 100644 --- a/tests/test_cap2geojson.py +++ b/tests/test_cap2geojson.py @@ -43,7 +43,7 @@ def sc_alert(): def test_to_geojson(sc_alert): with open("tests/output/sc.geojson", "r") as f: - expected = f.read() + expected = json.load(f) assert to_geojson(sc_alert) == expected