index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { getCoord, getGeom } from "@turf/invariant";
  2. // http://en.wikipedia.org/wiki/Even%E2%80%93odd_rule
  3. // modified from: https://github.com/substack/point-in-polygon/blob/master/index.js
  4. // which was modified from http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html
  5. /**
  6. * Takes a {@link Point} and a {@link Polygon} or {@link MultiPolygon} and determines if the point
  7. * resides inside the polygon. The polygon can be convex or concave. The function accounts for holes.
  8. *
  9. * @name booleanPointInPolygon
  10. * @param {Coord} point input point
  11. * @param {Feature<Polygon|MultiPolygon>} polygon input polygon or multipolygon
  12. * @param {Object} [options={}] Optional parameters
  13. * @param {boolean} [options.ignoreBoundary=false] True if polygon boundary should be ignored when determining if
  14. * the point is inside the polygon otherwise false.
  15. * @returns {boolean} `true` if the Point is inside the Polygon; `false` if the Point is not inside the Polygon
  16. * @example
  17. * var pt = turf.point([-77, 44]);
  18. * var poly = turf.polygon([[
  19. * [-81, 41],
  20. * [-81, 47],
  21. * [-72, 47],
  22. * [-72, 41],
  23. * [-81, 41]
  24. * ]]);
  25. *
  26. * turf.booleanPointInPolygon(pt, poly);
  27. * //= true
  28. */
  29. export default function booleanPointInPolygon(point, polygon, options) {
  30. if (options === void 0) { options = {}; }
  31. // validation
  32. if (!point) {
  33. throw new Error("point is required");
  34. }
  35. if (!polygon) {
  36. throw new Error("polygon is required");
  37. }
  38. var pt = getCoord(point);
  39. var geom = getGeom(polygon);
  40. var type = geom.type;
  41. var bbox = polygon.bbox;
  42. var polys = geom.coordinates;
  43. // Quick elimination if point is not inside bbox
  44. if (bbox && inBBox(pt, bbox) === false) {
  45. return false;
  46. }
  47. // normalize to multipolygon
  48. if (type === "Polygon") {
  49. polys = [polys];
  50. }
  51. var insidePoly = false;
  52. for (var i = 0; i < polys.length && !insidePoly; i++) {
  53. // check if it is in the outer ring first
  54. if (inRing(pt, polys[i][0], options.ignoreBoundary)) {
  55. var inHole = false;
  56. var k = 1;
  57. // check for the point in any of the holes
  58. while (k < polys[i].length && !inHole) {
  59. if (inRing(pt, polys[i][k], !options.ignoreBoundary)) {
  60. inHole = true;
  61. }
  62. k++;
  63. }
  64. if (!inHole) {
  65. insidePoly = true;
  66. }
  67. }
  68. }
  69. return insidePoly;
  70. }
  71. /**
  72. * inRing
  73. *
  74. * @private
  75. * @param {Array<number>} pt [x,y]
  76. * @param {Array<Array<number>>} ring [[x,y], [x,y],..]
  77. * @param {boolean} ignoreBoundary ignoreBoundary
  78. * @returns {boolean} inRing
  79. */
  80. function inRing(pt, ring, ignoreBoundary) {
  81. var isInside = false;
  82. if (ring[0][0] === ring[ring.length - 1][0] &&
  83. ring[0][1] === ring[ring.length - 1][1]) {
  84. ring = ring.slice(0, ring.length - 1);
  85. }
  86. for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) {
  87. var xi = ring[i][0];
  88. var yi = ring[i][1];
  89. var xj = ring[j][0];
  90. var yj = ring[j][1];
  91. var onBoundary = pt[1] * (xi - xj) + yi * (xj - pt[0]) + yj * (pt[0] - xi) === 0 &&
  92. (xi - pt[0]) * (xj - pt[0]) <= 0 &&
  93. (yi - pt[1]) * (yj - pt[1]) <= 0;
  94. if (onBoundary) {
  95. return !ignoreBoundary;
  96. }
  97. var intersect = yi > pt[1] !== yj > pt[1] &&
  98. pt[0] < ((xj - xi) * (pt[1] - yi)) / (yj - yi) + xi;
  99. if (intersect) {
  100. isInside = !isInside;
  101. }
  102. }
  103. return isInside;
  104. }
  105. /**
  106. * inBBox
  107. *
  108. * @private
  109. * @param {Position} pt point [x,y]
  110. * @param {BBox} bbox BBox [west, south, east, north]
  111. * @returns {boolean} true/false if point is inside BBox
  112. */
  113. function inBBox(pt, bbox) {
  114. return (bbox[0] <= pt[0] && bbox[1] <= pt[1] && bbox[2] >= pt[0] && bbox[3] >= pt[1]);
  115. }