Turf.jsMeta

turf.geomReduce

What is turf.geomReduce?

turf.geomReduce folds every Geometry of a GeoJSON input into a single accumulator, passing each Geometry along with its parent Feature's properties, bbox, and id to the callback.

JavaScript
turf.geomReduce(geojson, callback, initialValue?) → any

The callback receives (previousValue, currentGeometry, featureIndex, featureProperties, featureBBox, featureId).

When would you use turf.geomReduce?

Use turf.geomReduce when you need a summary that depends on both geometry and the owning Feature's metadata — for example a map from featureId to geometry type, a property-weighted centroid, or a bounding-box union that must retain the source id of each participating geometry.

JavaScript
1const typeCounts = turf.geomReduce(
2  fc,
3  (counts, geom) => ((counts[geom.type] = (counts[geom.type] || 0) + 1), counts),
4  {}
5);

FAQs

How do I install Turf.js to use this function?

Install npm install @turf/meta and import import { geomReduce } from '@turf/meta', or access turf.geomReduce on the umbrella bundle.

How does it differ from featureReduce?

featureReduce visits each Feature once. geomReduce unwraps GeometryCollections, visiting each contained geometry. Multi-geometries (MultiPolygon etc.) are yielded once as a whole.

Is the accumulator mutable?

Yes — you may mutate the accumulator or return a new value each iteration. Mutating is useful for building Maps or arrays without allocation overhead.

When should I pick flattenReduce instead?

Use flattenReduce when you want each part of a multi-geometry to count as a separate iteration. geomReduce counts a MultiPolygon with three islands as one iteration.