Skip to content

Instantly share code, notes, and snippets.

@brambow
Last active April 25, 2023 15:44
Show Gist options
  • Save brambow/889aca48831e189a62eec5a70067bf8e to your computer and use it in GitHub Desktop.
Save brambow/889aca48831e189a62eec5a70067bf8e to your computer and use it in GitHub Desktop.
PostGIS query to build a GeoJSON FeatureCollection
SELECT json_build_object(
'type', 'FeatureCollection',
'crs', json_build_object(
'type', 'name',
'properties', json_build_object(
'name', 'EPSG:4326'
)
),
'features', json_agg(
json_build_object(
'type', 'Feature',
'id', {id}, -- the GeoJson spec includes an 'id' field, but it is optional, replace {id} with your id field
'geometry', ST_AsGeoJSON(geom)::json,
'properties', json_build_object(
-- list of fields
'field1', field1,
'field2', field2
)
)
)
)
FROM yourtable; --replace with your table name
@Bnaya
Copy link

Bnaya commented Aug 18, 2019

How would it look like?
Merging the polygons?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment