Skip to content

pointToPolygonDistance failing if polygon contains redundant (consecutive duplicate) points #2807

@mwenko

Description

@mwenko

Please provide the following when reporting an issue:

  • Description of the problem, and how it differs from what you expected.
  • Version of Turf you are using, and of any other relevant software.
  • GeoJSON data as a gist file or geojson.io (filename extension must be .geojson). Simple reproducible examples are preferrable.
  • Snippet of source code for complex examples using jsfiddle.
  • 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"
      }
    }
  ]
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions