index.cjs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. "use strict";Object.defineProperty(exports, "__esModule", {value: true});// index.js
  2. var _length = require('@turf/length');
  3. var _lineslicealong = require('@turf/line-slice-along');
  4. var _meta = require('@turf/meta');
  5. var _helpers = require('@turf/helpers');
  6. function lineChunk(geojson, segmentLength, options) {
  7. options = options || {};
  8. if (!_helpers.isObject.call(void 0, 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. _meta.flattenEach.call(void 0, 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 _helpers.featureCollection.call(void 0, results);
  23. }
  24. function sliceLineSegments(line, segmentLength, units, callback) {
  25. var lineLength = _length.length.call(void 0, 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.lineSliceAlong.call(void 0,
  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. exports.default = turf_line_chunk_default; exports.lineChunk = lineChunk;
  43. //# sourceMappingURL=index.cjs.map