index.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. var boolean_point_in_polygon_1 = __importDefault(require("@turf/boolean-point-in-polygon"));
  7. var line_intersect_1 = __importDefault(require("@turf/line-intersect"));
  8. var meta_1 = require("@turf/meta");
  9. var polygon_to_line_1 = __importDefault(require("@turf/polygon-to-line"));
  10. /**
  11. * Boolean-disjoint returns (TRUE) if the intersection of the two geometries is an empty set.
  12. *
  13. * @name booleanDisjoint
  14. * @param {Geometry|Feature<any>} feature1 GeoJSON Feature or Geometry
  15. * @param {Geometry|Feature<any>} feature2 GeoJSON Feature or Geometry
  16. * @returns {boolean} true/false
  17. * @example
  18. * var point = turf.point([2, 2]);
  19. * var line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
  20. *
  21. * turf.booleanDisjoint(line, point);
  22. * //=true
  23. */
  24. function booleanDisjoint(feature1, feature2) {
  25. var bool = true;
  26. meta_1.flattenEach(feature1, function (flatten1) {
  27. meta_1.flattenEach(feature2, function (flatten2) {
  28. if (bool === false) {
  29. return false;
  30. }
  31. bool = disjoint(flatten1.geometry, flatten2.geometry);
  32. });
  33. });
  34. return bool;
  35. }
  36. /**
  37. * Disjoint operation for simple Geometries (Point/LineString/Polygon)
  38. *
  39. * @private
  40. * @param {Geometry<any>} geom1 GeoJSON Geometry
  41. * @param {Geometry<any>} geom2 GeoJSON Geometry
  42. * @returns {boolean} true/false
  43. */
  44. function disjoint(geom1, geom2) {
  45. switch (geom1.type) {
  46. case "Point":
  47. switch (geom2.type) {
  48. case "Point":
  49. return !compareCoords(geom1.coordinates, geom2.coordinates);
  50. case "LineString":
  51. return !isPointOnLine(geom2, geom1);
  52. case "Polygon":
  53. return !boolean_point_in_polygon_1.default(geom1, geom2);
  54. }
  55. /* istanbul ignore next */
  56. break;
  57. case "LineString":
  58. switch (geom2.type) {
  59. case "Point":
  60. return !isPointOnLine(geom1, geom2);
  61. case "LineString":
  62. return !isLineOnLine(geom1, geom2);
  63. case "Polygon":
  64. return !isLineInPoly(geom2, geom1);
  65. }
  66. /* istanbul ignore next */
  67. break;
  68. case "Polygon":
  69. switch (geom2.type) {
  70. case "Point":
  71. return !boolean_point_in_polygon_1.default(geom2, geom1);
  72. case "LineString":
  73. return !isLineInPoly(geom1, geom2);
  74. case "Polygon":
  75. return !isPolyInPoly(geom2, geom1);
  76. }
  77. }
  78. return false;
  79. }
  80. // http://stackoverflow.com/a/11908158/1979085
  81. function isPointOnLine(lineString, pt) {
  82. for (var i = 0; i < lineString.coordinates.length - 1; i++) {
  83. if (isPointOnLineSegment(lineString.coordinates[i], lineString.coordinates[i + 1], pt.coordinates)) {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. function isLineOnLine(lineString1, lineString2) {
  90. var doLinesIntersect = line_intersect_1.default(lineString1, lineString2);
  91. if (doLinesIntersect.features.length > 0) {
  92. return true;
  93. }
  94. return false;
  95. }
  96. function isLineInPoly(polygon, lineString) {
  97. for (var _i = 0, _a = lineString.coordinates; _i < _a.length; _i++) {
  98. var coord = _a[_i];
  99. if (boolean_point_in_polygon_1.default(coord, polygon)) {
  100. return true;
  101. }
  102. }
  103. var doLinesIntersect = line_intersect_1.default(lineString, polygon_to_line_1.default(polygon));
  104. if (doLinesIntersect.features.length > 0) {
  105. return true;
  106. }
  107. return false;
  108. }
  109. /**
  110. * Is Polygon (geom1) in Polygon (geom2)
  111. * Only takes into account outer rings
  112. * See http://stackoverflow.com/a/4833823/1979085
  113. *
  114. * @private
  115. * @param {Geometry|Feature<Polygon>} feature1 Polygon1
  116. * @param {Geometry|Feature<Polygon>} feature2 Polygon2
  117. * @returns {boolean} true/false
  118. */
  119. function isPolyInPoly(feature1, feature2) {
  120. for (var _i = 0, _a = feature1.coordinates[0]; _i < _a.length; _i++) {
  121. var coord1 = _a[_i];
  122. if (boolean_point_in_polygon_1.default(coord1, feature2)) {
  123. return true;
  124. }
  125. }
  126. for (var _b = 0, _c = feature2.coordinates[0]; _b < _c.length; _b++) {
  127. var coord2 = _c[_b];
  128. if (boolean_point_in_polygon_1.default(coord2, feature1)) {
  129. return true;
  130. }
  131. }
  132. var doLinesIntersect = line_intersect_1.default(polygon_to_line_1.default(feature1), polygon_to_line_1.default(feature2));
  133. if (doLinesIntersect.features.length > 0) {
  134. return true;
  135. }
  136. return false;
  137. }
  138. function isPointOnLineSegment(lineSegmentStart, lineSegmentEnd, pt) {
  139. var dxc = pt[0] - lineSegmentStart[0];
  140. var dyc = pt[1] - lineSegmentStart[1];
  141. var dxl = lineSegmentEnd[0] - lineSegmentStart[0];
  142. var dyl = lineSegmentEnd[1] - lineSegmentStart[1];
  143. var cross = dxc * dyl - dyc * dxl;
  144. if (cross !== 0) {
  145. return false;
  146. }
  147. if (Math.abs(dxl) >= Math.abs(dyl)) {
  148. if (dxl > 0) {
  149. return lineSegmentStart[0] <= pt[0] && pt[0] <= lineSegmentEnd[0];
  150. }
  151. else {
  152. return lineSegmentEnd[0] <= pt[0] && pt[0] <= lineSegmentStart[0];
  153. }
  154. }
  155. else if (dyl > 0) {
  156. return lineSegmentStart[1] <= pt[1] && pt[1] <= lineSegmentEnd[1];
  157. }
  158. else {
  159. return lineSegmentEnd[1] <= pt[1] && pt[1] <= lineSegmentStart[1];
  160. }
  161. }
  162. /**
  163. * compareCoords
  164. *
  165. * @private
  166. * @param {Position} pair1 point [x,y]
  167. * @param {Position} pair2 point [x,y]
  168. * @returns {boolean} true/false if coord pairs match
  169. */
  170. function compareCoords(pair1, pair2) {
  171. return pair1[0] === pair2[0] && pair1[1] === pair2[1];
  172. }
  173. exports.default = booleanDisjoint;