index.js 4.8 KB

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