Skip to content

Commit

Permalink
fix: return JSON format, unstrinigified
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryPTB committed Sep 9, 2024
1 parent 6d8b2d6 commit ecb68d7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/cap2geojson/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
)


def transform(xml: str) -> str:
def transform(xml: str) -> dict:
return to_geojson(xml)
31 changes: 15 additions & 16 deletions src/cap2geojson/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 []
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cap2geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit ecb68d7

Please sign in to comment.