2

I'm trying to determine if a point is inside a polygon using TurfJS. But I'm getting unexpected results.

In first place, I tested this simple code and works well. The intersection is true because the point pt0 is one the points in the polygon.

var pt0 = turf.point([1, 1]);

var poly0 = turf.polygon([
    [
        [-1, -1],
        [-1, 1],
        [1, 1],
        [-1, -1]
    ]
]);
var inter0 = turf.intersect(poly0,pt0); // TRUE
var inside0 = turf.inside(pt0,poly0); // FALSE

The next code is very similar, but the intersection returns undefined, while the tested point also belongs to the polygon.

var polygon1 = turf.polygon([
  [-56.14700317382812,-33.179944977396694], 
  [-56.14502906799316,-33.16895330313461],
  [-56.13266944885254,-33.174557074027],
  [-56.14700317382812,-33.179944977396694]
]);

var point1 = turf.point([-56.13266944885254,-33.174557074027]);

var inter1 = turf.intersect(polygon1,point1); // UNDEFINED
var inside1 = turf.inside(point1,polygon1); // FALSE

Here is a JSFiddle.

1 Answers1

3

You can also use this

var pt0 = turf.point([1, 1]);
var poly0 = turf.polygon([
    [
        [-1, -1],
        [-1, 1],
        [1, 1],
        [-1, -1]
    ]
]);

var inside = turf.pointsWithinPolygon(pt0, poly0);
Robert S
  • 626
  • 6
  • 13