index.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. 'use strict';
  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 _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  7. var length__default = /*#__PURE__*/_interopDefaultLegacy(length);
  8. var lineSliceAlong__default = /*#__PURE__*/_interopDefaultLegacy(lineSliceAlong);
  9. /**
  10. * Divides a {@link LineString} into chunks of a specified length.
  11. * If the line is shorter than the segment length then the original line is returned.
  12. *
  13. * @name lineChunk
  14. * @param {FeatureCollection|Geometry|Feature<LineString|MultiLineString>} geojson the lines to split
  15. * @param {number} segmentLength how long to make each segment
  16. * @param {Object} [options={}] Optional parameters
  17. * @param {string} [options.units='kilometers'] units can be degrees, radians, miles, or kilometers
  18. * @param {boolean} [options.reverse=false] reverses coordinates to start the first chunked segment at the end
  19. * @returns {FeatureCollection<LineString>} collection of line segments
  20. * @example
  21. * var line = turf.lineString([[-95, 40], [-93, 45], [-85, 50]]);
  22. *
  23. * var chunk = turf.lineChunk(line, 15, {units: 'miles'});
  24. *
  25. * //addToMap
  26. * var addToMap = [chunk];
  27. */
  28. function lineChunk(geojson, segmentLength, options) {
  29. // Optional parameters
  30. options = options || {};
  31. if (!helpers.isObject(options)) throw new Error("options is invalid");
  32. var units = options.units;
  33. var reverse = options.reverse;
  34. // Validation
  35. if (!geojson) throw new Error("geojson is required");
  36. if (segmentLength <= 0)
  37. throw new Error("segmentLength must be greater than 0");
  38. // Container
  39. var results = [];
  40. // Flatten each feature to simple LineString
  41. meta.flattenEach(geojson, function (feature) {
  42. // reverses coordinates to start the first chunked segment at the end
  43. if (reverse)
  44. feature.geometry.coordinates = feature.geometry.coordinates.reverse();
  45. sliceLineSegments(feature, segmentLength, units, function (segment) {
  46. results.push(segment);
  47. });
  48. });
  49. return helpers.featureCollection(results);
  50. }
  51. /**
  52. * Slice Line Segments
  53. *
  54. * @private
  55. * @param {Feature<LineString>} line GeoJSON LineString
  56. * @param {number} segmentLength how long to make each segment
  57. * @param {string}[units='kilometers'] units can be degrees, radians, miles, or kilometers
  58. * @param {Function} callback iterate over sliced line segments
  59. * @returns {void}
  60. */
  61. function sliceLineSegments(line, segmentLength, units, callback) {
  62. var lineLength = length__default['default'](line, { units: units });
  63. // If the line is shorter than the segment length then the orginal line is returned.
  64. if (lineLength <= segmentLength) return callback(line);
  65. var numberOfSegments = lineLength / segmentLength;
  66. // If numberOfSegments is integer, no need to plus 1
  67. if (!Number.isInteger(numberOfSegments)) {
  68. numberOfSegments = Math.floor(numberOfSegments) + 1;
  69. }
  70. for (var i = 0; i < numberOfSegments; i++) {
  71. var outline = lineSliceAlong__default['default'](
  72. line,
  73. segmentLength * i,
  74. segmentLength * (i + 1),
  75. { units: units }
  76. );
  77. callback(outline, i);
  78. }
  79. }
  80. module.exports = lineChunk;
  81. module.exports.default = lineChunk;