1

I've been working lately with openlayers 3 and trying to get a fully offline Mapping app, I am trying to get the smallest distance between a point and a line.

I looked for a solution in the api and found nothing about it, so my question is : is there any function in openlayers 3 that returns the smallest distance between a point and a line or do I have to code it myself ?

3 Answers 3

2

Take a look at the ol3 API here

this will get you the point of the line closest to the given coordinate. Otherwise you have to calculate the perpendicular distance. There's a few ways to accomplish that, see Wikipedia for reference. But with the function I pointed out in the API you are already halfway done. You just have to figure out the second point (it's either the one before or after the point the API call returns). Imagine a line between those two points and calculate the perpendicular distance using one of the ways described in the Wikipedia article.

So, to answer the question directly: You have to code it yourself if you need the shortest distance to the line itself and not to one point within the line.

To clarify it a litte see this picture Given a line consisting of two points and a point from which you want to know the shortest distance to the line, the perpendicular distance is displayed as the pink line. It is the shortest distance to the line itself, even though there might be no actual point. The API call would return the distance towards the closest point of the line, which would, in this case, be P1 (green line).Depending on what you need, you have to choose between those two, as they are obviously not the same.

2
  • I'm working on it thanks for pointing out the getClosestPoint(), I don't get it when you say "if you need the the shortest distance to the lie itself and not to one point within the line" either way I'm going to have to code it since it's the same thing I guess ( shortest distance = distance to the closest point of the line to the point ) Commented May 24, 2016 at 12:06
  • I've edited the answer and tried to further clarify it
    – werkuh
    Commented May 24, 2016 at 12:46
2

what I needed was to get the shortest distance to the one of the vertices like the green segment in the answer of Werkuh so I'll let my code here, it works fine in case someone is looking for it:

var wgs84Sphere = new ol.Sphere(6378137);
var distance=wgs84Sphere.haversineDistance(myline.getGeometry().getClosestPoint(myPoint.getGeometry().getFirstCoordinate()),myPoint.getGeometry().getFirstCoordinate());

Credits to @werkuh for his answer

0

You can use JSTS library to calculate such distance (not the distance to the closest point, but the distance to the line segment). There is an OL3 geometry parser/writer to interact with OL3 geometries. There is a useful example in OL3 docs. For the distance calculation, here an example that I ran to ensure it does what I want:

import WKTReader from 'jsts/org/locationtech/jts/io/WKTReader'
var reader = new WKTReader()
var a=reader.read('LINESTRING(-20 20,20 20))')
var b=reader.read('POINT (0 0)')
import DistanceOp from 'jsts/org/locationtech/jts/operation/distance/DistanceOp'
console.log(DistanceOp.distance(a, b))

Note that JSTS does not deal with projections, so ensure your geometries are in suitable projection to get the distance you want.

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.