Leaflet.PointInPolygon icon indicating copy to clipboard operation
Leaflet.PointInPolygon copied to clipboard

false positive (and false negative) results for non-contiguous geometries

Open se-ti opened this issue 1 year ago • 1 comments

code to reproduce

// just 3 squares
let group = [[[0,0], [1,0], [1,1], [0,1], [0,0]],
    [[2,2], [3,2], [3,3], [2,3], [2,2]],
    [[0,4], [1,4], [1,5], [0,5], [0,4]]
    ];
    
let poly = L.polygon(group);
if (poly.contains(L.latLng([1, 2])))
    console.error('should be false');

Reason: algorithms works with flattened array, which really corresponds to geometry with another topology (see white contour on the screenshot). https://github.com/hayeswise/Leaflet.PointInPolygon/blob/7a7c7df3d5ccef184c6720c696fa54fde5a0b4e9/wise-leaflet-pip.js#L119

Given is a false positive example, but geometries with holes can produce false negative results.

That's hi from countries with lots of enclaves and exclaves. pip_er2

se-ti avatar Sep 10 '23 11:09 se-ti

here it is: false negative. Just a square with 2 holes;

let geo =
    [[[0,0], [0,7], [7,7], [7,0]],
    [[5,5], [6,5], [6,6], [5,6]],
    [[2,2], [3,2], [3,3], [2,3]]
];

let poly = L.polygon(geo );
if (!poly.contains(L.latLng([1, 1])))
    console.error('should be true');
if (poly.contains(L.latLng([2.5, 2.5])))
    console.error('should be false');
if (!poly.contains(L.latLng([4, 4])))
    console.error('should be true');

Blue contour -- expected geometry (square with 2 holes). White contour -- geometry, this lib really tests.

Moreover: "flattened" geometry depends very much on the direction (clock/counterclockwise) of the holes, their starting points, etc...

Let's be honest: the idea, you could simply flatten arrays was a FAIL.

pip_er3

se-ti avatar Sep 10 '23 12:09 se-ti