MultiPolygon.fromJson constructor

  1. @override
MultiPolygon.fromJson(
  1. Map<String, dynamic> json
)

Creates a MultiPolygon from a GeoJSON Map.

Example:

MultiPolygon.fromJson({'type': 'Feature', 'geometry': {'type': 'MultiPolygon', 'coordinates': [[[[1, 2], [3, 4], [5, 6], [1, 2]]], [[[7, 8], [9, 10], [11, 12], [7, 8]]]]}, 'properties': {}}); // MultiPolygon([[LinearRing([Coordinate(1, 2), Coordinate(3, 4), Coordinate(5, 6), Coordinate(1, 2)]), LinearRing([Coordinate(7, 8), Coordinate(9, 10), Coordinate(11, 12), Coordinate(7, 8)])]])

Implementation

@override
factory MultiPolygon.fromJson(Map<String, dynamic> json) {
  if (json['geometry']['type'] != 'MultiPolygon') {
    throw ArgumentError('json is not a MultiPolygon');
  }

  MultiPolygon poly = MultiPolygon(
    (json['geometry']['coordinates'] as List)
        .map((poly) => (poly as List)
            .map((shape) => LinearRing((shape as List)
                .map((coord) => Coordinate.fromJson((coord as List)
                    .map((e) => (e is int ? e.toDouble() : e as double))
                    .toList()))
                .toList()))
            .toList()) // Convert the outer Iterable to a List
        .toList(), // Convert the inner Iterable to a List
    properties: Map<String, dynamic>.from(json['properties']),
  );

  return poly;
}