5

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.

7
  • I am using Angular 2 and I also want to use point-in-polygon but somehow I am not begin able to use it because it does not have a typescript definition nor an Angular 2 module. Can you tell me how you can import the JavaScript to your Angular 2 project? Commented Nov 30, 2016 at 16:34
  • If you have another question, please ask it. Commented Nov 30, 2016 at 16:35
  • why? is this one not good enough? :( Commented Nov 30, 2016 at 16:36
  • The problem is that you have put it as an answer, which it is not. Commented Nov 30, 2016 at 16:37
  • I know but i have no reputation to comment... Commented Nov 30, 2016 at 16:37

1 Answer 1

3
  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.

4
  • stackoverflow.com/questions/22521982/… - the project suggested in one of the answers there is the project I'm using. It says the test point isn't inside the polygon I'm testing against. Commented Apr 20, 2016 at 10:26
  • There are two solutions. One is labelled as the solution which finally worked. If you don't like that solution I suggest you look at one of the NPM resources and adapt the code there to your purposes. However, without knowing how you implemented the solutino I can't say any more. Commented Apr 20, 2016 at 10:31
  • Thanks, I've not implemented my own algorithm. I've tried using existing libraries - import pp from 'robust-point-in-polygon'; and import pp from 'point-in-polygon' for doing the test, they both come up saying point is outside the polygon.... Commented Apr 20, 2016 at 10:43
  • 1
    Figured out - apologies, it was the way I was feeding test points, it works now...! gosh, I should have had a coffee. Commented Apr 20, 2016 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.