turf icon indicating copy to clipboard operation
turf copied to clipboard

pointToPolygonDistance failing if polygon contains redundant (consecutive duplicate) points

Open mwenko opened this issue 11 months ago • 6 comments

Please provide the following when reporting an issue:

  • [x] Description of the problem, and how it differs from what you expected.
  • [x] Version of Turf you are using, and of any other relevant software.
  • [x] GeoJSON data as a gist file or geojson.io (filename extension must be .geojson). Simple reproducible examples are preferrable.
  • [x] Snippet of source code for complex examples using jsfiddle.
  • [x] Confirmation this issue hasn't already been reported, or is resolved and just hasn't been released yet.

Problem

Version: 7.2.0

pointToPolygonDistance When using the method pointToPolygonDistance I get an error that says coordinates must contain numbers. It definitely has something to do with the redundant points at the end of the polygon. (Look at the last 2 entries of the polygon in the GeoJSON section below, they are equal). If I remove one of them, the method works.

When debugging, I see that it is failing because coordinates seems to be NaN: image

Code:

// point & polygon can be used from GeoJSON below
const distance = turf.pointToPolygonDistance(point, polygon, {
                units: "meters",
            });

Stacktrace:

at point (../node_modules/@turf/helpers/index.ts:269:11)
      at ../node_modules/@turf/nearest-point-on-line/index.ts:113:25
      at ../node_modules/@turf/meta/index.js:748:11
      at geomEach (../node_modules/@turf/meta/index.js:597:13)
      at flattenEach (../node_modules/@turf/meta/index.js:739:3)
      at nearestPointOnLine (../node_modules/@turf/nearest-point-on-line/index.ts:70:3)
      at distanceToSegment (../node_modules/@turf/point-to-line-distance/index.ts:113:3)
      at ../node_modules/@turf/point-to-line-distance/index.ts:78:15
      at ../node_modules/@turf/meta/index.js:941:13
      at coordEach (../node_modules/@turf/meta/index.js:119:15)
      at ../node_modules/@turf/meta/index.js:913:7
      at ../node_modules/@turf/meta/index.js:748:11
      at geomEach (../node_modules/@turf/meta/index.js:597:13)
      at flattenEach (../node_modules/@turf/meta/index.js:739:3)
      at segmentEach (../node_modules/@turf/meta/index.js:898:3)
      at pointToLineDistance (../node_modules/@turf/point-to-line-distance/index.ts:73:3)
      at ../node_modules/@turf/point-to-polygon-distance/index.ts:80:7
      at ../node_modules/@turf/meta/index.js:748:11
      at geomEach (../node_modules/@turf/meta/index.js:597:13)
      at flattenEach (../node_modules/@turf/meta/index.js:739:3)
      at Object.pointToPolygonDistance (../node_modules/@turf/point-to-polygon-distance/index.ts:77:3)
      at pointToPolygonDistance (turf/turf.service.ts:128:35)

GeoJson:

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "coordinates": [
          [
            [
              9.717640356727232,
              48.2907894548215
            ],
            [
              9.718316273398985,
              48.289925696874604
            ],
            [
              9.719571547217955,
              48.2903397314326
            ],
            [
              9.718884901710142,
              48.29126058936186
            ],
            [
              9.717640356727232,
              48.2907894548215
            ],
            [
              9.717640356727232,
              48.2907894548215
            ]
          ]
        ],
        "type": "Polygon"
      }
    },
    {
      "type": "Feature",
      "properties": {},
      "geometry": {
        "coordinates": [
          9.73541368510476,
          48.28771763952804
        ],
        "type": "Point"
      }
    }
  ]
}

mwenko avatar Jan 09 '25 13:01 mwenko

Redundant coordinates can cause problems. Can you try using the turf cleanCoords helper function and see if that addresses the error? Unclear if there's something to be fixed here or if it's expected that you don't have redundant coordinates

twelch avatar Jan 11 '25 18:01 twelch

Unclear if there's something to be fixed here or if it's expected that you don't have redundant coordinates

The GeoJSON specification allows redundant points. So there is a point in allowing that for consistency reasons.

I think I would wish at least a better error handling that describes the issue clearly.

mwenko avatar Jan 11 '25 20:01 mwenko

I reworked this area a few months ago @mwenko and @twelch so will take a look.

smallsaucepan avatar Jan 12 '25 05:01 smallsaucepan

I also encountered the same error message, and I have already used the cleanCoord method.

Felankia avatar Jan 13 '25 02:01 Felankia

Same issue here

kawork1 avatar Jan 18 '25 22:01 kawork1

Hello, same issue

Consider the following Feature (a Polygon with consecutive duplicated vertices):

const data = {
  type: 'Feature',
  id: 0,
  geometry: {
    type: 'Polygon',
    coordinates: [
      [
        [1.610801725, 44.072336553],
        [1.609931694, 44.072693195],
        [1.613978273, 44.066500014],
        [1.613978273, 44.066500014],
        [1.613978273, 44.066500014],
        [1.614624814, 44.066066981],
        [1.610801725, 44.072336553],
      ],
    ],
  },
  properties: {},
};

When I call:

pointToPolygonDistance(turfPoint([1.5, 44.1]), data)

it throws the following error:

Error: coordinates must contain numbers

However, calling cleanCoords on the polygon geometry before passing it to pointToPolygonDistance fixes the issue.

However, I don't think this would be a reliable fix because calling cleanCoords can lead to this new error

Each LinearRing of a Polygon must have 4 or more Positions.

In the case where the Polygon has rings and one of them has duplicated coords that would lead to less than 4 coordinates after cleaning.

valentinbeggiaxa avatar Apr 29 '25 13:04 valentinbeggiaxa