propReduce<T> function

T? propReduce<T>(
  1. GeoJSONObject geojson,
  2. PropReduceCallback<T> callback,
  3. T? initialValue
)

Reduces properties in any GeoJSONObject into a single value, similar to how Iterable.reduce works. However, in this case we lazily run the reduction, so List of all properties is unnecessary.

Takes any FeatureCollection or Feature, a PropReduceCallback, an initialValue to be used 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'})
]);

propReduce(features, (previousValue, currentProperties, featureIndex) {
  //=previousValue
  //=currentProperties
  //=featureIndex
  return currentProperties
});

Implementation

T? propReduce<T>(
  GeoJSONObject geojson,
  PropReduceCallback<T> callback,
  T? initialValue,
) {
  T? previousValue = initialValue;
  propEach(geojson, (currentProperties, featureIndex) {
    if (featureIndex == 0 && initialValue == null) {
      previousValue = currentProperties != null
          ? Map<String, dynamic>.of(currentProperties) as T
          : null;
    } else {
      previousValue = callback(previousValue, currentProperties, featureIndex);
    }
  });
  return previousValue;
}