Changes between Version 1 and Version 2 of UsersWikiExamplesFindNearbyLatLon


Ignore:
Timestamp:
Aug 11, 2009, 10:45:42 AM (13 years ago)
Author:
pramsey
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • UsersWikiExamplesFindNearbyLatLon

    v1 v2  
    2020        ' LANGUAGE 'SQL' IMMUTABLE;
    2121}}}
     22If you are willing to accept some slop in the final distance calculation, you can replace it with an approximation based on the latitude of one of the arguments. This will allow the function to accept arguments that are lines and polygons as well as points.
     23{{{
     24#!sql
     25CREATE OR REPLACE FUNCTION ST_DWithin_Sphere(geometry, geometry, float8)
     26        RETURNS boolean
     27        AS '
     28            SELECT $1 && ST_Expand($2,$3 * 1.79866403673916e-05)
     29               AND $2 && ST_Expand($1,$3 * 1.79866403673916e-05)
     30               AND ST_Distance($1, $2) <
     31                        $3 / ST_Distance_Sphere(
     32                                   ST_MakePoint(0, ST_Y(ST_Centroid($1))),
     33                                   ST_MakePoint(1, ST_Y(ST_Centroid($1))))
     34        ' LANGUAGE 'SQL' IMMUTABLE;
     35}}}