Turf: Closest point to a polygon.

Created on 22 Apr 2015  路  6Comments  路  Source: Turfjs/turf

Hello,

I would like to find the closest point to a polygon with turf but I haven't see anything in the documentation about it.
I know it's possible to do this with jsts : https://github.com/bjornharrtell/jsts/blob/master/src/jsts/operation/distance/DistanceOp.js.

Do I miss something in the documentation?

Most helpful comment

This is not supported directly, but it is quite possible.

To find the distance to the closest vertex of a polygon to a point, you can do:

var vertices = turf.explode(myPoly)
var closestVertex = turf.nearest(myPt, vertices)
var distance = turf.distance(myPt, closestVertex, 'miles')

All 6 comments

This is not supported directly, but it is quite possible.

To find the distance to the closest vertex of a polygon to a point, you can do:

var vertices = turf.explode(myPoly)
var closestVertex = turf.nearest(myPt, vertices)
var distance = turf.distance(myPt, closestVertex, 'miles')

This solution works great in my use case. Thank you. Sorry for the noise.

Not sure if I should open a new issue because this one fits my problem specifically.

I think there may have been a misunderstanding, as the solution provided by @morganherlocker isn't complete. Distance to a polygon isn't distance to the nearest vertex - the distance may well be to the middle of the polygon's edge between two vertexes (and sometimes by a very large amount much closer than any vertexes).

closest-point-to-polygon

In this image, B is clearly the closest point to the polygon, but using the above algorithm we'd end up with point A.

I've been searching through many libraries on npm for one that can achieve getting the correct distance between a point and a polygon (I'd like to rank polygons by their closeness to a certain point, along with the actual distance between that point and the polygons). Unfortunately I haven't come across anything that can achieve this yet.

If it is not possible I might look into creating my own solution, and if that goes well, I'll add a pull request.

I've managed to solve my issue using the following code:

const distanceKm = pointToLineDistance(point, polygonToLineString(polygon));

Using this over an array of Polygons allows me to achieve what I want.

Still need to add support for MultiPolygons (as polygonToLineString doesn't support them) - but that's as easy as splitting the coordinate array into multiple regular Polygons and finding the shortest distance within those and choosing the shortest to represent the shape as a whole.

Since there is no direct function for Polygons, it makes use of polygonToLineString() to convert them to lines first and run the distance checks on that.

Hi @lostpebble

Can you open this as a new issue because I think we should resolve this with a proper algorithm.

Thanks
Rowan

@lostpebble first of all thanks for the tip! But using your approach I have small issue (maybe issue) described here:
https://github.com/Turfjs/turf/issues/2002
Maybe you have any ideas why?

Was this page helpful?
0 / 5 - 0 ratings