3

What is the fastest algorithm to get the nearest line segment to a point (that is not on any line segment) in a GIS database?

1
  • Do you mean to find the nearest line segment to a point?
    – Zoltan
    Commented Dec 4, 2015 at 19:37

1 Answer 1

4

Using the <#> operator:

SELECT *
FROM point
CROSS JOIN line
WHERE [condition for the points];
ORDER BY point.geom <#> line.geom
LIMIT 1;

Make spatial index before running this query, to speed up.

Create index pnt_idx on point using GIST(geom);
Create index lin_idx on line using GIST(geom);`
1
  • You should use <-> to return the real distance instead of the bounding box distance. And I think that point.geom will not be available inside the subquery. Better to do something like this: select point.id from point, line order by point.geom <-> line.geom limit 1
    – pLumo
    Commented Sep 7, 2017 at 11:40

Your Answer

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

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