Changes between Initial Version and Version 1 of UsersWikiExamplesFindNearbyLatLon


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

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiExamplesFindNearbyLatLon

    v1 v1  
     1Here's a simple 80/20 solution for people with lon/lat tables of
     2points wanting to do distance searches.  It only works for places less
     3than 60degrees north or south, and only for points. If you know your
     4data is all more southerly than 60d, adjust the constant factor to
     5make the index portion of the search more selective (and hence,
     6efficient (faster)).
     7{{{
     8#!sql
     9--
     10-- ST_DWithin_Sphere(lonlat-point, lonlat-point, radius-in-meters) returns boolean
     11--
     12-- Meters/Degree @ 60N: select 1/distance_sphere('POINT(0 60)','POINT(1 60)') = 1.79866403673916e-05
     13--
     14CREATE OR REPLACE FUNCTION ST_DWithin_Sphere(geometry, geometry, float8)
     15        RETURNS boolean
     16        AS '
     17            SELECT $1 && ST_Expand($2,$3 * 1.79866403673916e-05)
     18               AND $2 && ST_Expand($1,$3 * 1.79866403673916e-05)
     19               AND ST_Distance_Sphere($1, $2) < $3
     20        ' LANGUAGE 'SQL' IMMUTABLE;
     21}}}