index.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // index.js
  2. import { length } from "@turf/length";
  3. import { lineSliceAlong } from "@turf/line-slice-along";
  4. import { flattenEach } from "@turf/meta";
  5. import { featureCollection, isObject } from "@turf/helpers";
  6. function lineChunk(geojson, segmentLength, options) {
  7. options = options || {};
  8. if (!isObject(options)) throw new Error("options is invalid");
  9. var units = options.units;
  10. var reverse = options.reverse;
  11. if (!geojson) throw new Error("geojson is required");
  12. if (segmentLength <= 0)
  13. throw new Error("segmentLength must be greater than 0");
  14. var results = [];
  15. flattenEach(geojson, function(feature) {
  16. if (reverse)
  17. feature.geometry.coordinates = feature.geometry.coordinates.reverse();
  18. sliceLineSegments(feature, segmentLength, units, function(segment) {
  19. results.push(segment);
  20. });
  21. });
  22. return featureCollection(results);
  23. }
  24. function sliceLineSegments(line, segmentLength, units, callback) {
  25. var lineLength = length(line, { units });
  26. if (lineLength <= segmentLength) return callback(line);
  27. var numberOfSegments = lineLength / segmentLength;
  28. if (!Number.isInteger(numberOfSegments)) {
  29. numberOfSegments = Math.floor(numberOfSegments) + 1;
  30. }
  31. for (var i = 0; i < numberOfSegments; i++) {
  32. var outline = lineSliceAlong(
  33. line,
  34. segmentLength * i,
  35. segmentLength * (i + 1),
  36. { units }
  37. );
  38. callback(outline, i);
  39. }
  40. }
  41. var turf_line_chunk_default = lineChunk;
  42. export {
  43. turf_line_chunk_default as default,
  44. lineChunk
  45. };
  46. //# sourceMappingURL=index.js.map