index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 helpers_1 = require("@turf/helpers");
  7. var invariant_1 = require("@turf/invariant");
  8. var line_segment_1 = __importDefault(require("@turf/line-segment"));
  9. var meta_1 = require("@turf/meta");
  10. var geojson_rbush_1 = __importDefault(require("geojson-rbush"));
  11. /**
  12. * Takes any LineString or Polygon GeoJSON and returns the intersecting point(s).
  13. *
  14. * @name lineIntersect
  15. * @param {GeoJSON} line1 any LineString or Polygon
  16. * @param {GeoJSON} line2 any LineString or Polygon
  17. * @returns {FeatureCollection<Point>} point(s) that intersect both
  18. * @example
  19. * var line1 = turf.lineString([[126, -11], [129, -21]]);
  20. * var line2 = turf.lineString([[123, -18], [131, -14]]);
  21. * var intersects = turf.lineIntersect(line1, line2);
  22. *
  23. * //addToMap
  24. * var addToMap = [line1, line2, intersects]
  25. */
  26. function lineIntersect(line1, line2) {
  27. var unique = {};
  28. var results = [];
  29. // First, normalize geometries to features
  30. // Then, handle simple 2-vertex segments
  31. if (line1.type === "LineString") {
  32. line1 = helpers_1.feature(line1);
  33. }
  34. if (line2.type === "LineString") {
  35. line2 = helpers_1.feature(line2);
  36. }
  37. if (line1.type === "Feature" &&
  38. line2.type === "Feature" &&
  39. line1.geometry !== null &&
  40. line2.geometry !== null &&
  41. line1.geometry.type === "LineString" &&
  42. line2.geometry.type === "LineString" &&
  43. line1.geometry.coordinates.length === 2 &&
  44. line2.geometry.coordinates.length === 2) {
  45. var intersect = intersects(line1, line2);
  46. if (intersect) {
  47. results.push(intersect);
  48. }
  49. return helpers_1.featureCollection(results);
  50. }
  51. // Handles complex GeoJSON Geometries
  52. var tree = geojson_rbush_1.default();
  53. tree.load(line_segment_1.default(line2));
  54. meta_1.featureEach(line_segment_1.default(line1), function (segment) {
  55. meta_1.featureEach(tree.search(segment), function (match) {
  56. var intersect = intersects(segment, match);
  57. if (intersect) {
  58. // prevent duplicate points https://github.com/Turfjs/turf/issues/688
  59. var key = invariant_1.getCoords(intersect).join(",");
  60. if (!unique[key]) {
  61. unique[key] = true;
  62. results.push(intersect);
  63. }
  64. }
  65. });
  66. });
  67. return helpers_1.featureCollection(results);
  68. }
  69. /**
  70. * Find a point that intersects LineStrings with two coordinates each
  71. *
  72. * @private
  73. * @param {Feature<LineString>} line1 GeoJSON LineString (Must only contain 2 coordinates)
  74. * @param {Feature<LineString>} line2 GeoJSON LineString (Must only contain 2 coordinates)
  75. * @returns {Feature<Point>} intersecting GeoJSON Point
  76. */
  77. function intersects(line1, line2) {
  78. var coords1 = invariant_1.getCoords(line1);
  79. var coords2 = invariant_1.getCoords(line2);
  80. if (coords1.length !== 2) {
  81. throw new Error("<intersects> line1 must only contain 2 coordinates");
  82. }
  83. if (coords2.length !== 2) {
  84. throw new Error("<intersects> line2 must only contain 2 coordinates");
  85. }
  86. var x1 = coords1[0][0];
  87. var y1 = coords1[0][1];
  88. var x2 = coords1[1][0];
  89. var y2 = coords1[1][1];
  90. var x3 = coords2[0][0];
  91. var y3 = coords2[0][1];
  92. var x4 = coords2[1][0];
  93. var y4 = coords2[1][1];
  94. var denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
  95. var numeA = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
  96. var numeB = (x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3);
  97. if (denom === 0) {
  98. if (numeA === 0 && numeB === 0) {
  99. return null;
  100. }
  101. return null;
  102. }
  103. var uA = numeA / denom;
  104. var uB = numeB / denom;
  105. if (uA >= 0 && uA <= 1 && uB >= 0 && uB <= 1) {
  106. var x = x1 + uA * (x2 - x1);
  107. var y = y1 + uA * (y2 - y1);
  108. return helpers_1.point([x, y]);
  109. }
  110. return null;
  111. }
  112. exports.default = lineIntersect;