Turfjs / turf

A modular geospatial engine written in JavaScript and TypeScript

Home Page:https://turfjs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

When calculating whether a point is in a polygon area, the calculation result will be wrong

M-xiansen opened this issue · comments

(计算点是否在多边形区域里,计算结果会出错)

var polygon = turf.polygon([[
[85.2, 42.2],
[-173, 42.2],
[-173, 7.5],
[85.2, 7.5],
[85.2, 42.2]
]]);
var point = turf.point([122, 13]);
let bool = turf.booleanPointInPolygon(point, polygon);
The point should be in the area, but the result is (点应该是在区域里的,但是结果是) false

add

"@turf/turf": "^6.5.0",

import * as turf from "@turf/turf"

Hi @M-xiansen. Thanks for reporting this. It is the correct behaviour though. The polygon you defined looks like this:

Screenshot 2024-06-29 at 5 25 57 pm

Have numbered the points to make it more clear. 2' and 3' are points 2 and 3, just duplicated because the map starts repeating.

Your points join in clockwise order 1 -> 2 -> 3 -> 4 -> 1 defining a polygon with the "inside" as shown. The point you are testing is not inside.

Fixing this is a little complicated. If you were to define your polygon by joining 2' -> 1 -> 4 -> 3' -> 2' instead, that polygon would not be valid, because it crosses the meridian. Instead you need to split it into two side by side polygons as shown blue and green below:

Screenshot 2024-06-29 at 6 11 36 pm

GeoJSON of the above: https://gist.github.com/smallsaucepan/2eaae576ca67101bdca3eee5b8b11836

Hope that's of some help!