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
Distance | \n297 m | \n
Start | \n18 October 2023 at 13:08:28 GMT+2 | \n
Stop | \n18 October 2023 at 13:20:52 GMT+2 | \n
Duration | \n12 minutes | \n
Avg. Speed | \n1,4 kmph | \n
Elev. Max | \n159 m | \n
Elev. Min | \n151 m | \n
Elev. Gain | \n5,32 m | \n
Elev. Loss | \n-4,93 m | \n
Points | \n29 | \n