Skip to content

Commit

Permalink
refactor: improve flatgeobuf loading and generation (#1235)
Browse files Browse the repository at this point in the history
* refactor: update flatgeobuf --> geojson to extract properties

* fix: create flatgeobuf files with spatial index by default

* docs: comment note for future fgb generation

* docs: extra info on fgb generation with tags
  • Loading branch information
spwoodcock authored Feb 20, 2024
1 parent 9a8867d commit 9f00b9b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
23 changes: 19 additions & 4 deletions src/backend/app/db/postgis_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ async def geojson_to_flatgeobuf(
"""
# FIXME make this with with properties / tags
# FIXME this is important
# FIXME but difficult to guarantee users upload geojson
# FIXME With required properties included
# sql = """
# DROP TABLE IF EXISTS public.temp_features CASCADE;

Expand Down Expand Up @@ -171,7 +173,7 @@ async def geojson_to_flatgeobuf(
FROM data
) AS f;
SELECT ST_AsFlatGeobuf(fgb_data)
SELECT ST_AsFlatGeobuf(fgb_data, true)
FROM (SELECT * FROM public.temp_features as geoms) AS fgb_data;
"""
# Run the SQL
Expand Down Expand Up @@ -201,6 +203,7 @@ async def flatgeobuf_to_geojson(
Returns:
geojson.FeatureCollection: A FeatureCollection object.
"""
# FIXME can we use SELECT * to extract all fields into geojson properties?
sql = text(
"""
DROP TABLE IF EXISTS public.temp_fgb CASCADE;
Expand All @@ -215,11 +218,23 @@ async def flatgeobuf_to_geojson(
SELECT jsonb_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(fgb_data.geom)::jsonb,
'properties', fgb_data.properties::jsonb
'properties', jsonb_build_object(
'osm_id', fgb_data.osm_id,
'tags', fgb_data.tags,
'version', fgb_data.version,
'changeset', fgb_data.changeset,
'timestamp', fgb_data.timestamp
)::jsonb
) AS feature
FROM (
SELECT *,
NULL as properties
SELECT
geom,
NULL as osm_id,
NULL as tags,
NULL as version,
NULL as changeset,
NULL as timestamp
FROM ST_FromFlatGeobuf(null::temp_fgb, :fgb_bytes)
) AS fgb_data
) AS features;
Expand Down
4 changes: 3 additions & 1 deletion src/backend/app/projects/project_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,7 +1048,9 @@ async def download_features(
feature_collection = await project_crud.get_project_features_geojson(db, project_id)

headers = {
"Content-Disposition": "attachment; filename=project_features.geojson",
"Content-Disposition": (
f"attachment; filename=fmtm_project_{project_id}_features.geojson"
),
"Content-Type": "application/media",
}

Expand Down

0 comments on commit 9f00b9b

Please sign in to comment.