2
votes

I need to calculate if one point is not more distant than a given radius from another point. I used the function ST_DWithin, in google maps I get lanLot using "what is here" section of two points. First: (43.2137617, 76.8598076) and second (43.220109 76.865100). The distance between them is 1.25km. My query

SELECT ST_DWithin (
   ST_GeomFromText('POINT(76.8598076 43.2137617)',3857),
   ST_GeomFromText('POINT(76.865100 43.220109)',3857),
   100
);

And it always returns true. I think that I put radius 100 meters and used SRID 3875 to use meters. What is wrong?

1

1 Answers

4
votes

The coordinates you are using are not in CRS 3857 but are rather unprojected lat-long, i.e. CRS 4326, so you are looking for points within 100 degrees of each others. You would need to create the point in 4326, project it in 3857 using ST_Transform and then make the distance computation in meters.

SELECT ST_DWithin (
   ST_Transform(ST_GeomFromText('POINT(76.8598076 43.2137617)',4326),3857),
   ST_Transform(ST_GeomFromText('POINT(76.865100 43.220109)',4326),3857),
   100
);

CRS 3857 is a projection that does not preserve distances that well as you move away from the equator. You may want to use ST_Distance_Sphere instead. Comparing the two methods, the first one gives 1134m between the points and the second one 825m... quite a difference!

SELECT ST_Distance_Sphere (
   ST_GeomFromText('POINT(76.8598076 43.2137617)',4326),
   ST_GeomFromText('POINT(76.865100 43.220109)',4326))
   <= 100;