I'm trying to return a GeoJSON FeatureCollection object from a SQL-query with PostgreSQL 9.3 and PostGIS 2.1.

This is my query so far:

SELECT row_to_json(fc) AS p
 FROM (SELECT 'FeatureCollection' AS type, array_to_json(array_agg(f)) AS features 
 FROM (SELECT 'Feature' AS type, ST_AsGeoJSON("wkb_geometry")::json AS geometry
 FROM "4547cd00-b570-4504-9796-cf2f61dbebe4") AS f) AS fc;

I'm getting the error:

ERROR:  parse error - invalid geometry

However, if I just run this query:

SELECT ST_AsGeoJSON("wkb_geometry")::json AS geometry
 FROM "4547cd00-b570-4504-9796-cf2f61dbebe4";

It works and print out a nice geometry. If I ignore the ST_AsGeoJSON it also prints out a result, but not something I can use as a GeoJSON FeatureCollection, as I need the type and coordinates seperated into JSON.

  • Ok, noted. I've also tried to add WHERE ST_IsValid(ST_AsGeoJSON("wkb_geometry")) at my SELECT, but with same error as before. – user809829 Jun 24 '14 at 10:27
  • Might this be of any help? – Michal Zimmermann Jul 27 '15 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.