11import { length } from "@turf/length" ;
22import { lineSliceAlong } from "@turf/line-slice-along" ;
33import { flattenEach } from "@turf/meta" ;
4- import { featureCollection , isObject } from "@turf/helpers" ;
4+ import { featureCollection , isObject , Units } from "@turf/helpers" ;
5+ import {
6+ Feature ,
7+ FeatureCollection ,
8+ GeometryCollection ,
9+ LineString ,
10+ MultiLineString ,
11+ } from "geojson" ;
512
613/**
714 * Divides a {@link LineString} into chunks of a specified length.
@@ -22,30 +29,47 @@ import { featureCollection, isObject } from "@turf/helpers";
2229 * //addToMap
2330 * var addToMap = [chunk];
2431 */
25- function lineChunk ( geojson , segmentLength , options ) {
32+ function lineChunk < T extends LineString | MultiLineString > (
33+ geojson :
34+ | Feature < T >
35+ | FeatureCollection < T >
36+ | T
37+ | GeometryCollection
38+ | Feature < GeometryCollection > ,
39+ segmentLength : number ,
40+ options : {
41+ units ?: Units ;
42+ reverse ?: boolean ;
43+ } = { }
44+ ) : FeatureCollection < LineString > {
2645 // Optional parameters
27- options = options || { } ;
2846 if ( ! isObject ( options ) ) throw new Error ( "options is invalid" ) ;
29- var units = options . units ;
30- var reverse = options . reverse ;
47+ const { units = "kilometers" , reverse = false } = options ;
3148
3249 // Validation
3350 if ( ! geojson ) throw new Error ( "geojson is required" ) ;
34- if ( segmentLength <= 0 )
51+ if ( segmentLength <= 0 ) {
3552 throw new Error ( "segmentLength must be greater than 0" ) ;
53+ }
3654
3755 // Container
38- var results = [ ] ;
56+ const results : Feature < LineString > [ ] = [ ] ;
3957
4058 // Flatten each feature to simple LineString
41- flattenEach ( geojson , function ( feature ) {
59+ flattenEach ( geojson , ( feature : Feature < T > ) => {
4260 // reverses coordinates to start the first chunked segment at the end
43- if ( reverse )
61+ if ( reverse ) {
4462 feature . geometry . coordinates = feature . geometry . coordinates . reverse ( ) ;
63+ }
4564
46- sliceLineSegments ( feature , segmentLength , units , function ( segment ) {
47- results . push ( segment ) ;
48- } ) ;
65+ sliceLineSegments (
66+ feature as Feature < LineString > ,
67+ segmentLength ,
68+ units ,
69+ ( segment ) => {
70+ results . push ( segment ) ;
71+ }
72+ ) ;
4973 } ) ;
5074 return featureCollection ( results ) ;
5175}
@@ -60,11 +84,18 @@ function lineChunk(geojson, segmentLength, options) {
6084 * @param {Function } callback iterate over sliced line segments
6185 * @returns {void }
6286 */
63- function sliceLineSegments ( line , segmentLength , units , callback ) {
87+ function sliceLineSegments (
88+ line : Feature < LineString > ,
89+ segmentLength : number ,
90+ units : Units ,
91+ callback : ( feature : Feature < LineString > ) => void
92+ ) : void {
6493 var lineLength = length ( line , { units : units } ) ;
6594
6695 // If the line is shorter than the segment length then the orginal line is returned.
67- if ( lineLength <= segmentLength ) return callback ( line ) ;
96+ if ( lineLength <= segmentLength ) {
97+ return callback ( line ) ;
98+ }
6899
69100 var numberOfSegments = lineLength / segmentLength ;
70101
@@ -80,7 +111,7 @@ function sliceLineSegments(line, segmentLength, units, callback) {
80111 segmentLength * ( i + 1 ) ,
81112 { units : units }
82113 ) ;
83- callback ( outline , i ) ;
114+ callback ( outline ) ;
84115 }
85116}
86117
0 commit comments