Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am building a system to bring in shapefile data from a PostGIS database and render it on top of OpenStreetMap tiles using the Leaflet.js library.

My table (sierra_county_parcels) has a polygon geometry field (geom) in SRID 4326, and a field called "upc" that contains the parcel ID. There are about 16,000 rows in sierra_county_parcels. What I need to do is retrieve all of the upc values for rows where the geom polygon is within the currently visible part of the Leaflet map control. I have already written the code to send the northwest and southeast lat/lon pairs and generate an SQL query, which looks like this:

SELECT upc AS pt_parcel_key
FROM  sierra_county_parcels
WHERE  ST_Within(geom, ST_Envelope(ST_GeomFromText('MULTIPOINT(33.167444534375925 -107.14931488037108, 33.08909661229697 -107.35513687133788)')))

The MULTIPOINT WKT is what I'm currently using to specify the corners (nw_latitude, nw_longitude, se_latitude, se_longitude).

The problem is that the query always returns 0 results. For this query, the map was zoomed out to show most of the region where the geometries should be.

Any ideas?

Thanks in advance,

John W.

share|improve this question
    
What if a polygon is not completely within your box and just a part of it is intersecting? –  Tamas Apr 21 '13 at 9:51
add comment

2 Answers

up vote 5 down vote accepted

Your x's and y's are reversed. Longitude is X and comes first, Latitude is Y and comes second.

share|improve this answer
    
Ended up changing the query a bit to use the && operator and BOX3D too. But yes, my lat/longs were indeed switched. Thanks much for your help! –  jwillis Apr 4 '13 at 18:40
add comment

Try using ST_MakeEnvelope to make a box:

SELECT upc AS pt_parcel_key
FROM sierra_county_parcels
WHERE geom && ST_MakeEnvelope($long_min, $lat_min, $long_max, $lat_max, 4326);

(note: replace the $ parameters with the appropriate value)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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