PowerGeoJSONFeatureCollection.fromMap constructor

PowerGeoJSONFeatureCollection.fromMap(
  1. Map<String, dynamic> json
)

Creates a PowerGeoJSONFeatureCollection instance from a map.

  • json: A map representation of the PowerGeoJSONFeatureCollection.

Returns a PowerGeoJSONFeatureCollection instance.

Implementation

factory PowerGeoJSONFeatureCollection.fromMap(Map<String, dynamic> json) {
  Object? type = json['type'];
  PowerGeoJSONFeatureCollection featureCollectionDefault =
      PowerGeoJSONFeatureCollection(
        geoJSONPoints: <PowerGeoPoint>[],
        geoJSONLineStrings: <PowerGeoLineString>[],
        geoJSONPolygons: <PowerGeoPolygon>[],
      );
  switch (type) {
    case 'Point':
      GeoJSONPoint point = GeoJSONPoint.fromMap(json);
      PowerGeoJSONFeatureCollection featureCollectionPoint =
          PowerGeoJSONFeatureCollection(
            geoJSONPoints: <PowerGeoPoint>[PowerGeoPoint(geometry: point)],
            geoJSONLineStrings: <PowerGeoLineString>[],
            geoJSONPolygons: <PowerGeoPolygon>[],
          );
      return featureCollectionPoint;

    // ... (other cases for different GeoJSON types)

    case 'FeatureCollection':
      GeoJSONFeatureCollection geoJSONFeatureCollection =
          GeoJSONFeatureCollection.fromMap(json);
      List<GeoJSONFeature?> features = geoJSONFeatureCollection.features;
      List<GeoJSONFeature> listFeatures = features
          .where((GeoJSONFeature? element) => element != null)
          .map((GeoJSONFeature? e) => e as GeoJSONFeature)
          .toList();
      List<PowerGeoFeature> listGeoFeatures = listFeatures
          .map(PowerGeoFeature.parseFeature)
          .expand((List<PowerGeoFeature> e) => e)
          .toList();
      return PowerGeoJSONFeatureCollection(
        geoJSONPoints: listGeoFeatures.whereType<PowerGeoPoint>().toList(),
        geoJSONLineStrings: listGeoFeatures
            .whereType<PowerGeoLineString>()
            .toList(),
        geoJSONPolygons: listGeoFeatures
            .whereType<PowerGeoPolygon>()
            .toList(),
      );
    default:
      return featureCollectionDefault;
  }
}