index.cjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// index.ts
  2. var _geojsonrbush = require('@turf/geojson-rbush');
  3. var _linesegment = require('@turf/line-segment');
  4. var _nearestpointonline = require('@turf/nearest-point-on-line');
  5. var _booleanpointonline = require('@turf/boolean-point-on-line');
  6. var _invariant = require('@turf/invariant');
  7. var _meta = require('@turf/meta');
  8. var _helpers = require('@turf/helpers');
  9. var _fastdeepequal = require('fast-deep-equal'); var _fastdeepequal2 = _interopRequireDefault(_fastdeepequal);
  10. function lineOverlap(line1, line2, options = {}) {
  11. options = options || {};
  12. if (!_helpers.isObject.call(void 0, options)) throw new Error("options is invalid");
  13. var tolerance = options.tolerance || 0;
  14. var features = [];
  15. var tree = _geojsonrbush.geojsonRbush.call(void 0, );
  16. const line = _linesegment.lineSegment.call(void 0, line1);
  17. tree.load(line);
  18. var overlapSegment;
  19. let additionalSegments = [];
  20. _meta.segmentEach.call(void 0, line2, function(segment) {
  21. var doesOverlaps = false;
  22. if (!segment) {
  23. return;
  24. }
  25. _meta.featureEach.call(void 0, tree.search(segment), function(match) {
  26. if (doesOverlaps === false) {
  27. var coordsSegment = _invariant.getCoords.call(void 0, segment).sort();
  28. var coordsMatch = _invariant.getCoords.call(void 0, match).sort();
  29. if (_fastdeepequal2.default.call(void 0, coordsSegment, coordsMatch)) {
  30. doesOverlaps = true;
  31. if (overlapSegment) {
  32. overlapSegment = concatSegment(overlapSegment, segment) || overlapSegment;
  33. } else overlapSegment = segment;
  34. } else if (tolerance === 0 ? _booleanpointonline.booleanPointOnLine.call(void 0, coordsSegment[0], match) && _booleanpointonline.booleanPointOnLine.call(void 0, coordsSegment[1], match) : _nearestpointonline.nearestPointOnLine.call(void 0, match, coordsSegment[0]).properties.dist <= tolerance && _nearestpointonline.nearestPointOnLine.call(void 0, 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.booleanPointOnLine.call(void 0, coordsMatch[0], segment) && _booleanpointonline.booleanPointOnLine.call(void 0, coordsMatch[1], segment) : _nearestpointonline.nearestPointOnLine.call(void 0, segment, coordsMatch[0]).properties.dist <= tolerance && _nearestpointonline.nearestPointOnLine.call(void 0, 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 _helpers.featureCollection.call(void 0, features);
  62. }
  63. function concatSegment(line, segment) {
  64. var coords = _invariant.getCoords.call(void 0, segment);
  65. var lineCoords = _invariant.getCoords.call(void 0, line);
  66. var start = lineCoords[0];
  67. var end = lineCoords[lineCoords.length - 1];
  68. var geom = line.geometry.coordinates;
  69. if (_fastdeepequal2.default.call(void 0, coords[0], start)) geom.unshift(coords[1]);
  70. else if (_fastdeepequal2.default.call(void 0, coords[0], end)) geom.push(coords[1]);
  71. else if (_fastdeepequal2.default.call(void 0, coords[1], start)) geom.unshift(coords[0]);
  72. else if (_fastdeepequal2.default.call(void 0, coords[1], end)) geom.push(coords[0]);
  73. else return;
  74. return line;
  75. }
  76. var turf_line_overlap_default = lineOverlap;
  77. exports.default = turf_line_overlap_default; exports.lineOverlap = lineOverlap;
  78. //# sourceMappingURL=index.cjs.map