Skip to content

Commit

Permalink
feat(backend): add helper route to convert multipolygon geojson --> p…
Browse files Browse the repository at this point in the history
…olygons
  • Loading branch information
spwoodcock committed Jul 16, 2024
1 parent 5abab5c commit c688e11
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/backend/app/helpers/helper_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
from app.db.postgis_utils import (
add_required_geojson_properties,
javarosa_to_geojson_geom,
multipolygon_to_polygon,
parse_and_filter_geojson,
)
from app.models.enums import GeometryType, HTTPStatus, XLSFormType
Expand Down Expand Up @@ -267,3 +268,29 @@ async def view_user_oauth_token(
status_code=HTTPStatus.OK,
content={"access_token": request.cookies.get(cookie_name)},
)


@router.post("/multipolygons-to-polygons")
async def flatten_multipolygons_to_polygons(
geojson: UploadFile,
current_user: AuthUser = Depends(login_required),
):
"""If any MultiPolygons are present, replace with multiple Polygons."""
featcol = parse_and_filter_geojson(await geojson.read(), filter=False)
if not featcol:
raise HTTPException(
status_code=HTTPStatus.UNPROCESSABLE_ENTITY, detail="No geometries present"
)
multi_to_single_polygons = multipolygon_to_polygon(featcol)

if multi_to_single_polygons:
headers = {
"Content-Disposition": ("attachment; filename=flattened_polygons.geojson"),
"Content-Type": "application/media",
}
return Response(content=json.dumps(multi_to_single_polygons), headers=headers)

raise HTTPException(
status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
detail="Your geojson file is invalid.",
)

0 comments on commit c688e11

Please sign in to comment.