clusterReduce<T> function

T? clusterReduce<T>(
  1. FeatureCollection<GeometryObject> geojson,
  2. dynamic property,
  3. ClusterReduceCallback<T> callback,
  4. dynamic initialValue,
)

Reduces clusters in Features, similar to Iterable.reduce Takes a geojson, a dynamic porperty, a GeoJSONObject's property key/value used to create clusters, a ClusterReduceCallback method, and an initialValue to use as the first argument to the first call of the callback. Returns the value that results from the reduction. For example:

var geojson = FeatureCollection<Point>(features: [
   Feature(
     geometry: Point(coordinates: Position.of([10, 10])),
   ),
   Feature(
     geometry: Point(coordinates: Position.of([20, 20])),
   ),
   Feature(
     geometry: Point(coordinates: Position.of([30, 30])),
   ),
   Feature(
     geometry: Point(coordinates: Position.of([40, 40])),
   ),
 ]);

// Creates a cluster using K-Means (adds `cluster` to GeoJSON properties)
var clustered = clustersKmeans(geojson);

// Iterates over each cluster and perform a calculation
var initialValue = 0
clusterReduce(clustered, 'cluster', (previousValue, cluster, clusterValue, currentIndex) {
    //=previousValue
    //=cluster
    //=clusterValue
    //=currentIndex
    return previousValue++;
}, initialValue);

// Calculates the total number of clusters
var total = clusterReduce(clustered, 'cluster', function (previousValue) {
    return previousValue++;
}, 0);

// Creates a [List] of all the values retrieved from the 'cluster' property.
var values = clusterReduce(clustered, 'cluster', (previousValue, cluster, clusterValue){
    return previousValue.addAll(clusterValue);
}, []);

Implementation

T? clusterReduce<T>(
  FeatureCollection geojson,
  dynamic property,
  ClusterReduceCallback<T> callback,
  dynamic initialValue,
) {
  var previousValue = initialValue;
  clusterEach(geojson, property, (cluster, clusterValue, currentIndex) {
    if (currentIndex == 0 && initialValue == null) {
      previousValue = cluster;
    } else {
      previousValue =
          callback(previousValue, cluster, clusterValue, currentIndex);
    }
  });
  return previousValue;
}