Loading...
Loading...
Convert JSON rows with latitude/longitude fields into a GeoJSON FeatureCollection using raw PostGIS SQL.
npx skill4agent add mmbmf1/geospatial-skills geojson-pointsFeatureCollection{ id, lat, lng, ... }WITH data_rows AS (
SELECT jsonb_array_elements($1::jsonb) AS row_data
)
SELECT jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(
jsonb_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(
ST_Point(
(row_data->>'lng')::float,
(row_data->>'lat')::float
)
)::jsonb,
'properties', row_data - 'lat' - 'lng'
)
)
)
FROM data_rows;SELECT jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(
jsonb_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(
ST_Point(lng, lat)
)::jsonb,
'properties', to_jsonb(t) - 'lat' - 'lng'
)
)
)
FROM my_table t;[longitude, latitude]-90..90-180..180type"FeatureCollection"featuresFeaturegeometry.type = "Point"geometry.coordinates = [lng, lat]propertiesST_Point(lng, lat)ST_MakePoint(lat, lng)ST_AsGeoJSON(... )::jsonbproperties