featureReduce<T> function

T? featureReduce<T>(
  1. GeoJSONObject geojson,
  2. FeatureReduceCallback<T> callback,
  3. T? initialValue
)

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

Takes FeatureCollection, Feature, or GeometryObject, a FeatureReduceCallback method that takes (previousValue, currentFeature, featureIndex), 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'})
]);

featureReduce(features, (previousValue, currentFeature, featureIndex) {
  //=previousValue
  //=currentFeature
  //=featureIndex
  return currentFeature
});

Implementation

T? featureReduce<T>(
  GeoJSONObject geojson,
  FeatureReduceCallback<T> callback,
  T? initialValue,
) {
  T? previousValue = initialValue;
  featureEach(geojson, (currentFeature, featureIndex) {
    if (featureIndex == 0 && initialValue == null && currentFeature is T) {
      previousValue = currentFeature.clone() as T;
    } else {
      previousValue = callback(previousValue, currentFeature, featureIndex);
    }
  });
  return previousValue;
}