20

I have a minLat, minLong, maxLat and maxLong from a box drawn on a map. Right now, my query to get all the points in the box looks like this:

SELECT *
FROM geomTable
WHERE (longitude BETWEEN minLon AND maxLon)
AND (latitude BETWEEN minLat AND maxLat)

I want to use the geometry column instead of lat/long columns to get the results. I tried this query:

SELECT *
FROM mytable
WHERE mytable.geom && ST_MakeEnvelope(minLon, minLat, maxLon, maxLat, 4326);

from this post: Select bounding box using postgis but it is returning no results.

Does anyone have an example of how to select all the points within a box created by min and max lat/longs using geometry in postgis?

5
  • What version of PostGIS are you using?
    – Mapperz
    May 14, 2013 at 16:02
  • My PostGIS is Version 2.0.1
    – bl8rchk
    May 14, 2013 at 16:06
  • 2
    are both geom columns in the same SRID?
    – nickves
    May 14, 2013 at 16:10
  • I think I might be way off with my approach...I only have one geom column. It's SRID is 2223
    – bl8rchk
    May 14, 2013 at 16:29
  • @bl8rchk what is the table geomTable and field mytable.geom? Jan 9, 2017 at 19:43

1 Answer 1

20

Your data is not in lat/lon, so you need to push your box into the coordinate space of your data:

SELECT *
FROM mytable
WHERE 
  mytable.geom && 
  ST_Transform(ST_MakeEnvelope(minLon, minLat, maxLon, maxLat, 4326), 2223);
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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