index.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 bbox_1 = __importDefault(require("@turf/bbox"));
  7. var boolean_point_in_polygon_1 = __importDefault(require("@turf/boolean-point-in-polygon"));
  8. var boolean_point_on_line_1 = __importDefault(require("@turf/boolean-point-on-line"));
  9. var invariant_1 = require("@turf/invariant");
  10. /**
  11. * Boolean-contains returns True if the second geometry is completely contained by the first geometry.
  12. * The interiors of both geometries must intersect and, the interior and boundary of the secondary (geometry b)
  13. * must not intersect the exterior of the primary (geometry a).
  14. * Boolean-contains returns the exact opposite result of the `@turf/boolean-within`.
  15. *
  16. * @name booleanContains
  17. * @param {Geometry|Feature<any>} feature1 GeoJSON Feature or Geometry
  18. * @param {Geometry|Feature<any>} feature2 GeoJSON Feature or Geometry
  19. * @returns {boolean} true/false
  20. * @example
  21. * var line = turf.lineString([[1, 1], [1, 2], [1, 3], [1, 4]]);
  22. * var point = turf.point([1, 2]);
  23. *
  24. * turf.booleanContains(line, point);
  25. * //=true
  26. */
  27. function booleanContains(feature1, feature2) {
  28. var geom1 = invariant_1.getGeom(feature1);
  29. var geom2 = invariant_1.getGeom(feature2);
  30. var type1 = geom1.type;
  31. var type2 = geom2.type;
  32. var coords1 = geom1.coordinates;
  33. var coords2 = geom2.coordinates;
  34. switch (type1) {
  35. case "Point":
  36. switch (type2) {
  37. case "Point":
  38. return compareCoords(coords1, coords2);
  39. default:
  40. throw new Error("feature2 " + type2 + " geometry not supported");
  41. }
  42. case "MultiPoint":
  43. switch (type2) {
  44. case "Point":
  45. return isPointInMultiPoint(geom1, geom2);
  46. case "MultiPoint":
  47. return isMultiPointInMultiPoint(geom1, geom2);
  48. default:
  49. throw new Error("feature2 " + type2 + " geometry not supported");
  50. }
  51. case "LineString":
  52. switch (type2) {
  53. case "Point":
  54. return boolean_point_on_line_1.default(geom2, geom1, { ignoreEndVertices: true });
  55. case "LineString":
  56. return isLineOnLine(geom1, geom2);
  57. case "MultiPoint":
  58. return isMultiPointOnLine(geom1, geom2);
  59. default:
  60. throw new Error("feature2 " + type2 + " geometry not supported");
  61. }
  62. case "Polygon":
  63. switch (type2) {
  64. case "Point":
  65. return boolean_point_in_polygon_1.default(geom2, geom1, { ignoreBoundary: true });
  66. case "LineString":
  67. return isLineInPoly(geom1, geom2);
  68. case "Polygon":
  69. return isPolyInPoly(geom1, geom2);
  70. case "MultiPoint":
  71. return isMultiPointInPoly(geom1, geom2);
  72. default:
  73. throw new Error("feature2 " + type2 + " geometry not supported");
  74. }
  75. default:
  76. throw new Error("feature1 " + type1 + " geometry not supported");
  77. }
  78. }
  79. exports.default = booleanContains;
  80. function isPointInMultiPoint(multiPoint, pt) {
  81. var i;
  82. var output = false;
  83. for (i = 0; i < multiPoint.coordinates.length; i++) {
  84. if (compareCoords(multiPoint.coordinates[i], pt.coordinates)) {
  85. output = true;
  86. break;
  87. }
  88. }
  89. return output;
  90. }
  91. exports.isPointInMultiPoint = isPointInMultiPoint;
  92. function isMultiPointInMultiPoint(multiPoint1, multiPoint2) {
  93. for (var _i = 0, _a = multiPoint2.coordinates; _i < _a.length; _i++) {
  94. var coord2 = _a[_i];
  95. var matchFound = false;
  96. for (var _b = 0, _c = multiPoint1.coordinates; _b < _c.length; _b++) {
  97. var coord1 = _c[_b];
  98. if (compareCoords(coord2, coord1)) {
  99. matchFound = true;
  100. break;
  101. }
  102. }
  103. if (!matchFound) {
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. exports.isMultiPointInMultiPoint = isMultiPointInMultiPoint;
  110. function isMultiPointOnLine(lineString, multiPoint) {
  111. var haveFoundInteriorPoint = false;
  112. for (var _i = 0, _a = multiPoint.coordinates; _i < _a.length; _i++) {
  113. var coord = _a[_i];
  114. if (boolean_point_on_line_1.default(coord, lineString, { ignoreEndVertices: true })) {
  115. haveFoundInteriorPoint = true;
  116. }
  117. if (!boolean_point_on_line_1.default(coord, lineString)) {
  118. return false;
  119. }
  120. }
  121. if (haveFoundInteriorPoint) {
  122. return true;
  123. }
  124. return false;
  125. }
  126. exports.isMultiPointOnLine = isMultiPointOnLine;
  127. function isMultiPointInPoly(polygon, multiPoint) {
  128. for (var _i = 0, _a = multiPoint.coordinates; _i < _a.length; _i++) {
  129. var coord = _a[_i];
  130. if (!boolean_point_in_polygon_1.default(coord, polygon, { ignoreBoundary: true })) {
  131. return false;
  132. }
  133. }
  134. return true;
  135. }
  136. exports.isMultiPointInPoly = isMultiPointInPoly;
  137. function isLineOnLine(lineString1, lineString2) {
  138. var haveFoundInteriorPoint = false;
  139. for (var _i = 0, _a = lineString2.coordinates; _i < _a.length; _i++) {
  140. var coords = _a[_i];
  141. if (boolean_point_on_line_1.default({ type: "Point", coordinates: coords }, lineString1, {
  142. ignoreEndVertices: true,
  143. })) {
  144. haveFoundInteriorPoint = true;
  145. }
  146. if (!boolean_point_on_line_1.default({ type: "Point", coordinates: coords }, lineString1, {
  147. ignoreEndVertices: false,
  148. })) {
  149. return false;
  150. }
  151. }
  152. return haveFoundInteriorPoint;
  153. }
  154. exports.isLineOnLine = isLineOnLine;
  155. function isLineInPoly(polygon, linestring) {
  156. var output = false;
  157. var i = 0;
  158. var polyBbox = bbox_1.default(polygon);
  159. var lineBbox = bbox_1.default(linestring);
  160. if (!doBBoxOverlap(polyBbox, lineBbox)) {
  161. return false;
  162. }
  163. for (i; i < linestring.coordinates.length - 1; i++) {
  164. var midPoint = getMidpoint(linestring.coordinates[i], linestring.coordinates[i + 1]);
  165. if (boolean_point_in_polygon_1.default({ type: "Point", coordinates: midPoint }, polygon, {
  166. ignoreBoundary: true,
  167. })) {
  168. output = true;
  169. break;
  170. }
  171. }
  172. return output;
  173. }
  174. exports.isLineInPoly = isLineInPoly;
  175. /**
  176. * Is Polygon2 in Polygon1
  177. * Only takes into account outer rings
  178. *
  179. * @private
  180. * @param {Geometry|Feature<Polygon>} feature1 Polygon1
  181. * @param {Geometry|Feature<Polygon>} feature2 Polygon2
  182. * @returns {boolean} true/false
  183. */
  184. function isPolyInPoly(feature1, feature2) {
  185. // Handle Nulls
  186. if (feature1.type === "Feature" && feature1.geometry === null) {
  187. return false;
  188. }
  189. if (feature2.type === "Feature" && feature2.geometry === null) {
  190. return false;
  191. }
  192. var poly1Bbox = bbox_1.default(feature1);
  193. var poly2Bbox = bbox_1.default(feature2);
  194. if (!doBBoxOverlap(poly1Bbox, poly2Bbox)) {
  195. return false;
  196. }
  197. var coords = invariant_1.getGeom(feature2).coordinates;
  198. for (var _i = 0, coords_1 = coords; _i < coords_1.length; _i++) {
  199. var ring = coords_1[_i];
  200. for (var _a = 0, ring_1 = ring; _a < ring_1.length; _a++) {
  201. var coord = ring_1[_a];
  202. if (!boolean_point_in_polygon_1.default(coord, feature1)) {
  203. return false;
  204. }
  205. }
  206. }
  207. return true;
  208. }
  209. exports.isPolyInPoly = isPolyInPoly;
  210. function doBBoxOverlap(bbox1, bbox2) {
  211. if (bbox1[0] > bbox2[0]) {
  212. return false;
  213. }
  214. if (bbox1[2] < bbox2[2]) {
  215. return false;
  216. }
  217. if (bbox1[1] > bbox2[1]) {
  218. return false;
  219. }
  220. if (bbox1[3] < bbox2[3]) {
  221. return false;
  222. }
  223. return true;
  224. }
  225. exports.doBBoxOverlap = doBBoxOverlap;
  226. /**
  227. * compareCoords
  228. *
  229. * @private
  230. * @param {Position} pair1 point [x,y]
  231. * @param {Position} pair2 point [x,y]
  232. * @returns {boolean} true/false if coord pairs match
  233. */
  234. function compareCoords(pair1, pair2) {
  235. return pair1[0] === pair2[0] && pair1[1] === pair2[1];
  236. }
  237. exports.compareCoords = compareCoords;
  238. function getMidpoint(pair1, pair2) {
  239. return [(pair1[0] + pair2[0]) / 2, (pair1[1] + pair2[1]) / 2];
  240. }
  241. exports.getMidpoint = getMidpoint;