[postgis-users] ST_DWithin advice
Adam Wright
awright.x64 at gmail.com
Mon Jun 2 11:58:36 PDT 2014
We have several tables with latitude and longitude columns (data type:
numeric) and I need to calculate whether a given lat/lon is within #
degrees of a point-radius ring. I came up with the function below. The user
supplied radius has to be in degrees (e.g. give me all records within 5 to
25 degrees of this lat/lon pair). Sample query: select * from mytable
where
dist_check(mytable.latitude.mytable.longitude,35.6895,139.6917,5,25)=1.
Any advice on solving the same problem using the geography data type when
the input radius is supplied in degrees?
CREATE OR REPLACE FUNCTION dist_check(lat1 numeric, lon1 numeric, lat2
numeric, lon2 numeric, innerradius numeric, outerradius numeric)
pt1 geometry;
pt2 geometry;
BEGIN
pt1 := ST_MakePoint(lon1,lat1);
pt2 := ST_MakePoint (lon2,lat2);
IF ST_DWithin(pt1,pt2,outerRadius) AND NOT ST_DWithin(pt1,pt2,innerRadius)
THEN return 1;
ELSE
return 0;
END IF;
Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20140602/fb507081/attachment.html>
More information about the postgis-users
mailing list