createBins function

Map<dynamic, List<int>> createBins(
  1. FeatureCollection<GeometryObject> geojson,
  2. dynamic property
)

createBins Takes a FeatureCollection geojson, and dynamic property key whose corresponding values of the Features will be used to create bins. Returns Map<String, List

var geojson = FeatureCollection<Point>(features: [
   Feature(
     geometry: Point(coordinates: Position.of([10, 10])),
     properties:{'cluster': 0, 'foo': 'null'},
   ),
   Feature(
     geometry: Point(coordinates: Position.of([20, 20])),
     properties: {'cluster': 1, 'foo': 'bar'},
   ),
   Feature(
     geometry: Point(coordinates: Position.of([30, 30])),
     properties: {'0': 'foo'},
   ),
   Feature(
     geometry: Point(coordinates: Position.of([40, 40])),
     properties: {'cluster': 1},
   ),
 ]);
createBins(geojson, 'cluster');
//= { '0': [ 0 ], '1': [ 1, 3 ] }

Implementation

Map<dynamic, List<int>> createBins(
    FeatureCollection geojson, dynamic property) {
  Map<dynamic, List<int>> bins = {};

  featureEach(geojson, (feature, i) {
    var properties = feature.properties ?? {};
    if (properties.containsKey(property)) {
      var value = properties[property];
      if (bins.containsKey(value)) {
        bins[value]!.add(i);
      } else {
        bins[value] = [i];
      }
    }
  });
  return bins;
}