Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pointToPolygonDistance failing if polygon's last points are redundant #2807

Open
5 tasks done
mwenko opened this issue Jan 9, 2025 · 5 comments
Open
5 tasks done
Assignees

Comments

@mwenko
Copy link
Contributor

mwenko commented Jan 9, 2025

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"
      }
    }
  ]
}
@mwenko mwenko changed the title pointToPolygonDistance if polygon's last points are redundant pointToPolygonDistance failing if polygon's last points are redundant Jan 9, 2025
@twelch
Copy link
Collaborator

twelch commented Jan 11, 2025

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

@mwenko
Copy link
Contributor Author

mwenko commented Jan 11, 2025

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.

@smallsaucepan
Copy link
Member

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

@smallsaucepan smallsaucepan self-assigned this Jan 12, 2025
@Felankia
Copy link

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

@kawork1
Copy link

kawork1 commented Jan 18, 2025

Same issue here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants