Skip to main content
Version: 7.0.0

segmentEach

Description

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

Parameters

NameTypeDescription
geojsonFeatureCollection | Feature | Geometryany GeoJSON
callbackFunctiona method that takes (currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex)

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.segmentEach(
polygon,
function (
currentSegment,
featureIndex,
multiFeatureIndex,
geometryIndex,
segmentIndex,
) {
//=currentSegment
//=featureIndex
//=multiFeatureIndex
//=geometryIndex
//=segmentIndex
},
);

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

Installation

$ npm install @turf/meta

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

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