flattenReduce<T> function

T? flattenReduce<T>(
  1. GeoJSONObject geojson,
  2. FlattenReduceCallback<T> callback,
  3. T? initialValue
)

Reduces flattened features in any GeoJSONObject, similar to Iterable.reduce. Takes a FeatureCollection, Feature, or Geometry a FlattenReduceCallback method that takes (previousValue, currentFeature, featureIndex, multiFeatureIndex), 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'})
]);

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

Implementation

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