diff --git a/terraso_backend/apps/core/gis/parsers.py b/terraso_backend/apps/core/gis/parsers.py index 94a63986f..881c3f189 100644 --- a/terraso_backend/apps/core/gis/parsers.py +++ b/terraso_backend/apps/core/gis/parsers.py @@ -15,6 +15,7 @@ import json import os +import shutil import uuid import zipfile @@ -157,10 +158,7 @@ def parse_shapefile(file): gdf_transformed = gdf.to_crs(crs=DEFAULT_CRS) # Delete extracted files - os.remove(os.path.join(tmp_folder, shp_filenames[0])) - os.remove(os.path.join(tmp_folder, shx_filenames[0])) - os.remove(os.path.join(tmp_folder, prj_filenames[0])) - os.rmdir(tmp_folder) + shutil.rmtree(tmp_folder) return json.loads(gdf_transformed.to_json()) diff --git a/terraso_backend/tests/core/gis/test_parsers.py b/terraso_backend/tests/core/gis/test_parsers.py index 26b481ff5..279a90902 100644 --- a/terraso_backend/tests/core/gis/test_parsers.py +++ b/terraso_backend/tests/core/gis/test_parsers.py @@ -16,20 +16,21 @@ import json import os import tempfile -import zipfile from importlib import resources -import geopandas as gpd import pytest from apps.core.gis.parsers import parse_file_to_geojson -from apps.core.gis.utils import DEFAULT_CRS 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"), ("resources/gis/kml_sample_3.kml", "resources/gis/kml_sample_3_geojson.json"), ] +SHAPEFILE_TEST_FILES = [ + ("resources/gis/shapefile_sample_1.zip", "resources/gis/shapefile_sample_1_geojson.json"), + ("resources/gis/shapefile_sample_2.zip", "resources/gis/shapefile_sample_2_geojson.json"), +] GPX_CONTENT = """ ", b"", b""), - (b"", b"", b""), - ], - indirect=True, + "file_path_expected", + SHAPEFILE_TEST_FILES, ) -def test_parse_shapefile(shapefile_zip): - # Create a GeoDataFrame with a single point - gdf = gpd.GeoDataFrame({"geometry": gpd.points_from_xy([0], [0])}, crs=DEFAULT_CRS) - - # Convert the GeoDataFrame to a Shapefile - with tempfile.TemporaryDirectory() as tmpdir: - shapefile_path = os.path.join(tmpdir, "test.shp") - gdf.to_file(shapefile_path) - - # Zip the Shapefile components - with zipfile.ZipFile(shapefile_zip.name, "w") as zf: - for component in ["shp", "shx", "prj"]: - zf.write(os.path.join(tmpdir, f"test.{component}"), f"test.{component}") +def test_parse_shapefile(file_path_expected): + file_path = file_path_expected[0] + with open(resources.files("tests").joinpath(file_path), "rb") as file: + shapefile_json = parse_file_to_geojson(file) - with open(shapefile_zip.name, "rb") as file: - shapefile_json = parse_file_to_geojson(file) + expected_file_path = file_path_expected[1] + with open(resources.files("tests").joinpath(expected_file_path), "rb") as file: + expected_json = json.load(file) - # Verify that the parsed Shapefile is equivalent to the original GeoDataFrame - gdf_json = json.loads(gdf.to_json()) - assert shapefile_json == gdf_json + print(f"shapefile_json: {shapefile_json}") + assert json.dumps(shapefile_json) == json.dumps(expected_json) @pytest.mark.parametrize( diff --git a/terraso_backend/tests/resources/gis/shapefile_sample_1.zip b/terraso_backend/tests/resources/gis/shapefile_sample_1.zip new file mode 100644 index 000000000..60e94353a Binary files /dev/null and b/terraso_backend/tests/resources/gis/shapefile_sample_1.zip differ diff --git a/terraso_backend/tests/resources/gis/shapefile_sample_1_geojson.json b/terraso_backend/tests/resources/gis/shapefile_sample_1_geojson.json new file mode 100644 index 000000000..ad70b993e --- /dev/null +++ b/terraso_backend/tests/resources/gis/shapefile_sample_1_geojson.json @@ -0,0 +1,25 @@ +{ + "type":"FeatureCollection", + "features":[ + { + "id":"0", + "type":"Feature", + "properties":{ + + }, + "geometry":{ + "type":"Point", + "coordinates":[ + -78.48247445411837, + -0.16708426936664011 + ] + } + } + ], + "crs":{ + "type":"name", + "properties":{ + "name":"urn:ogc:def:crs:OGC::CRS84" + } + } +} \ No newline at end of file diff --git a/terraso_backend/tests/resources/gis/shapefile_sample_2.zip b/terraso_backend/tests/resources/gis/shapefile_sample_2.zip new file mode 100644 index 000000000..de380d5f2 Binary files /dev/null and b/terraso_backend/tests/resources/gis/shapefile_sample_2.zip differ diff --git a/terraso_backend/tests/resources/gis/shapefile_sample_2_geojson.json b/terraso_backend/tests/resources/gis/shapefile_sample_2_geojson.json new file mode 100644 index 000000000..ad70b993e --- /dev/null +++ b/terraso_backend/tests/resources/gis/shapefile_sample_2_geojson.json @@ -0,0 +1,25 @@ +{ + "type":"FeatureCollection", + "features":[ + { + "id":"0", + "type":"Feature", + "properties":{ + + }, + "geometry":{ + "type":"Point", + "coordinates":[ + -78.48247445411837, + -0.16708426936664011 + ] + } + } + ], + "crs":{ + "type":"name", + "properties":{ + "name":"urn:ogc:def:crs:OGC::CRS84" + } + } +} \ No newline at end of file