Search code examples
javascriptturfjs

Identify point inside a polygon (inclusive of point lying on boundaries and edges)


I am using turf.js to find a point inside a polygon and i found the problem persisting for several use cases one of such use case is as below below mentioned use case (i.e. for point (X=32.8,Y=40). I have also plotted the image for reference for which turf.booleanPointInPolygon is showing false. Image of Graph plot

var turf = require("@turf/turf");

var pt = turf.point([32.8,40]);
var poly = turf.polygon([[

 [ 7.2  ,           160.0],
[38.3,              30.0],
[65.1   ,           30.0],
[62.3   ,           96.0],
[18.4   ,           325.0],
[7.6    ,           380.0],
[7.5    ,           307.0],
[7.4    ,           234.0],
[7.2    ,           160.0]

]]);

var bool = turf.booleanPointInPolygon(pt, poly, {ignoreBoundary: false});
//bool = false, even though the point is inside the polygon

failed to recognize a point within the polygon, this is a glitch from turf.js. How to report to turf.js. Anyone please help in fixing this.


Solution

  • Your point is not inside the polygon. There is no bug in turf.js. You probably been tricked because your plotted graph is completely wrong.

    Here it is how your polygon should look likes on a graph. You see that your point (J) is clearly outside:

    enter image description here