Loading...
Loading...
Convert JSON rows with WKT geometry strings into a GeoJSON FeatureCollection using raw PostGIS SQL.
npx skill4agent add mmbmf1/geospatial-skills geojson-wkt"POINT(-87.6 41.8)""LINESTRING(...)""POLYGON(...)"FeatureCollection{ id, wkt, ... }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_GeomFromText(row_data->>'wkt')
)::jsonb,
'properties', row_data - 'wkt'
)
)
)
FROM data_rows;SELECT jsonb_build_object(
'type', 'FeatureCollection',
'features', jsonb_agg(
jsonb_build_object(
'type', 'Feature',
'geometry', ST_AsGeoJSON(
ST_GeomFromText(t.wkt)
)::jsonb,
'properties', to_jsonb(t) - 'wkt'
)
)
)
FROM my_table t;ST_SetSRID(ST_GeomFromText(row_data->>'wkt'), 4326)ST_AsGeoJSON(ST_Transform(
ST_SetSRID(ST_GeomFromText(row_data->>'wkt'), <source_srid>),
4326
))::jsonb[
{ "id": 1, "name": "A", "wkt": "POINT(-87.6298 41.8781)", "status": "active" },
{ "id": 2, "name": "B", "wkt": "POINT(-95.3698 29.7604)", "status": "inactive" }
]{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-87.6298, 41.8781]
},
"properties": {
"id": 1,
"name": "A",
"status": "active"
}
}
]
}ST_GeomFromText(...)ST_AsGeoJSON(...)::jsonbrow_data - '<wkt_field>'ST_SetSRID