WhichPolygon<T> constructor

WhichPolygon<T>(
  1. Iterable<Map<String, dynamic>> features
)

Reads a GeoJSON-like collection of features, and stores all polygon and multipolygon geometries along with their properties in an r-tree.

Note that properties can be of any type, marked with T. Sometimes you might want to preprocess a feature stream like this:

final query = WhichPolygon<MyData>(fc['features'].map((f) => {
  'geometry': f['geometry'],
  'properties': MyData.fromJson(f['properties']),
});

Implementation

WhichPolygon(Iterable<Map<String, dynamic>> features) {
  final List<_TreeItem<T>> bboxes = [];
  for (final feature in features) {
    final Map<String, dynamic>? geom = feature['geometry'];
    if (geom == null || feature['properties'] == null) continue;

    if (feature['id'] != null && feature['properties'] is Map) {
      // Push id property into the properties map
      feature['properties']['id'] ??= feature['id'];
    }

    final List<dynamic> coords = geom['coordinates'];
    if (geom['type'] == 'Polygon') {
      bboxes.add(_TreeItem<T>(
        _Polygon.fromJson(coords),
        feature['properties'],
      ));
    } else if (geom['type'] == 'MultiPolygon') {
      for (final List<dynamic> polygon in coords) {
        bboxes.add(_TreeItem<T>(
          _Polygon.fromJson(polygon),
          feature['properties'],
        ));
      }
    }
  }
  _tree.load(bboxes);
}