1

I have the following query on a PostGIS Database

SELECT row_to_json(fc) 
FROM ( 
  SELECT 'FeatureCollection' As type, 
         array_to_json(array_agg(f)) As features 
  FROM (
    SELECT 'Feature' As type, 
      ST_AsGeoJSON(lg.geom)::json As geometry, 
      row_to_json((id, name)) As properties 
    FROM lines As lg) As f
  ) As fc;

which returns a GEOJSON Object of all features in the database.

How do i filter the results by a Bounding Box already in the query?

3

You can use the && operator on a new box. Don't forget to use the same SRID for the box and for your data.

SELECT row_to_json(fc) 
 FROM ( 
     SELECT 'FeatureCollection' As type, 
            array_to_json(array_agg(f)) As features 
       FROM (
         SELECT 'Feature' As type, 
         ST_AsGeoJSON(lg.geom)::json As geometry, 
         row_to_json((id, name)) As properties 
      FROM lines As lg
      WHERE lg.geom && 
          ST_SETSRID(
            ST_MakeBox2D(
                ST_MakePoint(p_BB_XLong_MIN_3857, p_BB_YLat_MIN_3857),
                ST_MakePoint(p_BB_XLong_MAX_3857, p_BB_YLat_MAX_3857)),
          3857)
) As f) As fc;
  • looks great. However i am trying to fix this Error, not having much luck: "subquery in FROM must have an alias" – niaR Feb 23 at 7:57
  • ah ok, an extra closing ). It's now removed – JGH Feb 23 at 13:19

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.