I'm trying to query all the objects within 100m and I want the result to be in GeoJSON not a list of GeoJSON. I'm using PostgreSQL 9.1, PostGIS 2.0. Tried this query:

SELECT ST_AsGeoJSON(geocolumn) 
FROM geotable 
WHERE ST_DWithin(geocolumn, 'POINT(1000 1000)', 100.0);

but the result is a list of GeoJSON.

Is there a way to convert the result of my query to a single GeoJSON output?

  • Welcome to gis.SE. It isn't clear from your question what you are trying to do, or the environment in which you are trying to do it. Please edit your question (just clock edit below the question) to add details of your environment (e.g. what database type you are trying to query (postgis? mysql? spatialite? oracle?), what version of that database, what the data is); what you've already tried / looked at, and what the results of those investigations / tests were; any constraints on the solution; and anything else that might help. Remember we can't see your screen, so you have to describe it. – BradHards Dec 7 '13 at 3:06
  • sorry im new here, im using postgis database and hoping to convert the all the query result to a single geojson output – noobprog Dec 7 '13 at 5:21
  • When I said "Please edit the question", I meant exactly that - click the "edit" link below the question. That puts everything into one place. Also, you still haven't told us what version of postgis, or any of the other information I asked for. – BradHards Dec 7 '13 at 5:35
  • I hope the information I added on my question is enough for everyone to understand it. – noobprog Dec 7 '13 at 5:40
  • You still haven't shown us a sample of the data, the current results, and what you expect instead. For example, are you expecting a MULTIPOINT object for the various "objects"? I'm sure you know what you want, but you aren't helping us to help you. Just describe it! – BradHards Dec 7 '13 at 6:55

As answered on the cross post, use an aggregate function like ST_Collect to group the geometries:

SELECT ST_AsGeoJSON(ST_Collect(geocolumn))
FROM geotable
WHERE ST_DWithin(geocolumn, 'POINT(1000 1000)', 100.0);

The result will either be a MULTI- geometry, or GEOMETRYCOLLECTION.


Use ST_AsGeoJSON which returns the geometry as a GeoJSON element.

SELECT ST_AsGeoJSON(geocolumn) FROM geotable WHERE ST_DWithin(geocolumn, 'POINT(1000 1000)', 100.0);

Please check https://gist.github.com/bmcbride/1913855 to combine multiple records into single GeoJSON.

  • isnt the result would be a list of geojson? – noobprog Dec 7 '13 at 2:48
  • Yes if the point you mentioned in your query exist more than one time in your table. – Farhat Abbas Dec 7 '13 at 3:03
  • can the result be combined to one geojson not as a list of geojson? – noobprog Dec 7 '13 at 3:34
  • Yes please check gist.github.com/bmcbride/1913855 but anyways I have also updated the answer. – Farhat Abbas Dec 7 '13 at 7:03

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.