Skip to content

Commit

Permalink
fix: task splitting with custom data extract (#1255)
Browse files Browse the repository at this point in the history
* fix: handle GeometryCollection wrappers in geojson parse

* fix: task splitting when custom data extract uploaded
  • Loading branch information
spwoodcock authored Feb 23, 2024
1 parent 0a3de42 commit 55bfdf5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/backend/app/db/postgis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ def parse_and_filter_geojson(
) -> Optional[geojson.FeatureCollection]:
"""Parse geojson string and filter out incomaptible geometries."""
geojson_parsed = geojson.loads(geojson_str)

if isinstance(geojson_parsed, geojson.FeatureCollection):
log.debug("Already in FeatureCollection format, skipping reparse")
featcol = geojson_parsed
Expand All @@ -352,9 +353,18 @@ def parse_and_filter_geojson(
)

# Exit early if no geoms
if not featcol.get("features", []):
if not (features := featcol.get("features", [])):
return None

# Strip out GeometryCollection wrappers
for feat in features:
geom = feat.get("geometry")
if (
geom.get("type") == "GeometryCollection"
and len(geom.get("geometries")) == 1
):
feat["geometry"] = geom.get("geometries")[0]

# Return unfiltered featcol
if not filter:
return featcol
Expand All @@ -363,7 +373,7 @@ def parse_and_filter_geojson(
geom_type = get_featcol_main_geom_type(featcol)
features_filtered = [
feature
for feature in featcol.get("features", [])
for feature in features
if feature.get("geometry", {}).get("type", "") == geom_type
]

Expand Down Expand Up @@ -412,9 +422,11 @@ def is_valid_coordinate(coord):

if (input_geojson_type := input_geojson.get("type")) == "FeatureCollection":
features = input_geojson.get("features", [])
log.warning(features[-1])
coordinates = (
features[-1].get("geometry", {}).get("coordinates", []) if features else []
)
log.warning(coordinates)
elif input_geojson_type == "Feature":
coordinates = input_geojson.get("geometry", {}).get("coordinates", [])
else:
Expand Down
10 changes: 7 additions & 3 deletions src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
from app.auth.roles import mapper, org_admin, project_admin, super_admin
from app.central import central_crud
from app.db import database, db_models
from app.db.postgis_utils import check_crs
from app.db.postgis_utils import check_crs, parse_and_filter_geojson
from app.models.enums import TILES_FORMATS, TILES_SOURCE, HTTPStatus
from app.organisations import organisation_deps
from app.projects import project_crud, project_deps, project_schemas
Expand Down Expand Up @@ -455,8 +455,12 @@ async def task_split(
# read data extract
parsed_extract = None
if extract_geojson:
parsed_extract = geojson.loads(await extract_geojson.read())
await check_crs(parsed_extract)
geojson_data = json.dumps(json.loads(await extract_geojson.read()))
parsed_extract = parse_and_filter_geojson(geojson_data, filter=False)
if parsed_extract:
await check_crs(parsed_extract)
else:
log.warning("Parsed geojson file contained no geometries")

return await project_crud.split_geojson_into_tasks(
db,
Expand Down

0 comments on commit 55bfdf5

Please sign in to comment.