Turf: pointToLineDistance works strange (calculating minimal distance from point to a list of polygons)

Created on 13 Jan 2021  路  4Comments  路  Source: Turfjs/turf

On the screenshot below you can see 3 polygons (marked as violet color)
image

Red line is a minimal distance from point "A" to one of the three polygons. Shorter one calculated by the next formula:

import nearestPointOnLine from '@turf/nearest-point-on-line'
import pointToLineDistance from '@turf/point-to-line-distance'
import polygonToLine from '@turf/polygon-to-line'
import { polygon, point, lineString } from '@turf/helpers'

const turfPoint = point([49.26776779628557, 33.19880034774542]) //this is "A" marker coordinates

let nearestPoint = null
let minDistanceKm = null

const getClosestPointToPolygon = (arrayOfPoly) => {
  arrayOfPoly.map(pol => { //3 violet polygons mentioned above
    const turfPolygon = polygon(pol)
    const distanceKm = pointToLineDistance(turfPoint, polygonToLine(turfPolygon))
    if (!!!minDistanceKm || minDistanceKm > distanceKm) {
      minDistanceKm = distanceKm
      nearestPoint = nearestPointOnLine(polygonToLine(turfPolygon), turfPoint)
     }
  })
  return nearestPoint
}

And then I draw a red line (see on screenshot) from turfPoint ("A" marker coordinates) to nearestPoint (returned from getClosestPointToPolygon())

I do not know is this an issue OR I did something wrong but obviously the distance from "A" to polygon on the bottom (3) of the screenshot is much bigger than the distance to any of the polygons (1 and 2) on the right part of screenshot.

BUT if I replaced the marker "A" a little bit right (to [49.295726442323996,33.06410014629364] for example) - everything works fine, the shorter distance calculated to (1) polygon.
image

Here is 3 polygons:

1:
[[
[49.462728907250906, 32.03540626913309],
[49.43353833374158, 31.99601467698813],
[49.414854634751144, 32.03060880303383],
[49.40926600401516, 31.998867206275463],
[49.36541223268354, 32.06928189843893],
[49.4083992948085, 32.13303532451391],
[49.462728907250906, 32.03540626913309],
]]

2:
[[
[49.24136234816716, 31.863340884447098],
[49.22462075975919, 31.833318583667282],
[49.20040909648348, 31.86852157115936],
[49.203111744950746, 31.9387686252594],
[49.23448994162803, 31.94364957511425],
[49.24136234816716, 31.863340884447098],
]]

3:
[[
[48.13998183965103, 33.568177074193954],
[48.11256172127216, 33.465478643774986],
[48.06998415445941, 33.48885141313076],
[48.05255544955855, 33.464987464249134],
[48.060517859632476, 33.38402800261974],
[47.99290166965593, 33.42316813766956],
[47.92982293287458, 33.370756432414055],
[47.88709444387379, 33.248226419091225],
[47.88424459366558, 33.3445505797863],
[47.81532609062384, 33.33251014351845],
[47.87616927900448, 33.46778869628906],
[47.95971208927601, 33.45645636320114],
[48.13998183965103, 33.568177074193954],
]]

turf ver. "^6.2.0"

Most helpful comment

@valeriik I think you are using the coordinates in the wrong way (inverted).
I tried to display the features and it looks like they are flipped compared to your images and it does seem the purple polygon is in fact the closest one to the point A:

https://turf-sandbox.netlify.app/?gist=f1b1be32c77b77ed4265268bca471c65

Screen Shot 2021-01-13 at 2 02 11 PM

All 4 comments

First screenshot (wrong)
image

Second screenshot (good)
image

Also a question - is it ok if red line here (the shorter way to polygon) is not the shortest actually. For my point of view it should be 90掳 to the nearest line. Or not?
image

@valeriik I think you are using the coordinates in the wrong way (inverted).
I tried to display the features and it looks like they are flipped compared to your images and it does seem the purple polygon is in fact the closest one to the point A:

https://turf-sandbox.netlify.app/?gist=f1b1be32c77b77ed4265268bca471c65

Screen Shot 2021-01-13 at 2 02 11 PM

Many thanks @stebogit for the really fast answer! Your karma has definitely increased by several points! You are damn right, the issue was in wrong way coordinates using!

Was this page helpful?
0 / 5 - 0 ratings