Skip to content

Commit

Permalink
fix: correctly parse KMZ files (#1205)
Browse files Browse the repository at this point in the history
* chore: add sample KMZ files

* chore: add kmz parsing test

* fix: correctly pass file buffer and not file path to parse_kml_file

* style: add missing newline

* chore: add trailing newlines to sample JSON and KML files
  • Loading branch information
paulschreiber authored Mar 19, 2024
1 parent 70dd3a9 commit 65266a7
Show file tree
Hide file tree
Showing 10 changed files with 191 additions and 9 deletions.
3 changes: 2 additions & 1 deletion terraso_backend/apps/core/gis/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ def parse_kmz_file(file):

kml_file = zip.extract(kml_filenames[0], tmp_folder)

geojson = parse_kml_file(kml_file)
with open(kml_file, "rb") as file_buffer:
geojson = parse_kml_file(file_buffer)

# Delete extracted files
os.remove(os.path.join(tmp_folder, kml_filenames[0]))
Expand Down
19 changes: 19 additions & 0 deletions terraso_backend/tests/core/gis/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@

from apps.core.gis.parsers import parse_file_to_geojson

KMZ_TEST_FILES = [
("resources/gis/kmz_sample_1.kmz", "resources/gis/kmz_sample_1_geojson.json"),
]
KML_TEST_FILES = [
("resources/gis/kml_sample_1.kml", "resources/gis/kml_sample_1_geojson.json"),
("resources/gis/kml_sample_2.kml", "resources/gis/kml_sample_2_geojson.json"),
Expand Down Expand Up @@ -221,6 +224,22 @@ def test_parse_kml_file(kml_file_path_expected):
assert json.dumps(kml_json) == json.dumps(expected_json)


@pytest.mark.parametrize(
"kmz_file_path_expected",
KMZ_TEST_FILES,
)
def test_parse_kmz_file(kmz_file_path_expected):
kmz_file_path = kmz_file_path_expected[0]
with open(resources.files("tests").joinpath(kmz_file_path), "rb") as file:
kmz_json = parse_file_to_geojson(file)

expected_file_path = kmz_file_path_expected[1]
with open(resources.files("tests").joinpath(expected_file_path), "rb") as file:
expected_json = json.load(file)

assert json.dumps(kmz_json) == json.dumps(expected_json)


@pytest.fixture
def gpx_file(request):
gpx_contents, file_extension = request.param
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,4 @@
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1205,4 +1205,4 @@
}
}
]
}
}
2 changes: 1 addition & 1 deletion terraso_backend/tests/resources/gis/kml_sample_3.kml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
</Polygon>
</Placemark>
</Document>
</kml>
</kml>
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@
}
}
]
}
}
Binary file not shown.
162 changes: 162 additions & 0 deletions terraso_backend/tests/resources/gis/kmz_sample_1_geojson.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"type":"FeatureCollection",
"features":[
{
"type":"Feature",
"properties":{
"Name":"2023/10/18",
"description":"<style>\ntable{border-collapse:collapse; margin:5 5 10 0px;}\ntable td{border: 1px solid #e0e0e0; padding:5px;}\ntable .head {font-weight: bold; background-color:#f9f8f8;}\ntable .light {background-color: #f9f8f8;}\ntable .dark {background-color: #f3f2f2;}\n.datatablebox table {max-width=600px; margin:5 5 5 0px;}\n</style>\n<div id=\"com.miocool.mapplus.subtitle\">\n297&nbsp;m,&nbsp;12&nbsp;min\n</div>\n<br/>\n<div id=\"mapplus-base-properties\" class=\"datatablebox\">\n<table>\n <tr style=\"background-color: #f9f8f8;\">\n <td><b>Distance</b></td>\n <td>297&nbsp;m</td>\n </tr>\n <tr style=\"background-color: #f3f2f2;\">\n <td><b>Start</b></td>\n <td>18&nbsp;October&nbsp;2023&nbsp;at&nbsp;13:08:28&nbsp;GMT+2</td>\n </tr>\n <tr style=\"background-color: #f9f8f8;\">\n <td><b>Stop</b></td>\n <td>18&nbsp;October&nbsp;2023&nbsp;at&nbsp;13:20:52&nbsp;GMT+2</td>\n </tr>\n <tr style=\"background-color: #f3f2f2;\">\n <td><b>Duration</b></td>\n <td>12&nbsp;minutes</td>\n </tr>\n <tr style=\"background-color: #f9f8f8;\">\n <td><b>Avg.&nbsp;Speed</b></td>\n <td>1,4&nbsp;kmph</td>\n </tr>\n <tr style=\"background-color: #f3f2f2;\">\n <td><b>Elev.&nbsp;Max</b></td>\n <td>159&nbsp;m</td>\n </tr>\n <tr style=\"background-color: #f9f8f8;\">\n <td><b>Elev.&nbsp;Min</b></td>\n <td>151&nbsp;m</td>\n </tr>\n <tr style=\"background-color: #f3f2f2;\">\n <td><b>Elev.&nbsp;Gain</b></td>\n <td>5,32&nbsp;m</td>\n </tr>\n <tr style=\"background-color: #f9f8f8;\">\n <td><b>Elev.&nbsp;Loss</b></td>\n <td>-4,93&nbsp;m</td>\n </tr>\n <tr style=\"background-color: #f3f2f2;\">\n <td><b>Points</b></td>\n <td>29</td>\n </tr>\n</table>\n</div>\n<br/>\n<div id=\"com.miocool.mapplus.recorder\" style=\"color:#b7b7b7;font-size:11px;line-height:30px\">\nRecorded By <a href=\"https://itunes.apple.com/us/app/id438868200?mt=8\">Map Plus</a><br/>\n\n</div>"
},
"geometry":{
"type":"LineString",
"coordinates":[
[
22.59663677,
-33.98605253,
155.633652
],
[
22.5966076,
-33.98595971,
154.259445
],
[
22.59660618,
-33.98586526,
152.609299
],
[
22.59650336,
-33.98580228,
153.185974
],
[
22.59639406,
-33.98576777,
153.788025
],
[
22.59627639,
-33.9857417,
155.774704
],
[
22.59618089,
-33.98568352,
156.403198
],
[
22.59610081,
-33.98561315,
157.046158
],
[
22.59603216,
-33.98553925,
156.952423
],
[
22.59592388,
-33.98548113,
157.632202
],
[
22.5958389,
-33.98541891,
158.116058
],
[
22.59580311,
-33.98532135,
157.234528
],
[
22.59571012,
-33.98525727,
158.816055
],
[
22.59584222,
-33.98529332,
156.142609
],
[
22.59595874,
-33.98529793,
153.434128
],
[
22.59600202,
-33.9853111,
153.49176
],
[
22.59609239,
-33.98536789,
153.464493
],
[
22.59614266,
-33.98545746,
153.648254
],
[
22.59627174,
-33.98553563,
152.890488
],
[
22.59637918,
-33.98557102,
151.717392
],
[
22.59646774,
-33.98563175,
150.707565
],
[
22.59654701,
-33.98569996,
150.843887
],
[
22.59662362,
-33.98577448,
150.770142
],
[
22.59669836,
-33.98584456,
151.435516
],
[
22.5968001,
-33.98590725,
151.944595
],
[
22.59682657,
-33.98600427,
152.788086
],
[
22.59671285,
-33.98607463,
154.425354
],
[
22.59671103,
-33.986171,
156.02887
],
[
22.59671103,
-33.986171,
156.02887
]
]
}
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id":"0",
"type":"Feature",
"properties":{

},
"geometry":{
"type":"Point",
Expand All @@ -22,4 +22,4 @@
"name":"urn:ogc:def:crs:OGC::CRS84"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"id":"0",
"type":"Feature",
"properties":{

},
"geometry":{
"type":"Point",
Expand All @@ -22,4 +22,4 @@
"name":"urn:ogc:def:crs:OGC::CRS84"
}
}
}
}

0 comments on commit 65266a7

Please sign in to comment.