segmentReduce<T> function

T? segmentReduce<T>(
  1. GeoJSONObject geojson,
  2. SegmentReduceCallback<T> callback,
  3. T? initialValue, {
  4. bool combineNestedGeometries = true,
})

Reduces 2-vertex line segment in any GeoJSON object, similar to Iterable.reduce (Multi)Point geometries do not contain segments therefore they are ignored during this operation.

Takes FeatureCollection, Feature, GeoJSONObject, a SegmentReduceCallback method that takes (previousValue, currentSegment, currentIndex), an initialValue value to use as the first argument to the first call of the callback.

Iterates over GeoJSONObject by 2-vertex segments For example:

var polygon =Polygon(coordinates: [
     [
       Position.of([0, 0]),
       Position.of([1, 1]),
      Position.of([0, 1]),
       Position.of([0, 0]),
     ],
   ]),

segmentReduce(polygon, (previousSegment, currentSegment, featureIndex, multiFeatureIndex, geometryIndex, segmentIndex) {
  //= previousSegment
  //= currentSegment
  //= featureIndex
  //= multiFeatureIndex
  //= geometryIndex
  //= segmentIndex
  return currentSegment
});

// Calculate the total number of segments
var total = segmentReduce(polygon, (previousValue) {
    previousValue++;
    return previousValue;
}, 0);

Implementation

T? segmentReduce<T>(
  GeoJSONObject geojson,
  SegmentReduceCallback<T> callback,
  T? initialValue, {
  bool combineNestedGeometries = true,
}) {
  T? previousValue = initialValue;
  var started = false;
  segmentEach(
    geojson,
    (currentSegment, featureIndex, multiFeatureIndex, geometryIndex,
        segmentIndex) {
      if (started == false && initialValue == null && initialValue is T) {
        previousValue = currentSegment.clone() as T;
      } else {
        previousValue = callback(previousValue, currentSegment, initialValue,
            featureIndex, multiFeatureIndex, geometryIndex, segmentIndex);
      }
      started = true;
    },
    combineNestedGeometries: combineNestedGeometries,
  );
  return previousValue;
}