Skip to main content
Version: Next

segmentReduce

Description

Reduce 2-vertex line segment in any GeoJSON object, similar to Array.reduce() (Multi)Point geometries do not contain segments therefore they are ignored during this operation.

Parameters

NameTypeDescription
geojsonFeatureCollection | Feature | Geometryany GeoJSON
callbackFunctiona method that takes (previousValue, currentSegment, currentIndex)
initialValue?*Value to use as the first argument to the first call of the callback.

Returns

    void

Examples

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,
featureIndex,
multiFeatureIndex,
geometryIndex,
segmentIndex,
) {
//= previousSegment
//= currentSegment
//= featureIndex
//= multiFeatureIndex
//= geometryIndex
//= segmentIndex
return currentSegment;
},
);

// Calculate the total number of segments
var initialValue = 0;
var total = turf.segmentReduce(
polygon,
function (previousValue) {
previousValue++;
return previousValue;
},
initialValue,
);

Installation

$ npm install @turf/meta

import { segmentReduce } from "@turf/meta";
const result = segmentReduce(...);
$ npm install @turf/turf

import * as turf from "@turf/turf";
const result = turf.segmentReduce(...);