Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segmentReduce & segmentEach @turf/meta #968

Merged
merged 1 commit into from
Sep 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions packages/turf-meta/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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
});
Expand Down Expand Up @@ -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`
Expand All @@ -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
});

Expand Down
38 changes: 21 additions & 17 deletions packages/turf-meta/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,9 +707,9 @@ export function flattenReduce(geojson, callback, initialValue) {
*
* @callback segmentEachCallback
* @param {Feature<LineString>} 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}
*/

Expand All @@ -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
* });
Expand All @@ -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.
Expand All @@ -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;
});
});
Expand All @@ -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<LineString>} [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.
*/

/**
Expand All @@ -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
* });
*
Expand All @@ -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;
}
Expand Down
70 changes: 45 additions & 25 deletions packages/turf-meta/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down