diff --git a/packages/turf-meta/README.md b/packages/turf-meta/README.md index f8d90b68cf..d4a5a36486 100644 --- a/packages/turf-meta/README.md +++ b/packages/turf-meta/README.md @@ -510,9 +510,9 @@ Callback for segmentEach **Parameters** - `currentSegment` **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)>** The current segment being processed. -- `featureIndex` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The index of the current element being processed in the array, starts at index 0. -- `featureSubIndex` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The subindex of the current element being processed in the - array. Starts at index 0 and increases for each iterating line segment. +- `segmentIndex` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The segmentIndex currently being processed, starts at index 0. +- `featureIndex` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The featureIndex currently being processed, starts at index 0. +- `featureSubIndex` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The featureSubIndex currently being processed, starts at index 0. - `geojson` - `callback` @@ -536,6 +536,7 @@ var polygon = turf.polygon([[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5 // Iterate over GeoJSON by 2-vertex segments turf.segmentEach(polygon, function (currentSegment, featureIndex, featureSubIndex) { //= currentSegment + //= segmentIndex //= featureIndex //= featureSubIndex }); @@ -571,10 +572,9 @@ If an initialValue is not provided: - `previousValue` **\[Any]** The accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied. - `currentSegment` **\[[Feature](http://geojson.org/geojson-spec.html#feature-objects)<[LineString](http://geojson.org/geojson-spec.html#linestring)>]** The current segment being processed. -- `currentIndex` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** The index of the current element being processed in the - array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise. -- `currentSubIndex` **\[[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)]** The subindex of the current element being processed in the - array. Starts at index 0 and increases for each iterating line segment. +- `segmentIndex` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The segmentIndex currently being processed, starts at index 0. +- `featureIndex` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The featureIndex currently being processed, starts at index 0. +- `featureSubIndex` **[number](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number)** The featureSubIndex currently being processed, starts at index 0. - `geojson` - `callback` - `initialValue` @@ -596,11 +596,12 @@ Reduce 2-vertex line segment in any GeoJSON object, similar to Array.reduce() var polygon = turf.polygon([[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5]]]); // Iterate over GeoJSON by 2-vertex segments -turf.segmentReduce(polygon, function (previousSegment, currentSegment, currentIndex, currentSubIndex) { +turf.segmentReduce(polygon, function (previousSegment, currentSegment, segmentIndex, featureIndex, featureSubIndex) { //= previousSegment //= currentSegment - //= currentIndex - //= currentSubIndex + //= segmentInex + //= featureIndex + //= featureSubIndex return currentSegment }); diff --git a/packages/turf-meta/index.js b/packages/turf-meta/index.js index 287829752f..89e7035a7e 100644 --- a/packages/turf-meta/index.js +++ b/packages/turf-meta/index.js @@ -707,9 +707,9 @@ export function flattenReduce(geojson, callback, initialValue) { * * @callback segmentEachCallback * @param {Feature} currentSegment The current segment being processed. - * @param {number} featureIndex The index of the current element being processed in the array, starts at index 0. - * @param {number} featureSubIndex The subindex of the current element being processed in the - * array. Starts at index 0 and increases for each iterating line segment. + * @param {number} segmentIndex The segmentIndex currently being processed, starts at index 0. + * @param {number} featureIndex The featureIndex currently being processed, starts at index 0. + * @param {number} featureSubIndex The featureSubIndex currently being processed, starts at index 0. * @returns {void} */ @@ -726,6 +726,7 @@ export function flattenReduce(geojson, callback, initialValue) { * // Iterate over GeoJSON by 2-vertex segments * turf.segmentEach(polygon, function (currentSegment, featureIndex, featureSubIndex) { * //= currentSegment + * //= segmentIndex * //= featureIndex * //= featureSubIndex * }); @@ -737,8 +738,9 @@ export function flattenReduce(geojson, callback, initialValue) { * }); */ export function segmentEach(geojson, callback) { - flattenEach(geojson, function (feature, featureIndex) { - var featureSubIndex = 0; + flattenEach(geojson, function (feature, featureIndex, featureSubIndex) { + var segmentIndex = 0; + // Exclude null Geometries if (!feature.geometry) return; // (Multi)Point geometries do not contain segments therefore they are ignored during this operation. @@ -748,8 +750,8 @@ export function segmentEach(geojson, callback) { // Generate 2-vertex line segments coordReduce(feature, function (previousCoords, currentCoord) { var currentSegment = lineString([previousCoords, currentCoord], feature.properties); - callback(currentSegment, featureIndex, featureSubIndex); - featureSubIndex++; + callback(currentSegment, segmentIndex, featureIndex, featureSubIndex); + segmentIndex++; return currentCoord; }); }); @@ -773,10 +775,9 @@ export function segmentEach(geojson, callback) { * @param {*} [previousValue] The accumulated value previously returned in the last invocation * of the callback, or initialValue, if supplied. * @param {Feature} [currentSegment] The current segment being processed. - * @param {number} [currentIndex] The index of the current element being processed in the - * array. Starts at index 0, if an initialValue is provided, and at index 1 otherwise. - * @param {number} [currentSubIndex] The subindex of the current element being processed in the - * array. Starts at index 0 and increases for each iterating line segment. + * @param {number} segmentIndex The segmentIndex currently being processed, starts at index 0. + * @param {number} featureIndex The featureIndex currently being processed, starts at index 0. + * @param {number} featureSubIndex The featureSubIndex currently being processed, starts at index 0. */ /** @@ -791,11 +792,12 @@ export function segmentEach(geojson, callback) { * var polygon = turf.polygon([[[-50, 5], [-40, -10], [-50, -10], [-40, 5], [-50, 5]]]); * * // Iterate over GeoJSON by 2-vertex segments - * turf.segmentReduce(polygon, function (previousSegment, currentSegment, currentIndex, currentSubIndex) { + * turf.segmentReduce(polygon, function (previousSegment, currentSegment, segmentIndex, featureIndex, featureSubIndex) { * //= previousSegment * //= currentSegment - * //= currentIndex - * //= currentSubIndex + * //= segmentInex + * //= featureIndex + * //= featureSubIndex * return currentSegment * }); * @@ -808,9 +810,11 @@ export function segmentEach(geojson, callback) { */ export function segmentReduce(geojson, callback, initialValue) { var previousValue = initialValue; - segmentEach(geojson, function (currentSegment, currentIndex, currentSubIndex) { - if (currentIndex === 0 && initialValue === undefined) previousValue = currentSegment; - else previousValue = callback(previousValue, currentSegment, currentIndex, currentSubIndex); + var started = false; + segmentEach(geojson, function (currentSegment, segmentIndex, featureIndex, featureSubIndex) { + if (started === false && initialValue === undefined) previousValue = currentSegment; + else previousValue = callback(previousValue, currentSegment, segmentIndex, featureIndex, featureSubIndex); + started = true; }); return previousValue; } diff --git a/packages/turf-meta/test.js b/packages/turf-meta/test.js index 59a9e876da..cb1b52f34a 100644 --- a/packages/turf-meta/test.js +++ b/packages/turf-meta/test.js @@ -442,44 +442,64 @@ test('segmentReduce', t => { t.end(); }); +test('segmentReduce -- no initialValue', t => { + const segments = []; + var total = 0; + meta.segmentReduce(poly.geometry, (previousValue, currentSegment) => { + segments.push(currentSegment); + total++; + }); + t.equal(segments[0].geometry.coordinates.length, 2); + t.equal(total, 2); + t.end(); +}); + const geojsonSegments = featureCollection([ - point([0, 1]), - lineString([[0, 0], [2, 2], [4, 4]]), // subIndex = 0, 1 - polygon([[[5, 5], [0, 0], [2, 2], [4, 4], [5, 5]]]), // subIndex = 0, 1, 2, 3 - point([0, 1]), - lineString([[0, 0], [2, 2], [4, 4]]) // subIndex = 0, 1 + point([0, 1]), // ignored + lineString([[0, 0], [2, 2], [4, 4]]), + polygon([[[5, 5], [0, 0], [2, 2], [4, 4], [5, 5]]]), + point([0, 1]), // ignored + multiLineString([ + [[0, 0], [2, 2], [4, 4]], + [[0, 0], [2, 2], [4, 4]] + ]) ]); test('segmentEach -- index & subIndex', t => { - const index = []; - const subIndex = []; + const segmentIndexes = []; + const featureIndexes = []; + const featureSubIndexes = []; let total = 0; - meta.segmentEach(geojsonSegments, (segment, featureIndex, featureSubIndex) => { - index.push(featureIndex); - subIndex.push(featureSubIndex); + meta.segmentEach(geojsonSegments, (segment, segmentIndex, featureIndex, featureSubIndex) => { + segmentIndexes.push(segmentIndex); + featureIndexes.push(featureIndex); + featureSubIndexes.push(featureSubIndex); total++; }); - t.equal(total, 8, 'total'); - t.deepEqual(index, [1, 1, 2, 2, 2, 2, 4, 4], 'index'); - t.deepEqual(subIndex, [0, 1, 0, 1, 2, 3, 0, 1], 'subIndex'); + t.equal(total, 10, 'total'); + t.deepEqual(segmentIndexes, [0, 1, 0, 1, 2, 3, 0, 1, 0, 1], 'segmentEach.segmentIndex'); + t.deepEqual(featureIndexes, [1, 1, 2, 2, 2, 2, 4, 4, 4, 4], 'segmentEach.featureIndex'); + t.deepEqual(featureSubIndexes, [0, 0, 0, 0, 0, 0, 0, 0, 1, 1], 'segmentEach.featureSubIndex'); t.end(); }); test('segmentReduce -- index & subIndex', t => { - const index = []; - const subIndex = []; - - const total = meta.segmentReduce(geojsonSegments, (previousValue, segment, featureIndex, featureSubIndex) => { - index.push(featureIndex); - subIndex.push(featureSubIndex); - previousValue++; - return previousValue; - }, 0); + const segmentIndexes = []; + const featureIndexes = []; + const featureSubIndexes = []; + let total = 0; - t.equal(total, 8, 'total'); - t.deepEqual(index, [1, 1, 2, 2, 2, 2, 4, 4], 'index'); - t.deepEqual(subIndex, [0, 1, 0, 1, 2, 3, 0, 1], 'subIndex'); + meta.segmentReduce(geojsonSegments, (previousValue, segment, segmentIndex, featureIndex, featureSubIndex) => { + segmentIndexes.push(segmentIndex); + featureIndexes.push(featureIndex); + featureSubIndexes.push(featureSubIndex); + total++; + }); + t.equal(total, 9, 'total'); + t.deepEqual(segmentIndexes, [1, 0, 1, 2, 3, 0, 1, 0, 1], 'segmentEach.segmentIndex'); + t.deepEqual(featureIndexes, [1, 2, 2, 2, 2, 4, 4, 4, 4], 'segmentEach.featureIndex'); + t.deepEqual(featureSubIndexes, [0, 0, 0, 0, 0, 0, 0, 1, 1], 'segmentEach.featureSubIndex'); t.end(); });