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

@turf/boolean-point-in-polygon fails to check for points on edges, loads wrong version of library #2767

Closed
jsnoble opened this issue Dec 12, 2024 · 2 comments

Comments

@jsnoble
Copy link

jsnoble commented Dec 12, 2024

Downloading from scratch @turf/boolean-point-in-polygon currently is not able to check for edge points. Below is a code example to replicate the issue

package.json

{
  "name": "geo-test",
  "version": "1.0.0",
  "type": "module",
  "main": "index.js",
  "license": "MIT",

  "dependencies": {
    "@turf/boolean-point-in-polygon": "~7.1.0",
    "@turf/helpers": "~7.1.0"
  }
}

code

import { point, polygon } from '@turf/helpers';
import { booleanPointInPolygon } from '@turf/boolean-point-in-polygon';

const polyCoordinates = [
    [
        [140.43, 70.43],
        [154.4, 89.3],
        [123.4, 81.3],
        [140.43, 70.43]
    ]
]

const pt = point([ 140.43, 70.43 ]);
const poly = polygon(polyCoordinates)

const bool = booleanPointInPolygon(pt, poly);
bool === false // should be true

After diving deeper into the issue, it seems to come from a library used internally that is returning a wrong value (false instead of 0). The library in question is point-in-polygon-hao and I just recently made a ticket herehttps://github.com/rowanwins/point-in-polygon-hao/issues/23.

You have listed in your package.json to set that library to "^1.1.0". I verified that that version does in fact work, however a fresh install will give you 1.2.3 which breaks.

This is due to how semver works with the ^ symbol, "^1.1.0" will give you anything between anything >= 1.1 AND < 2. If you adjust your install to use ~, it will give you >= 1.1 AND < 1.2, or just locking it down completely to an exact version. Its unfortunate that package installs usually default to ^

I can lucky fix this by setting up resolutions in my personal package.json (for others to see, im using yarn)

{
  "name": "geo-test",
  "version": "1.0.0",
  "type": "module",
  "main": "index.js",
  "license": "MIT",
  "resolutions": {
    "point-in-polygon-hao": "~1.1.0"
  },
  "dependencies": {
    "@turf/boolean-point-in-polygon": "~7.1.0",
    "@turf/helpers": "~7.1.0"
  }
}

However I still recommend to lock down the 1.1.0 or use ~ instead of the ^ as that is far to loose and you rely a bit to much on others not breaking code in minor semver updates. This exact issue has bitten me a few times over the past few months for other libraries

@xThrodx
Copy link

xThrodx commented Dec 22, 2024

This should be fixed with the latest release of point-in-polygon-hao.

I had a similar problem with a point in the middle of a polygon that was wrongly determined to be outside of the polygon.

It might be a good idea to lock the dependencies, however for this particular problem neither the tests of Turf.js nor those of point-in-polygon-hao caught this regression.

Edit: My bad, Turf would have caught that in its tests.

@smallsaucepan
Copy link
Member

Hi @jsnoble and @xThrodx thanks for your help with this one. Will mark as resolved given it's been fixed upstream

You've raised a good point though. We'll discuss it amongst the maintainers and see if it's something we should adopt 👍

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

No branches or pull requests

3 participants