[GIS] Checking if point is inside irregular polygon

javascriptpoint-in-polygonpolygon

I'm using Javascript (https://github.com/substack/point-in-polygon) to test if a point is inside polygon, however I think it's adapted from https://www.ecse.rpi.edu/~wrf/Research/Short_Notes/pnpoly.html

Following are the list of points,

let points = [
            [ 42.34624, -71.06024 ],
            [ 42.33558, -71.06616 ],
            [ 42.32632, -71.05835 ],
            [ 42.32987, -71.05428 ],
            [ 42.34732, -71.05432 ],
            [ 42.34618, -71.05973 ],
            [ 42.34624, -71.06024 ]
        ];
let testPoint = [
            [42.33288, -71.05804]

        ];`

let isInside = pp(testPoint, points);
expect(isInside).to.equal(true); // always fails

enter image description here

I'm not sure why it fails though. Is it because the algorithm doesn't cope with this "type" of polygon? Any hints please? Please point me to any alternatives to this approach as well.

Best Answer

  1. If you are using Openlayers see here.
  2. If you are using leaflet see here.
  3. For pure javascript see here or for NPM resources here or here

There are other solutions beyond these but this should be enough to be getting on with.