Skip to content

Commit

Permalink
feat: add helper route to convert fgb --> geojson
Browse files Browse the repository at this point in the history
  • Loading branch information
spwoodcock committed Feb 26, 2024
1 parent 3b9c7e7 commit 649a459
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from typing import Optional

import geojson
import requests
from fastapi import (
APIRouter,
BackgroundTasks,
Expand All @@ -48,7 +49,11 @@
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, parse_and_filter_geojson
from app.db.postgis_utils import (
check_crs,
flatgeobuf_to_geojson,
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 @@ -1049,6 +1054,48 @@ async def download_features(
return Response(content=json.dumps(feature_collection), headers=headers)


@router.get("/convert-fgb-to-geojson/")
async def convert_fgb_to_geojson(
url: str,
db: Session = Depends(database.get_db),
current_user: AuthUser = Depends(login_required),
):
"""Convert flatgeobuf to GeoJSON format, extracting GeometryCollection.
Helper endpoint to test data extracts during project creation.
Required as the flatgeobuf files wrapped in GeometryCollection
cannot be read in QGIS or other tools.
Args:
url (str): URL to the flatgeobuf file.
db (Session): The database session, provided automatically.
current_user (AuthUser): Check if user is logged in.
Returns:
Response: The HTTP response object containing the downloaded file.
"""
with requests.get(url) as response:
if not response.ok:
raise HTTPException(
status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
detail="Download failed for data extract",
)
data_extract_geojson = await flatgeobuf_to_geojson(db, response.content)

if not data_extract_geojson:
raise HTTPException(
status_code=HTTPStatus.UNPROCESSABLE_ENTITY,
detail=("Failed to convert flatgeobuf --> geojson"),
)

headers = {
"Content-Disposition": ("attachment; filename=fmtm_data_extract.geojson"),
"Content-Type": "application/media",
}

return Response(content=json.dumps(data_extract_geojson), headers=headers)


@router.get("/tiles/{project_id}")
async def generate_project_tiles(
background_tasks: BackgroundTasks,
Expand Down

0 comments on commit 649a459

Please sign in to comment.