I'm trying to use PostgreSQL and PostGIS to return locations within a specific box bound.
Example
User has a map view on Google Maps
Google API returns box bounds using
map.getBounds()
- in my specific issue, the bounds from google maps arenorth: "51.52070000000002", south: "51.52070000000002", west: "-0.13450000000000184", east: "-0.13450000000000184"
and I'm using
ST_MakeEnvelope()
like so:ST_MakeEnvelope(-0.13450000000000184, 51.52070000000002, -0.13450000000000184, 51.52070000000002, 4326)
I have a locations table in my db with columns longitude, latitude and geom where geom is indexed with GIST and created by
ST_SetSRID(ST_MakePoint(longitude::numeric,latitude::numeric), 4326);
The SQL query just to sense check is
select geom && ST_MakeEnvelope(-0.13450000000000184, 51.52070000000002, -0.13450000000000184, 51.52070000000002, 4326)
where geom is
SRID=4326;POINT(-0.1309 51.5162)
and which should return true but returns false insteadI also had this query
SELECT id, ST_Y(ST_MakePoint(longitude, latitude)) AS y, ST_X(ST_MakePoint(longitude, latitude)) AS x FROM map_locations ml WHERE ST_SetSRID(ST_MakeBox2D(ST_Point(latitude, longitude), ST_Point(longitude, latitude) ), 4326) && ST_MakeEnvelope(-0.13450000000000184, 51.52070000000002, -0.13450000000000184, 51.52070000000002, 4326)
but these created box bounds in the incorrect places.
I also tried
ST_WITHIN()
andST_CONTAINS()
with the same result
What am I doing wrong?
SELECT id, ST_Y(ST_MakePoint(longitude, latitude)) AS y, ST_X(ST_MakePoint(longitude, latitude)) AS x FROM map_locations ml WHERE ST_SetSRID( ST_MakeBox2D( ST_Point(latitude, longitude), ST_Point(longitude, latitude) ), 4326 ) && ST_MakeEnvelope(-0.13450000000000184, 51.52070000000002, -0.13450000000000184, 51.52070000000002, 4326)
but these created box bounds in the incorrect places. I also tried ST_WITHIN and ST_CONTAINS with the same result