coordReduce<T> function

T? coordReduce<T>(
  1. GeoJSONObject geojson,
  2. CoordReduceCallback<T> callback,
  3. T? initialValue, [
  4. bool excludeWrapCoord = false,
])

Reduces coordinates in any GeoJSONObject, similar to Iterable.reduce

Takes FeatureCollection, GeometryObject, or a Feature, a CoordReduceCallback method that takes (previousValue, currentCoord, coordIndex), an initialValue Value to use as the first argument to the first call of the callback, and a boolean excludeWrapCoord (default false) for whether or not to include the final coordinate of LinearRings that wraps the ring in its iteration. 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'})
]);

coordReduce(features, (previousValue, currentCoord, coordIndex, featureIndex, multiFeatureIndex, geometryIndex) {
  //=previousValue
  //=currentCoord
  //=coordIndex
  //=featureIndex
  //=multiFeatureIndex
  //=geometryIndex
  return currentCoord;
});

Implementation

T? coordReduce<T>(
  GeoJSONObject geojson,
  CoordReduceCallback<T> callback,
  T? initialValue, [
  bool excludeWrapCoord = false,
]) {
  var previousValue = initialValue;
  coordEach(geojson, (Position? currentCoord, coordIndex, featureIndex,
      multiFeatureIndex, geometryIndex) {
    if (coordIndex == 0 && initialValue == null && currentCoord is T) {
      previousValue = currentCoord?.clone() as T;
    } else {
      previousValue = callback(previousValue, currentCoord, coordIndex,
          featureIndex, multiFeatureIndex, geometryIndex);
    }
  }, excludeWrapCoord);
  return previousValue;
}