2

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?

1 Answer 1

4

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; 
2
  • looks great. However i am trying to fix this Error, not having much luck: "subquery in FROM must have an alias" Commented Feb 23, 2019 at 7:57
  • ah ok, an extra closing ). It's now removed Commented Feb 23, 2019 at 13:19

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.