From 65266a74b92760d565373a78f7800d7404e65b1b Mon Sep 17 00:00:00 2001 From: Paul Schreiber Date: Tue, 19 Mar 2024 15:43:50 -0400 Subject: [PATCH] fix: correctly parse KMZ files (#1205) * 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 --- terraso_backend/apps/core/gis/parsers.py | 3 +- .../tests/core/gis/test_parsers.py | 19 ++ .../resources/gis/kml_sample_1_geojson.json | 2 +- .../resources/gis/kml_sample_2_geojson.json | 2 +- .../tests/resources/gis/kml_sample_3.kml | 2 +- .../resources/gis/kml_sample_3_geojson.json | 2 +- .../tests/resources/gis/kmz_sample_1.kmz | Bin 0 -> 1349 bytes .../resources/gis/kmz_sample_1_geojson.json | 162 ++++++++++++++++++ .../gis/shapefile_sample_1_geojson.json | 4 +- .../gis/shapefile_sample_2_geojson.json | 4 +- 10 files changed, 191 insertions(+), 9 deletions(-) create mode 100644 terraso_backend/tests/resources/gis/kmz_sample_1.kmz create mode 100644 terraso_backend/tests/resources/gis/kmz_sample_1_geojson.json 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 0000000000000000000000000000000000000000..86bed55ac3b0c581b6e03bfa7acc2bd033b866c0 GIT binary patch literal 1349 zcmV-L1-klBO9KQH000080MV9nSZnu`u+szp05lH(00;m80AgWtWiD%NY{i#Zj@w2M zhTn6F1`YTnBeM5Jn~`7~zu1Y9Ne)1ftr0QYK(@vj4|1|RqB@htX0Rf?# z`l|Y`s&2CQ_n&{QUz~~@-P@+ks&W&M5C_PXNtI^h?It+7{Xz#Xw(Gkh->$E#pDkW7B7Fm^4RUQ^`UFXL(Y>zvax!eYxrba!F zi+WY=+Im%7&0|8(?q_Ac3izGY*fr5d>kVrxc5!PLbyL;0ahYucl;m;SZi4d=gUQ<8 z@iT<{yg@#kP4M}|1_M7>q}|l3>IT*6x8Uh{7JE9$8ajGiXoDc8@Mi>39Y{8wo= zE^gc`-{gSw9XPfy3#gxsn*qxP)EwNtGGvilpe*2#Ox414y`HidUuNxM{OF_SA zSQnJ*`q;$X^Xa0{o}QSav+Y+;`w#cG;k@ciZEbpy)GIz)-4*rWt0(ehZtu^OzKKs4 z=@jOR9nK4~`Ju?{vbqcMEuSDChBmjt?-Ri2f0ap-#%2F*Yp=&!wx z6#e+%HOA=B*!?CrI9IpJD9YTiv~Ad}XhRSY9oq;dQI>M1wbT^j7lqqs@9j-oqqm^U zM-)$)8^U=K=sG%Y=+-Y!&XyqK8s7M0>)4G^>xaKL`6hVwq>c3do^_Gi?tKsu91#uu z@)Fp2=eLn;jCVnQU*Bcs7o0z*TI4K-$91O}%QgDKU(u#kXL#RrBNjweQeH02qk zfgjhR3b2XcM0v$ZafL@WL}NW6QZqq;ks-&77(Fr=Ycer}5dn+}F_%P)3}%!}as)F} zct1u^PFc?gLt&GQ4A+D(??*BU)fyPhW;vW_(`8VHgh0mih=8_94lK)hIh1ihM@A7$ z=*Uo^sPcYv=SPfwIHfaV80$kPoI){1$wDhWGs-ZZWzIOEQ|K7vPkIO;Jz|KGBLR_5 zQNcEX5IrI}H*8EYEITETL$#!5B-d2%sh(t3BbboP?;UM0*P|My%)Gz4Jakc!5G>h_6;?uydq=<~nTu|Wk)F}rL098mrc6$egG;V^KVZqHBVqK}UWRiZ zy&lelAY*}5s0hJq zWQaeYGQ@O_>GQxkI)5mvqw~i_boOl;&0oDy_Z9pPP)h*<6ay3h000O8(Ux;qYxk6} z(*ytjG!Fm(2mk;8000000000000000003fPb7d}TZER3W1qJ{B000310RT1t008*} H000001w4eW literal 0 HcmV?d00001 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 +}