index.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // index.ts
  2. import { bbox as calcBbox } from "@turf/bbox";
  3. import { booleanPointInPolygon } from "@turf/boolean-point-in-polygon";
  4. import { booleanPointOnLine as isPointOnLine } from "@turf/boolean-point-on-line";
  5. import { getGeom } from "@turf/invariant";
  6. function booleanContains(feature1, feature2) {
  7. const geom1 = getGeom(feature1);
  8. const geom2 = getGeom(feature2);
  9. const type1 = geom1.type;
  10. const type2 = geom2.type;
  11. const coords1 = geom1.coordinates;
  12. const coords2 = geom2.coordinates;
  13. switch (type1) {
  14. case "Point":
  15. switch (type2) {
  16. case "Point":
  17. return compareCoords(coords1, coords2);
  18. default:
  19. throw new Error("feature2 " + type2 + " geometry not supported");
  20. }
  21. case "MultiPoint":
  22. switch (type2) {
  23. case "Point":
  24. return isPointInMultiPoint(geom1, geom2);
  25. case "MultiPoint":
  26. return isMultiPointInMultiPoint(geom1, geom2);
  27. default:
  28. throw new Error("feature2 " + type2 + " geometry not supported");
  29. }
  30. case "LineString":
  31. switch (type2) {
  32. case "Point":
  33. return isPointOnLine(geom2, geom1, { ignoreEndVertices: true });
  34. case "LineString":
  35. return isLineOnLine(geom1, geom2);
  36. case "MultiPoint":
  37. return isMultiPointOnLine(geom1, geom2);
  38. default:
  39. throw new Error("feature2 " + type2 + " geometry not supported");
  40. }
  41. case "Polygon":
  42. switch (type2) {
  43. case "Point":
  44. return booleanPointInPolygon(geom2, geom1, { ignoreBoundary: true });
  45. case "LineString":
  46. return isLineInPoly(geom1, geom2);
  47. case "Polygon":
  48. return isPolyInPoly(geom1, geom2);
  49. case "MultiPoint":
  50. return isMultiPointInPoly(geom1, geom2);
  51. default:
  52. throw new Error("feature2 " + type2 + " geometry not supported");
  53. }
  54. case "MultiPolygon":
  55. switch (type2) {
  56. case "Polygon":
  57. return isPolygonInMultiPolygon(geom1, geom2);
  58. default:
  59. throw new Error("feature2 " + type2 + " geometry not supported");
  60. }
  61. default:
  62. throw new Error("feature1 " + type1 + " geometry not supported");
  63. }
  64. }
  65. function isPolygonInMultiPolygon(multiPolygon, polygon) {
  66. return multiPolygon.coordinates.some(
  67. (coords) => isPolyInPoly({ type: "Polygon", coordinates: coords }, polygon)
  68. );
  69. }
  70. function isPointInMultiPoint(multiPoint, pt) {
  71. let i;
  72. let output = false;
  73. for (i = 0; i < multiPoint.coordinates.length; i++) {
  74. if (compareCoords(multiPoint.coordinates[i], pt.coordinates)) {
  75. output = true;
  76. break;
  77. }
  78. }
  79. return output;
  80. }
  81. function isMultiPointInMultiPoint(multiPoint1, multiPoint2) {
  82. for (const coord2 of multiPoint2.coordinates) {
  83. let matchFound = false;
  84. for (const coord1 of multiPoint1.coordinates) {
  85. if (compareCoords(coord2, coord1)) {
  86. matchFound = true;
  87. break;
  88. }
  89. }
  90. if (!matchFound) {
  91. return false;
  92. }
  93. }
  94. return true;
  95. }
  96. function isMultiPointOnLine(lineString, multiPoint) {
  97. let haveFoundInteriorPoint = false;
  98. for (const coord of multiPoint.coordinates) {
  99. if (isPointOnLine(coord, lineString, { ignoreEndVertices: true })) {
  100. haveFoundInteriorPoint = true;
  101. }
  102. if (!isPointOnLine(coord, lineString)) {
  103. return false;
  104. }
  105. }
  106. if (haveFoundInteriorPoint) {
  107. return true;
  108. }
  109. return false;
  110. }
  111. function isMultiPointInPoly(polygon, multiPoint) {
  112. for (const coord of multiPoint.coordinates) {
  113. if (!booleanPointInPolygon(coord, polygon, { ignoreBoundary: true })) {
  114. return false;
  115. }
  116. }
  117. return true;
  118. }
  119. function isLineOnLine(lineString1, lineString2) {
  120. let haveFoundInteriorPoint = false;
  121. for (const coords of lineString2.coordinates) {
  122. if (isPointOnLine({ type: "Point", coordinates: coords }, lineString1, {
  123. ignoreEndVertices: true
  124. })) {
  125. haveFoundInteriorPoint = true;
  126. }
  127. if (!isPointOnLine({ type: "Point", coordinates: coords }, lineString1, {
  128. ignoreEndVertices: false
  129. })) {
  130. return false;
  131. }
  132. }
  133. return haveFoundInteriorPoint;
  134. }
  135. function isLineInPoly(polygon, linestring) {
  136. let output = false;
  137. let i = 0;
  138. const polyBbox = calcBbox(polygon);
  139. const lineBbox = calcBbox(linestring);
  140. if (!doBBoxOverlap(polyBbox, lineBbox)) {
  141. return false;
  142. }
  143. for (i; i < linestring.coordinates.length - 1; i++) {
  144. const midPoint = getMidpoint(
  145. linestring.coordinates[i],
  146. linestring.coordinates[i + 1]
  147. );
  148. if (booleanPointInPolygon({ type: "Point", coordinates: midPoint }, polygon, {
  149. ignoreBoundary: true
  150. })) {
  151. output = true;
  152. break;
  153. }
  154. }
  155. return output;
  156. }
  157. function isPolyInPoly(feature1, feature2) {
  158. if (feature1.type === "Feature" && feature1.geometry === null) {
  159. return false;
  160. }
  161. if (feature2.type === "Feature" && feature2.geometry === null) {
  162. return false;
  163. }
  164. const poly1Bbox = calcBbox(feature1);
  165. const poly2Bbox = calcBbox(feature2);
  166. if (!doBBoxOverlap(poly1Bbox, poly2Bbox)) {
  167. return false;
  168. }
  169. const coords = getGeom(feature2).coordinates;
  170. for (const ring of coords) {
  171. for (const coord of ring) {
  172. if (!booleanPointInPolygon(coord, feature1)) {
  173. return false;
  174. }
  175. }
  176. }
  177. return true;
  178. }
  179. function doBBoxOverlap(bbox1, bbox2) {
  180. if (bbox1[0] > bbox2[0]) {
  181. return false;
  182. }
  183. if (bbox1[2] < bbox2[2]) {
  184. return false;
  185. }
  186. if (bbox1[1] > bbox2[1]) {
  187. return false;
  188. }
  189. if (bbox1[3] < bbox2[3]) {
  190. return false;
  191. }
  192. return true;
  193. }
  194. function compareCoords(pair1, pair2) {
  195. return pair1[0] === pair2[0] && pair1[1] === pair2[1];
  196. }
  197. function getMidpoint(pair1, pair2) {
  198. return [(pair1[0] + pair2[0]) / 2, (pair1[1] + pair2[1]) / 2];
  199. }
  200. var turf_boolean_contains_default = booleanContains;
  201. export {
  202. booleanContains,
  203. compareCoords,
  204. turf_boolean_contains_default as default,
  205. doBBoxOverlap,
  206. getMidpoint,
  207. isLineInPoly,
  208. isLineOnLine,
  209. isMultiPointInMultiPoint,
  210. isMultiPointInPoly,
  211. isMultiPointOnLine,
  212. isPointInMultiPoint,
  213. isPolyInPoly,
  214. isPolygonInMultiPolygon
  215. };
  216. //# sourceMappingURL=index.js.map