Changes between Version 2 and Version 3 of UsersWikiExamplesFindNearby


Ignore:
Timestamp:
Jul 8, 2009, 3:26:23 PM (15 years ago)
Author:
pramsey
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiExamplesFindNearby

    v2 v3  
    44One of the most common spatial SQL queries answers the question: "What are all the features within distance X of this other feature?"  In concrete terms: "How many cell towers are within 1km of my current location?" or "What are the schools within 5km of this house?"
    55
    6 Taking the schools example as a template, here is a simple query to answer the question:
     6For versions 1.3 and higher, the answer is very simple:
     7
     8{{{
     9#!sql
     10SELECT the_geom, school_name
     11 FROM schools
     12 WHERE st_dwithin(the_geom, 'POINT(100312 102312)',5000)
     13
     14}}}
     15
     16For older versions, you will have to hand-roll the query:
    717
    818{{{
     
    3141
    3242Note the new logical clause before the AND.  The && operator is the index operator, and represents a bounding box filter between the feature in the_geom and the comparison feature.  The Expand() function is a utility function that takes an existing feature and returns a new feature with a larger bounding box.  The result of this new query is to first use the index to find all features where the bounding boxes interact within a tolerance of 5km, then use the exact Distance() function to strip the final result set out of the intermediate bounding box result set.
    33 
    34 Newer way for version 1.3 and above
    35 
    36 {{{
    37 #!sql
    38 SELECT the_geom, school_name
    39  FROM schools
    40  WHERE st_dwithin(the_geom, 'POINT(100312 102312)',5000)
    41 
    42 }}}