| 22 | If 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 |
| 25 | CREATE 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 | }}} |