| 1 | Here's a simple 80/20 solution for people with lon/lat tables of |
| 2 | points wanting to do distance searches. It only works for places less |
| 3 | than 60degrees north or south, and only for points. If you know your |
| 4 | data is all more southerly than 60d, adjust the constant factor to |
| 5 | make the index portion of the search more selective (and hence, |
| 6 | efficient (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 | -- |
| 14 | CREATE 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 | }}} |