index.js 3.9 KB

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