2

So, I have groups of polylines returned from google map API (Green, Red and Yellow), some starting point p (blue one), and a radius r. I need to find all routes whose distance from p is no greater than r (in this case red and yellow) enter image description here

I know that I can calculate it manually (find equation of each line and compare it to the equation of the circle) but it can be inefficient for many routes. So my question is:

Is there builtin function in PostGIS which will help me accomplish this task?

1 Answer 1

3

A better (much faster) solution is to use

ST_Dwithin(pointgeom, linegeom, maxdistance)

Also see manual

The ST_Distance function will calculate the exact distance whereas ST_DWithin stops when it knows it is in range, making it easier and faster to calculate.

Bonus: be careful to use the correct projection here, since lat-lon calculations will be in degrees, not meters.

5
  • I am not sure if it works for me. Following query should return true, but returns false. pastebin.com/yV8fxWKC Jun 1, 2017 at 13:07
  • The distance between these two geometries is more than 2000 meters. So the answer 'false' is corrrect. Why do you think it should return true?
    – tilt
    Jun 1, 2017 at 13:14
  • Because, according to google maps, from this point to nearest edge of the polyline is about 200 meters. Jun 1, 2017 at 13:28
  • You must have missed a decimator somewhere, because I just double checked it, and google tells me more than 2 km. How did you measure in google maps?
    – tilt
    Jun 1, 2017 at 13:47
  • It seems that I made a mistake previously, because now I double checked it and it is correct. Thanks man! Jun 1, 2017 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.