index.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // index.ts
  2. import { booleanPointInPolygon } from "@turf/boolean-point-in-polygon";
  3. import { lineIntersect } from "@turf/line-intersect";
  4. import { flattenEach } from "@turf/meta";
  5. import { polygonToLine } from "@turf/polygon-to-line";
  6. function booleanDisjoint(feature1, feature2, {
  7. ignoreSelfIntersections = true
  8. } = { ignoreSelfIntersections: true }) {
  9. let bool = true;
  10. flattenEach(feature1, (flatten1) => {
  11. flattenEach(feature2, (flatten2) => {
  12. if (bool === false) {
  13. return false;
  14. }
  15. bool = disjoint(
  16. flatten1.geometry,
  17. flatten2.geometry,
  18. ignoreSelfIntersections
  19. );
  20. });
  21. });
  22. return bool;
  23. }
  24. function disjoint(geom1, geom2, ignoreSelfIntersections) {
  25. switch (geom1.type) {
  26. case "Point":
  27. switch (geom2.type) {
  28. case "Point":
  29. return !compareCoords(geom1.coordinates, geom2.coordinates);
  30. case "LineString":
  31. return !isPointOnLine(geom2, geom1);
  32. case "Polygon":
  33. return !booleanPointInPolygon(geom1, geom2);
  34. }
  35. break;
  36. case "LineString":
  37. switch (geom2.type) {
  38. case "Point":
  39. return !isPointOnLine(geom1, geom2);
  40. case "LineString":
  41. return !isLineOnLine(geom1, geom2, ignoreSelfIntersections);
  42. case "Polygon":
  43. return !isLineInPoly(geom2, geom1, ignoreSelfIntersections);
  44. }
  45. break;
  46. case "Polygon":
  47. switch (geom2.type) {
  48. case "Point":
  49. return !booleanPointInPolygon(geom2, geom1);
  50. case "LineString":
  51. return !isLineInPoly(geom1, geom2, ignoreSelfIntersections);
  52. case "Polygon":
  53. return !isPolyInPoly(geom2, geom1, ignoreSelfIntersections);
  54. }
  55. }
  56. return false;
  57. }
  58. function isPointOnLine(lineString, pt) {
  59. for (let i = 0; i < lineString.coordinates.length - 1; i++) {
  60. if (isPointOnLineSegment(
  61. lineString.coordinates[i],
  62. lineString.coordinates[i + 1],
  63. pt.coordinates
  64. )) {
  65. return true;
  66. }
  67. }
  68. return false;
  69. }
  70. function isLineOnLine(lineString1, lineString2, ignoreSelfIntersections) {
  71. const doLinesIntersect = lineIntersect(lineString1, lineString2, {
  72. ignoreSelfIntersections
  73. });
  74. if (doLinesIntersect.features.length > 0) {
  75. return true;
  76. }
  77. return false;
  78. }
  79. function isLineInPoly(polygon, lineString, ignoreSelfIntersections) {
  80. for (const coord of lineString.coordinates) {
  81. if (booleanPointInPolygon(coord, polygon)) {
  82. return true;
  83. }
  84. }
  85. const doLinesIntersect = lineIntersect(lineString, polygonToLine(polygon), {
  86. ignoreSelfIntersections
  87. });
  88. if (doLinesIntersect.features.length > 0) {
  89. return true;
  90. }
  91. return false;
  92. }
  93. function isPolyInPoly(feature1, feature2, ignoreSelfIntersections) {
  94. for (const coord1 of feature1.coordinates[0]) {
  95. if (booleanPointInPolygon(coord1, feature2)) {
  96. return true;
  97. }
  98. }
  99. for (const coord2 of feature2.coordinates[0]) {
  100. if (booleanPointInPolygon(coord2, feature1)) {
  101. return true;
  102. }
  103. }
  104. const doLinesIntersect = lineIntersect(
  105. polygonToLine(feature1),
  106. polygonToLine(feature2),
  107. { ignoreSelfIntersections }
  108. );
  109. if (doLinesIntersect.features.length > 0) {
  110. return true;
  111. }
  112. return false;
  113. }
  114. function isPointOnLineSegment(lineSegmentStart, lineSegmentEnd, pt) {
  115. const dxc = pt[0] - lineSegmentStart[0];
  116. const dyc = pt[1] - lineSegmentStart[1];
  117. const dxl = lineSegmentEnd[0] - lineSegmentStart[0];
  118. const dyl = lineSegmentEnd[1] - lineSegmentStart[1];
  119. const cross = dxc * dyl - dyc * dxl;
  120. if (cross !== 0) {
  121. return false;
  122. }
  123. if (Math.abs(dxl) >= Math.abs(dyl)) {
  124. if (dxl > 0) {
  125. return lineSegmentStart[0] <= pt[0] && pt[0] <= lineSegmentEnd[0];
  126. } else {
  127. return lineSegmentEnd[0] <= pt[0] && pt[0] <= lineSegmentStart[0];
  128. }
  129. } else if (dyl > 0) {
  130. return lineSegmentStart[1] <= pt[1] && pt[1] <= lineSegmentEnd[1];
  131. } else {
  132. return lineSegmentEnd[1] <= pt[1] && pt[1] <= lineSegmentStart[1];
  133. }
  134. }
  135. function compareCoords(pair1, pair2) {
  136. return pair1[0] === pair2[0] && pair1[1] === pair2[1];
  137. }
  138. var turf_boolean_disjoint_default = booleanDisjoint;
  139. export {
  140. booleanDisjoint,
  141. turf_boolean_disjoint_default as default
  142. };
  143. //# sourceMappingURL=index.js.map