synw / geojson

Utilities to work with geojson data in Dart
MIT License
37 stars 51 forks source link

GeoJson don't read GeometryCollection #10

Closed CaiqueR closed 4 years ago

CaiqueR commented 4 years ago

Hi, I'm creating an app for my college work and I decided to use your library to be able to read a geojson. But when it comes to reading geojson when it comes to a part that has a type that the library can't understand inside geometry which is the GeometryCollection

It's all right when the type is Polygon, which I'm using. Is there any way to ignore this exception to continue running the application or any implementation for it?

{
    "type": "Feature",
    "geometry": {
        "geometries": [
            {
                "type": "Polygon",
                "coordinates": [
                    [
                        [coordinates]
                    ]
                ]
            },
            {
                "type": "Polygon",
                "coordinates": [
                    [
                        [morecoordinates]
                    ]
                ]
            },
            {
                "type": "Polygon",
                "coordinates": [
                    [
                        [anycoordinate]
                    ]
                ]
            },
            {
                "type": "Polygon",
                "coordinates": [
                    [
                        [coordinate]
                    ]
                ]
            }
        ],
        "type": "GeometryCollection"
    },
    "properties": {
        "name": "Inexistente ou Baixo"
    }
}

My code:

final polygons = <Polygon>[];

  Future<bool> processData() async {
    final geojson = GeoJson();
    geojson.processedPolygons.listen((GeoJsonPolygon multiPolygon) {
      for (final polygon in multiPolygon.geoSeries) {
        final geoSerie = GeoSerie(
            type: GeoSerieType.polygon,
            name: polygon.name,
            geoPoints: <GeoPoint>[]);

        geoSerie.geoPoints.addAll(polygon.geoPoints);
        final color =
        Color((math.Random().nextDouble() * 0xFFFFFF).toInt() << 0)
            .withOpacity(0.3);
        Polygon poly = Polygon(
          polygonId: PolygonId('Nice one'),
          fillColor: color,
          points: toLatLng(geoSerie.geoPoints, ignoreErrors: true),
        );
        setState(() => polygons.add(poly));
      }
    });
    geojson.endSignal.listen((bool _) => geojson.dispose());
    // The data is from https://datahub.io/core/geo-countries
    final data = await rootBundle.loadString('assets/coordinates/map.geojson');
    final nameProperty = "Name";
      await geojson.parse(data, nameProperty: nameProperty, verbose: true);

    return true;
  }
synw commented 4 years ago

The GeometryCollection support is definitely missing yes. This should be implemented.

Is there any way to ignore this exception to continue running the application or any implementation for it?

The error handling can be improved yes. There is refactoring work to do in this module considering the other pending improvement suggestions that we have actually. I hope that I can find the time to work on this soon

CaiqueR commented 4 years ago

Ok, thank you. Waiting implementation :)

synw commented 4 years ago

Reopening as this is not yet resolved

synw commented 4 years ago

@CaiqueR : I added support for parsing geometry collections. I only tested it with artificial test data. Could you please help testing the feature with some realistic datasets before we release?

CaiqueR commented 4 years ago

@CaiqueR : I added support for parsing geometry collections. I only tested it with artificial test data. Could you please help testing the feature with some realistic datasets before we release?

@synw How can I make this implementation of geometrycollection?

synw commented 4 years ago

Just run the parser as usual, it will detect the geometry collections and pack them into a new GeoJsonGeometryCollection object. The data used in one of the example has geometry collection: run the dart/functions.dart example to have a demo

synw commented 4 years ago

Released in 0.8.0