FeatureCollection.fromJson constructor

FeatureCollection.fromJson(
  1. Map<String, dynamic> json
)

Creates a FeatureCollection from a JSON object.

Example:

FeatureCollection.fromJson({'type': 'FeatureCollection', 'features': [{'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [1, 2]}, 'properties': {}}, {'type': 'Feature', 'geometry': {'type': 'Point', 'coordinates': [3, 4]}, 'properties': {}}]}); // FeatureCollection([Point(Coordinate(1, 2)), Point(Coordinate(3, 4))])

Implementation

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

  return FeatureCollection(
    (List<Map<String, dynamic>>.from(json['features']))
        .map((Map<String, dynamic> f) {
      if (f['geometry']['type'] == 'Point') {
        return Point.fromJson(f);
      } else if (f['geometry']['type'] == 'MultiPoint') {
        return MultiPoint.fromJson(f);
      } else if (f['geometry']['type'] == 'LineString') {
        return LineString.fromJson(f);
      } else if (f['geometry']['type'] == 'MultiLineString') {
        return MultiLineString.fromJson(f);
      } else if (f['geometry']['type'] == 'Polygon') {
        return Polygon.fromJson(f);
      } else if (f['geometry']['type'] == 'MultiPolygon') {
        return MultiPolygon.fromJson(f);
      } else {
        throw ArgumentError('json is not a feature');
      }
    }).toList(),
  );
}