index.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // index.ts
  2. import { geojsonRbush as rbush } from "@turf/geojson-rbush";
  3. import { lineSegment } from "@turf/line-segment";
  4. import { nearestPointOnLine } from "@turf/nearest-point-on-line";
  5. import { booleanPointOnLine } from "@turf/boolean-point-on-line";
  6. import { getCoords } from "@turf/invariant";
  7. import { featureEach, segmentEach } from "@turf/meta";
  8. import { featureCollection, isObject } from "@turf/helpers";
  9. import equal from "fast-deep-equal";
  10. function lineOverlap(line1, line2, options = {}) {
  11. options = options || {};
  12. if (!isObject(options)) throw new Error("options is invalid");
  13. var tolerance = options.tolerance || 0;
  14. var features = [];
  15. var tree = rbush();
  16. const line = lineSegment(line1);
  17. tree.load(line);
  18. var overlapSegment;
  19. let additionalSegments = [];
  20. segmentEach(line2, function(segment) {
  21. var doesOverlaps = false;
  22. if (!segment) {
  23. return;
  24. }
  25. featureEach(tree.search(segment), function(match) {
  26. if (doesOverlaps === false) {
  27. var coordsSegment = getCoords(segment).sort();
  28. var coordsMatch = getCoords(match).sort();
  29. if (equal(coordsSegment, coordsMatch)) {
  30. doesOverlaps = true;
  31. if (overlapSegment) {
  32. overlapSegment = concatSegment(overlapSegment, segment) || overlapSegment;
  33. } else overlapSegment = segment;
  34. } else if (tolerance === 0 ? booleanPointOnLine(coordsSegment[0], match) && booleanPointOnLine(coordsSegment[1], match) : nearestPointOnLine(match, coordsSegment[0]).properties.dist <= tolerance && nearestPointOnLine(match, coordsSegment[1]).properties.dist <= tolerance) {
  35. doesOverlaps = true;
  36. if (overlapSegment) {
  37. overlapSegment = concatSegment(overlapSegment, segment) || overlapSegment;
  38. } else overlapSegment = segment;
  39. } else if (tolerance === 0 ? booleanPointOnLine(coordsMatch[0], segment) && booleanPointOnLine(coordsMatch[1], segment) : nearestPointOnLine(segment, coordsMatch[0]).properties.dist <= tolerance && nearestPointOnLine(segment, coordsMatch[1]).properties.dist <= tolerance) {
  40. if (overlapSegment) {
  41. const combinedSegment = concatSegment(overlapSegment, match);
  42. if (combinedSegment) {
  43. overlapSegment = combinedSegment;
  44. } else {
  45. additionalSegments.push(match);
  46. }
  47. } else overlapSegment = match;
  48. }
  49. }
  50. });
  51. if (doesOverlaps === false && overlapSegment) {
  52. features.push(overlapSegment);
  53. if (additionalSegments.length) {
  54. features = features.concat(additionalSegments);
  55. additionalSegments = [];
  56. }
  57. overlapSegment = void 0;
  58. }
  59. });
  60. if (overlapSegment) features.push(overlapSegment);
  61. return featureCollection(features);
  62. }
  63. function concatSegment(line, segment) {
  64. var coords = getCoords(segment);
  65. var lineCoords = getCoords(line);
  66. var start = lineCoords[0];
  67. var end = lineCoords[lineCoords.length - 1];
  68. var geom = line.geometry.coordinates;
  69. if (equal(coords[0], start)) geom.unshift(coords[1]);
  70. else if (equal(coords[0], end)) geom.push(coords[1]);
  71. else if (equal(coords[1], start)) geom.unshift(coords[0]);
  72. else if (equal(coords[1], end)) geom.push(coords[0]);
  73. else return;
  74. return line;
  75. }
  76. var turf_line_overlap_default = lineOverlap;
  77. export {
  78. turf_line_overlap_default as default,
  79. lineOverlap
  80. };
  81. //# sourceMappingURL=index.js.map