index.cjs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.js
  2. var _geojsonrbush = require('@turf/geojson-rbush');
  3. var _square = require('@turf/square');
  4. var _bbox = require('@turf/bbox');
  5. var _truncate = require('@turf/truncate');
  6. var _linesegment = require('@turf/line-segment');
  7. var _lineintersect = require('@turf/line-intersect');
  8. var _nearestpointonline = require('@turf/nearest-point-on-line');
  9. var _invariant = require('@turf/invariant');
  10. var _meta = require('@turf/meta');
  11. var _helpers = require('@turf/helpers');
  12. function lineSplit(line, splitter) {
  13. if (!line) throw new Error("line is required");
  14. if (!splitter) throw new Error("splitter is required");
  15. var lineType = _invariant.getType.call(void 0, line);
  16. var splitterType = _invariant.getType.call(void 0, splitter);
  17. if (lineType !== "LineString") throw new Error("line must be LineString");
  18. if (splitterType === "FeatureCollection")
  19. throw new Error("splitter cannot be a FeatureCollection");
  20. if (splitterType === "GeometryCollection")
  21. throw new Error("splitter cannot be a GeometryCollection");
  22. var truncatedSplitter = _truncate.truncate.call(void 0, splitter, { precision: 7 });
  23. switch (splitterType) {
  24. case "Point":
  25. return splitLineWithPoint(line, truncatedSplitter);
  26. case "MultiPoint":
  27. return splitLineWithPoints(line, truncatedSplitter);
  28. case "LineString":
  29. case "MultiLineString":
  30. case "Polygon":
  31. case "MultiPolygon":
  32. return splitLineWithPoints(
  33. line,
  34. _lineintersect.lineIntersect.call(void 0, line, truncatedSplitter, {
  35. ignoreSelfIntersections: true
  36. })
  37. );
  38. }
  39. }
  40. function splitLineWithPoints(line, splitter) {
  41. var results = [];
  42. var tree = _geojsonrbush.geojsonRbush.call(void 0, );
  43. _meta.flattenEach.call(void 0, splitter, function(point) {
  44. results.forEach(function(feature, index) {
  45. feature.id = index;
  46. });
  47. if (!results.length) {
  48. results = splitLineWithPoint(line, point).features;
  49. results.forEach(function(feature) {
  50. if (!feature.bbox) feature.bbox = _square.square.call(void 0, _bbox.bbox.call(void 0, feature));
  51. });
  52. tree.load(_helpers.featureCollection.call(void 0, results));
  53. } else {
  54. var search = tree.search(point);
  55. if (search.features.length) {
  56. var closestLine = findClosestFeature(point, search);
  57. results = results.filter(function(feature) {
  58. return feature.id !== closestLine.id;
  59. });
  60. tree.remove(closestLine);
  61. _meta.featureEach.call(void 0, splitLineWithPoint(closestLine, point), function(line2) {
  62. results.push(line2);
  63. tree.insert(line2);
  64. });
  65. }
  66. }
  67. });
  68. return _helpers.featureCollection.call(void 0, results);
  69. }
  70. function splitLineWithPoint(line, splitter) {
  71. var results = [];
  72. var startPoint = _invariant.getCoords.call(void 0, line)[0];
  73. var endPoint = _invariant.getCoords.call(void 0, line)[line.geometry.coordinates.length - 1];
  74. if (pointsEquals(startPoint, _invariant.getCoord.call(void 0, splitter)) || pointsEquals(endPoint, _invariant.getCoord.call(void 0, splitter)))
  75. return _helpers.featureCollection.call(void 0, [line]);
  76. var tree = _geojsonrbush.geojsonRbush.call(void 0, );
  77. var segments = _linesegment.lineSegment.call(void 0, line);
  78. tree.load(segments);
  79. var search = tree.search(splitter);
  80. if (!search.features.length) return _helpers.featureCollection.call(void 0, [line]);
  81. var closestSegment = findClosestFeature(splitter, search);
  82. var initialValue = [startPoint];
  83. var lastCoords = _meta.featureReduce.call(void 0,
  84. segments,
  85. function(previous, current, index) {
  86. var currentCoords = _invariant.getCoords.call(void 0, current)[1];
  87. var splitterCoords = _invariant.getCoord.call(void 0, splitter);
  88. if (index === closestSegment.id) {
  89. previous.push(splitterCoords);
  90. results.push(_helpers.lineString.call(void 0, previous));
  91. if (pointsEquals(splitterCoords, currentCoords))
  92. return [splitterCoords];
  93. return [splitterCoords, currentCoords];
  94. } else {
  95. previous.push(currentCoords);
  96. return previous;
  97. }
  98. },
  99. initialValue
  100. );
  101. if (lastCoords.length > 1) {
  102. results.push(_helpers.lineString.call(void 0, lastCoords));
  103. }
  104. return _helpers.featureCollection.call(void 0, results);
  105. }
  106. function findClosestFeature(point, lines) {
  107. if (!lines.features.length) throw new Error("lines must contain features");
  108. if (lines.features.length === 1) return lines.features[0];
  109. var closestFeature;
  110. var closestDistance = Infinity;
  111. _meta.featureEach.call(void 0, lines, function(segment) {
  112. var pt = _nearestpointonline.nearestPointOnLine.call(void 0, segment, point);
  113. var dist = pt.properties.dist;
  114. if (dist < closestDistance) {
  115. closestFeature = segment;
  116. closestDistance = dist;
  117. }
  118. });
  119. return closestFeature;
  120. }
  121. function pointsEquals(pt1, pt2) {
  122. return pt1[0] === pt2[0] && pt1[1] === pt2[1];
  123. }
  124. var turf_line_split_default = lineSplit;
  125. exports.default = turf_line_split_default; exports.lineSplit = lineSplit;
  126. //# sourceMappingURL=index.cjs.map