segmentEach function
- GeoJSONObject geojson,
- SegmentEachCallback callback, {
- bool combineNestedGeometries = true,
Iterates over 2-vertex line segment in any GeoJSON object, similar to Iterable.forEach (Multi)Point geometries do not contain segments therefore they are ignored during this operation.
Takes FeatureCollection,Feature or GeometryObject geojson any GeoJSON
a SegmentEachCallback method that takes (currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex),
and a combineNestedGeometries
flag that connects Polygon's geometries with each other.
For example:
var polygon = Polygon(coordinates: [
[
Position.of([0, 0]),
Position.of([1, 1]),
Position.of([0, 1]),
Position.of([0, 0]),
],
]),
Iterates over GeoJSON by 2-vertex segments segmentEach(polygon, (currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex) { //=currentSegment //=featureIndex //=multiFeatureIndex //=geometryIndex //=segmentIndex });
// Calculate the total number of segments var total = 0; turf.segmentEach(polygon, function () { total++; });
Implementation
void segmentEach(
GeoJSONObject geojson,
SegmentEachCallback callback, {
bool combineNestedGeometries = true,
}) {
int segmentIndex = 0;
flattenEach(
geojson,
(Feature<GeometryType> currentFeature, int featureIndex,
int multiFeatureIndex) {
var geometry = currentFeature.geometry;
if (geometry is Point) {
return false;
}
if (geometry != null && combineNestedGeometries) {
segmentIndex = _segmentEachforEachUnit(
geometry,
callback,
currentFeature.properties,
featureIndex,
multiFeatureIndex,
segmentIndex,
);
} else {
List<List<Position>> coords = [];
if (geometry is Polygon) {
coords = geometry.coordinates;
}
if (geometry is LineString) {
coords.add(geometry.coordinates);
}
for (int i = 0; i < coords.length; i++) {
var line = LineString(coordinates: coords[i]);
segmentIndex = _segmentEachforEachUnit(
line,
callback,
currentFeature.properties,
featureIndex,
multiFeatureIndex,
segmentIndex,
);
}
}
},
);
}