parseGeometry static method

List<PowerGeoFeature> parseGeometry(
  1. GeoJSONGeometry geometry, {
  2. Map<String, dynamic>? properties,
  3. List<double>? bbox,
  4. String? title,
  5. dynamic id,
})

Parses a GeoJSON geometry and returns a list of PowerGeoFeature instances.

  • geometry: The GeoJSON geometry to parse.
  • properties: Optional properties to associate with the parsed features.
  • bbox: Optional bounding box to associate with the parsed features.
  • title: Optional title to associate with the parsed features.
  • id: Optional unique identifier to associate with the parsed features.

Returns a list of PowerGeoFeature instances based on the provided GeoJSON geometry.

Implementation

static List<PowerGeoFeature> parseGeometry(
  GeoJSONGeometry geometry, {
  Map<String, dynamic>? properties,
  List<double>? bbox,
  String? title,
  dynamic id,
}) {
  switch (geometry.type) {
    case GeoJSONType.point:
      GeoJSONPoint geom = geometry as GeoJSONPoint;
      List<double> coordinates = geom.coordinates;
      return <PowerGeoFeature>[
        PowerGeoPoint(
          geometry: GeoJSONPoint(coordinates),
          properties: properties,
          bbox: bbox,
          title: title,
          id: id,
        ),
      ];
    case GeoJSONType.multiPoint:
      GeoJSONMultiPoint geom = geometry as GeoJSONMultiPoint;
      List<List<double>> coordinates = geom.coordinates;
      return coordinates
          .map(
            (List<double> e) => PowerGeoPoint(
              geometry: GeoJSONPoint(e),
              properties: properties,
              bbox: bbox,
              title: title,
              id: id,
            ),
          )
          .toList();
    case GeoJSONType.lineString:
      GeoJSONLineString geom = geometry as GeoJSONLineString;
      List<List<double>> coordinates = geom.coordinates;
      return <PowerGeoFeature>[
        PowerGeoLineString(
          geometry: GeoJSONLineString(coordinates),
          properties: properties,
          bbox: bbox,
          title: title,
          id: id,
        ),
      ];
    case GeoJSONType.multiLineString:
      GeoJSONMultiLineString geom = geometry as GeoJSONMultiLineString;
      List<List<List<double>>> coordinates = geom.coordinates;
      return coordinates
          .map(
            (List<List<double>> e) => PowerGeoLineString(
              geometry: GeoJSONLineString(e),
              properties: properties,
              bbox: bbox,
              title: title,
              id: id,
            ),
          )
          .toList();
    case GeoJSONType.polygon:
      GeoJSONPolygon geom = geometry as GeoJSONPolygon;
      List<List<List<double>>> coordinates = geom.coordinates;
      return <PowerGeoFeature>[
        PowerGeoPolygon(
          geometry: GeoJSONPolygon(coordinates),
          properties: properties,
          bbox: bbox,
          title: title,
          id: id,
        ),
      ];
    case GeoJSONType.multiPolygon:
      GeoJSONMultiPolygon geom = geometry as GeoJSONMultiPolygon;
      List<List<List<List<double>>>> coordinates = geom.coordinates;
      return coordinates
          .map(
            (List<List<List<double>>> e) => PowerGeoPolygon(
              geometry: GeoJSONPolygon(e),
              properties: properties,
              bbox: bbox,
              title: title,
              id: id,
            ),
          )
          .toList();
    case GeoJSONType.feature:
      GeoJSONFeature geom = geometry as GeoJSONFeature;
      GeoJSONGeometry? geometry2 = geom.geometry;
      if (geometry2 == null) return <PowerGeoFeature>[];
      return parseGeometry(
        geometry2,
        properties: properties,
        bbox: bbox,
        title: title,
        id: id,
      );
    case GeoJSONType.featureCollection:
      GeoJSONFeatureCollection geom = geometry as GeoJSONFeatureCollection;
      List<GeoJSONFeature?> features = geom.features;
      Iterable<GeoJSONFeature> notNull = features.nonNulls;
      Iterable<List<PowerGeoFeature>?> featuresParse = notNull.map((
        GeoJSONFeature e,
      ) {
        GeoJSONGeometry? geometry3 = e.geometry;
        if (geometry3 == null) return null;
        return parseGeometry(
          geometry3,
          properties: properties,
          bbox: bbox,
          title: title,
          id: id,
        );
      });
      Iterable<List<PowerGeoFeature>> whereNotNull = featuresParse.nonNulls;
      return whereNotNull.expand((List<PowerGeoFeature> e) => e).toList();
    case GeoJSONType.geometryCollection:
      GeoJSONGeometryCollection geom = geometry as GeoJSONGeometryCollection;
      Iterable<List<PowerGeoFeature>> coordinates = geom.geometries.map(
        (GeoJSONGeometry e) => parseGeometry(
          e,
          properties: properties,
          bbox: bbox,
          title: title,
          id: id,
        ),
      );
      return coordinates.expand((List<PowerGeoFeature> e) => e).toList();
  }
}