turf icon indicating copy to clipboard operation
turf copied to clipboard

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

Open valeriik opened this issue 5 years ago • 1 comments

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 calculated by 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 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 on the right part (1 and 2) of screenshot.

BUT if I replaced the marker "A" a little bit right - 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], ]]

valeriik avatar Jan 13 '21 15:01 valeriik

Hi @valeriik. Not sure if this is still of interest to you. However, looking at the data you provided I think you have your latitudes and longitudes around the wrong way.

The point in your code [49.26776779628557, 33.19880034774542] actually shows up in Iran. If you swap the values to [33.19880034774542, 49.26776779628557] this is the point marked A in your photo.

Screenshot 2024-09-21 at 12 31 08 AM

Try swapping to [longitude, latitude] and see if that helps.

smallsaucepan avatar Sep 20 '24 14:09 smallsaucepan