index.cjs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.ts
  2. var _bbox = require('@turf/bbox');
  3. var _booleanpointonline = require('@turf/boolean-point-on-line');
  4. var _booleanpointinpolygon = require('@turf/boolean-point-in-polygon');
  5. var _invariant = require('@turf/invariant');
  6. function booleanWithin(feature1, feature2) {
  7. var geom1 = _invariant.getGeom.call(void 0, feature1);
  8. var geom2 = _invariant.getGeom.call(void 0, feature2);
  9. var type1 = geom1.type;
  10. var type2 = geom2.type;
  11. switch (type1) {
  12. case "Point":
  13. switch (type2) {
  14. case "MultiPoint":
  15. return isPointInMultiPoint(geom1, geom2);
  16. case "LineString":
  17. return _booleanpointonline.booleanPointOnLine.call(void 0, geom1, geom2, { ignoreEndVertices: true });
  18. case "Polygon":
  19. case "MultiPolygon":
  20. return _booleanpointinpolygon.booleanPointInPolygon.call(void 0, geom1, geom2, { ignoreBoundary: true });
  21. default:
  22. throw new Error("feature2 " + type2 + " geometry not supported");
  23. }
  24. case "MultiPoint":
  25. switch (type2) {
  26. case "MultiPoint":
  27. return isMultiPointInMultiPoint(geom1, geom2);
  28. case "LineString":
  29. return isMultiPointOnLine(geom1, geom2);
  30. case "Polygon":
  31. case "MultiPolygon":
  32. return isMultiPointInPoly(geom1, geom2);
  33. default:
  34. throw new Error("feature2 " + type2 + " geometry not supported");
  35. }
  36. case "LineString":
  37. switch (type2) {
  38. case "LineString":
  39. return isLineOnLine(geom1, geom2);
  40. case "Polygon":
  41. case "MultiPolygon":
  42. return isLineInPoly(geom1, geom2);
  43. default:
  44. throw new Error("feature2 " + type2 + " geometry not supported");
  45. }
  46. case "Polygon":
  47. switch (type2) {
  48. case "Polygon":
  49. case "MultiPolygon":
  50. return isPolyInPoly(geom1, geom2);
  51. default:
  52. throw new Error("feature2 " + type2 + " geometry not supported");
  53. }
  54. default:
  55. throw new Error("feature1 " + type1 + " geometry not supported");
  56. }
  57. }
  58. function isPointInMultiPoint(point, multiPoint) {
  59. var i;
  60. var output = false;
  61. for (i = 0; i < multiPoint.coordinates.length; i++) {
  62. if (compareCoords(multiPoint.coordinates[i], point.coordinates)) {
  63. output = true;
  64. break;
  65. }
  66. }
  67. return output;
  68. }
  69. function isMultiPointInMultiPoint(multiPoint1, multiPoint2) {
  70. for (var i = 0; i < multiPoint1.coordinates.length; i++) {
  71. var anyMatch = false;
  72. for (var i2 = 0; i2 < multiPoint2.coordinates.length; i2++) {
  73. if (compareCoords(multiPoint1.coordinates[i], multiPoint2.coordinates[i2])) {
  74. anyMatch = true;
  75. }
  76. }
  77. if (!anyMatch) {
  78. return false;
  79. }
  80. }
  81. return true;
  82. }
  83. function isMultiPointOnLine(multiPoint, lineString) {
  84. var foundInsidePoint = false;
  85. for (var i = 0; i < multiPoint.coordinates.length; i++) {
  86. if (!_booleanpointonline.booleanPointOnLine.call(void 0, multiPoint.coordinates[i], lineString)) {
  87. return false;
  88. }
  89. if (!foundInsidePoint) {
  90. foundInsidePoint = _booleanpointonline.booleanPointOnLine.call(void 0,
  91. multiPoint.coordinates[i],
  92. lineString,
  93. { ignoreEndVertices: true }
  94. );
  95. }
  96. }
  97. return foundInsidePoint;
  98. }
  99. function isMultiPointInPoly(multiPoint, polygon) {
  100. var output = true;
  101. var oneInside = false;
  102. var isInside = false;
  103. for (var i = 0; i < multiPoint.coordinates.length; i++) {
  104. isInside = _booleanpointinpolygon.booleanPointInPolygon.call(void 0, multiPoint.coordinates[i], polygon);
  105. if (!isInside) {
  106. output = false;
  107. break;
  108. }
  109. if (!oneInside) {
  110. isInside = _booleanpointinpolygon.booleanPointInPolygon.call(void 0, multiPoint.coordinates[i], polygon, {
  111. ignoreBoundary: true
  112. });
  113. }
  114. }
  115. return output && isInside;
  116. }
  117. function isLineOnLine(lineString1, lineString2) {
  118. for (var i = 0; i < lineString1.coordinates.length; i++) {
  119. if (!_booleanpointonline.booleanPointOnLine.call(void 0, lineString1.coordinates[i], lineString2)) {
  120. return false;
  121. }
  122. }
  123. return true;
  124. }
  125. function isLineInPoly(linestring, polygon) {
  126. var polyBbox = _bbox.bbox.call(void 0, polygon);
  127. var lineBbox = _bbox.bbox.call(void 0, linestring);
  128. if (!doBBoxOverlap(polyBbox, lineBbox)) {
  129. return false;
  130. }
  131. var foundInsidePoint = false;
  132. for (var i = 0; i < linestring.coordinates.length; i++) {
  133. if (!_booleanpointinpolygon.booleanPointInPolygon.call(void 0, linestring.coordinates[i], polygon)) {
  134. return false;
  135. }
  136. if (!foundInsidePoint) {
  137. foundInsidePoint = _booleanpointinpolygon.booleanPointInPolygon.call(void 0,
  138. linestring.coordinates[i],
  139. polygon,
  140. { ignoreBoundary: true }
  141. );
  142. }
  143. if (!foundInsidePoint && i < linestring.coordinates.length - 1) {
  144. var midpoint = getMidpoint(
  145. linestring.coordinates[i],
  146. linestring.coordinates[i + 1]
  147. );
  148. foundInsidePoint = _booleanpointinpolygon.booleanPointInPolygon.call(void 0, midpoint, polygon, {
  149. ignoreBoundary: true
  150. });
  151. }
  152. }
  153. return foundInsidePoint;
  154. }
  155. function isPolyInPoly(geometry1, geometry2) {
  156. var poly1Bbox = _bbox.bbox.call(void 0, geometry1);
  157. var poly2Bbox = _bbox.bbox.call(void 0, geometry2);
  158. if (!doBBoxOverlap(poly2Bbox, poly1Bbox)) {
  159. return false;
  160. }
  161. for (var i = 0; i < geometry1.coordinates[0].length; i++) {
  162. if (!_booleanpointinpolygon.booleanPointInPolygon.call(void 0, geometry1.coordinates[0][i], geometry2)) {
  163. return false;
  164. }
  165. }
  166. return true;
  167. }
  168. function doBBoxOverlap(bbox1, bbox2) {
  169. if (bbox1[0] > bbox2[0]) return false;
  170. if (bbox1[2] < bbox2[2]) return false;
  171. if (bbox1[1] > bbox2[1]) return false;
  172. if (bbox1[3] < bbox2[3]) return false;
  173. return true;
  174. }
  175. function compareCoords(pair1, pair2) {
  176. return pair1[0] === pair2[0] && pair1[1] === pair2[1];
  177. }
  178. function getMidpoint(pair1, pair2) {
  179. return [(pair1[0] + pair2[0]) / 2, (pair1[1] + pair2[1]) / 2];
  180. }
  181. var turf_boolean_within_default = booleanWithin;
  182. exports.booleanWithin = booleanWithin; exports.default = turf_boolean_within_default;
  183. //# sourceMappingURL=index.cjs.map