geomReduce<T> function

T? geomReduce<T>(
  1. GeoJSONObject geoJSON,
  2. GeomReduceCallback<T> callback,
  3. T? initialValue
)

Reduces geometry in any GeoJSONObject, similar to Iterable.reduce.

Takes FeatureCollection, Feature or GeometryObject, a GeomReduceCallback method that takes (previousValue, currentGeometry, featureIndex, featureProperties, featureBBox, featureId) and an initialValue Value to use as the first argument to the first call of the callback. Returns the value that results from the reduction. For example:

var features = FeatureCollection(features: [
  Feature(geometry: Point(coordinates: Position.of([26, 37])), properties: {'foo': 'bar'}),
  Feature(geometry: Point(coordinates: Position.of([36, 53])), properties: {'foo': 'bar'})
]);

geomReduce(features, (previousValue, currentGeometry, featureIndex, featureProperties, featureBBox, featureId) {
  //=previousValue
  //=currentGeometry
  //=featureIndex
  //=featureProperties
  //=featureBBox
  //=featureId
  return currentGeometry
});

Implementation

T? geomReduce<T>(
  GeoJSONObject geoJSON,
  GeomReduceCallback<T> callback,
  T? initialValue,
) {
  T? previousValue = initialValue;
  geomEach(
    geoJSON,
    (
      currentGeometry,
      featureIndex,
      featureProperties,
      featureBBox,
      featureId,
    ) {
      if (previousValue == null && featureIndex == 0 && currentGeometry is T) {
        previousValue = currentGeometry?.clone() as T;
      } else {
        previousValue = callback(
          previousValue,
          currentGeometry,
          featureIndex,
          featureProperties,
          featureBBox,
          featureId,
        );
      }
    },
  );
  return previousValue;
}