diff --git a/terraso_backend/apps/core/gis/parsers.py b/terraso_backend/apps/core/gis/parsers.py index 881c3f189..395800ca0 100644 --- a/terraso_backend/apps/core/gis/parsers.py +++ b/terraso_backend/apps/core/gis/parsers.py @@ -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])) diff --git a/terraso_backend/tests/core/gis/test_parsers.py b/terraso_backend/tests/core/gis/test_parsers.py index e7f7a411f..8ea6c015f 100644 --- a/terraso_backend/tests/core/gis/test_parsers.py +++ b/terraso_backend/tests/core/gis/test_parsers.py @@ -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"), @@ -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 diff --git a/terraso_backend/tests/resources/gis/kml_sample_1_geojson.json b/terraso_backend/tests/resources/gis/kml_sample_1_geojson.json index f7869de2e..95e76792d 100644 --- a/terraso_backend/tests/resources/gis/kml_sample_1_geojson.json +++ b/terraso_backend/tests/resources/gis/kml_sample_1_geojson.json @@ -159,4 +159,4 @@ } } ] -} \ No newline at end of file +} diff --git a/terraso_backend/tests/resources/gis/kml_sample_2_geojson.json b/terraso_backend/tests/resources/gis/kml_sample_2_geojson.json index 5d0367d04..f96fddfa9 100644 --- a/terraso_backend/tests/resources/gis/kml_sample_2_geojson.json +++ b/terraso_backend/tests/resources/gis/kml_sample_2_geojson.json @@ -1205,4 +1205,4 @@ } } ] -} \ No newline at end of file +} diff --git a/terraso_backend/tests/resources/gis/kml_sample_3.kml b/terraso_backend/tests/resources/gis/kml_sample_3.kml index 5e93f28e3..41a9d0090 100644 --- a/terraso_backend/tests/resources/gis/kml_sample_3.kml +++ b/terraso_backend/tests/resources/gis/kml_sample_3.kml @@ -40,4 +40,4 @@ - \ No newline at end of file + diff --git a/terraso_backend/tests/resources/gis/kml_sample_3_geojson.json b/terraso_backend/tests/resources/gis/kml_sample_3_geojson.json index fcee77a32..cc8499260 100644 --- a/terraso_backend/tests/resources/gis/kml_sample_3_geojson.json +++ b/terraso_backend/tests/resources/gis/kml_sample_3_geojson.json @@ -38,4 +38,4 @@ } } ] -} \ No newline at end of file +} diff --git a/terraso_backend/tests/resources/gis/kmz_sample_1.kmz b/terraso_backend/tests/resources/gis/kmz_sample_1.kmz new file mode 100644 index 000000000..86bed55ac Binary files /dev/null and b/terraso_backend/tests/resources/gis/kmz_sample_1.kmz differ diff --git a/terraso_backend/tests/resources/gis/kmz_sample_1_geojson.json b/terraso_backend/tests/resources/gis/kmz_sample_1_geojson.json new file mode 100644 index 000000000..95e76792d --- /dev/null +++ b/terraso_backend/tests/resources/gis/kmz_sample_1_geojson.json @@ -0,0 +1,162 @@ +{ + "type":"FeatureCollection", + "features":[ + { + "type":"Feature", + "properties":{ + "Name":"2023/10/18", + "description":"\n
\n297 m, 12 min\n
\n
\n
\n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n
Distance297 m
Start18 October 2023 at 13:08:28 GMT+2
Stop18 October 2023 at 13:20:52 GMT+2
Duration12 minutes
Avg. Speed1,4 kmph
Elev. Max159 m
Elev. Min151 m
Elev. Gain5,32 m
Elev. Loss-4,93 m
Points29
\n
\n
\n
\nRecorded By Map Plus
\n\n
" + }, + "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 + ] + ] + } + } + ] +} diff --git a/terraso_backend/tests/resources/gis/shapefile_sample_1_geojson.json b/terraso_backend/tests/resources/gis/shapefile_sample_1_geojson.json index ad70b993e..f806eb692 100644 --- a/terraso_backend/tests/resources/gis/shapefile_sample_1_geojson.json +++ b/terraso_backend/tests/resources/gis/shapefile_sample_1_geojson.json @@ -5,7 +5,7 @@ "id":"0", "type":"Feature", "properties":{ - + }, "geometry":{ "type":"Point", @@ -22,4 +22,4 @@ "name":"urn:ogc:def:crs:OGC::CRS84" } } -} \ No newline at end of file +} diff --git a/terraso_backend/tests/resources/gis/shapefile_sample_2_geojson.json b/terraso_backend/tests/resources/gis/shapefile_sample_2_geojson.json index ad70b993e..f806eb692 100644 --- a/terraso_backend/tests/resources/gis/shapefile_sample_2_geojson.json +++ b/terraso_backend/tests/resources/gis/shapefile_sample_2_geojson.json @@ -5,7 +5,7 @@ "id":"0", "type":"Feature", "properties":{ - + }, "geometry":{ "type":"Point", @@ -22,4 +22,4 @@ "name":"urn:ogc:def:crs:OGC::CRS84" } } -} \ No newline at end of file +}