beOpen

This query gets all of the other parcels within 300 US feet from the parcel numbered ‘XXXXXX’ that are either contained in that 300 foot buffer or are intersected by it.

The parcel number column is uniquely indexed both ascending and decending so postgres will find the value equally quickly if its a higher value using the decending index or if its lower using the acending index. I’ve found that postgres’ index engine does very well deciding on which one to use as long as I provide them.

The query could be written a bit more simply but using the aliases gives me some flexibility to use this same query in other code, so I only have to call it once.

Obviously the table and field names, and the radius distance are parameterized in the code so that it will operate on any PostGIS enabled table.

Also you could skip the box3d() calls and parse the geometry using php, but I’ve found that PostGIS does it much faster and if you have many results this is a good thing.

SELECT p.*, oid, xmin(box3d(p.the_geom)) AS minx, ymin(box3d(p.the_geom)) AS miny, xmax(box3d(p.the_geom)) AS maxx, ymax(box3d(p.the_geom)) AS maxy FROM parcels p, ( SELECT the_geom FROM parcels WHERE parcel = 'XXXXXX' ) s 
WHERE p.the_geom && expand(s.the_geom,300)
AND contains(buffer(s.the_geom, 300), p.the_geom)
AND intersects(buffer(s.the_geom, 300), p.the_geom)
 
postgresql/postgis_query.txt · Ultima modifica: 19/07/2006 23:16
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki