Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Delete all folders created by shapefile processing #1047

Merged
merged 2 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions terraso_backend/apps/core/gis/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import json
import os
import shutil
import uuid
import zipfile

Expand Down Expand Up @@ -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())

Expand Down
49 changes: 15 additions & 34 deletions terraso_backend/tests/core/gis/test_parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = """<?xml version="1.0" standalone="yes"?>
<gpx xmlns="http://www.topografix.com/GPX/1/1" version="1.1"
Expand Down Expand Up @@ -188,41 +189,21 @@
}


@pytest.fixture
def shapefile_zip(request):
shp, shx, prj = request.param
zip_file = tempfile.NamedTemporaryFile(suffix=".zip")
return zip_file


@pytest.mark.parametrize(
"shapefile_zip",
[
(b"<shapefile component 1>", b"<shapefile component 2>", b"<shapefile component 3>"),
(b"<shapefile component 4>", b"<shapefile component 5>", b"<shapefile component 6>"),
],
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(
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -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"
}
}
}