chuyentt / geojson_vi

An Open-Source Dart and Flutter Library for Efficiently Handling GeoJSON Data in Compliance with RFC 7946
https://pub.dev/packages/geojson_vi
MIT License
15 stars 10 forks source link

please allow geometry collection to be empty #16

Closed barbalex closed 3 years ago

barbalex commented 3 years ago

I have code that creates a geometry collection. It either loads an existing one from the database row. Or creates a new one with empty geometry.

Later the code branches into different functions and adds or removes geometries, depending on what actions the user completes.

Then the row is saved, including the geometry collection that now (usually) includes geometries.

The problem is: geojson_vi does not allow a geometry collection to be created without geometries:

E/flutter (31756): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: 'package:geojson_vi/src/classes/geometry_collection.dart': Failed assertion: line 39 pos 16: 'geometries.isNotEmpty': The coordinates MUST be one or more elements

This makes it impossible to work efficiently with geometry collections. I can't know beforehand when the user will remove the last geometry before adding new ones. And I cant simply start with a geometry collection, no matter whether one already existed. Instead I have to convert existing ones into maps. And build all my functions to work with maps. And at the end convert these maps to a geometry collection. Which then is the ONLY time geojson_vi will be of any help.

This makes the code much more complicated. And drastically reduces the use that geojson_vi brings as a tool: namely to work with geometries themselves instead of maps.

What is the reason a geometry collection is not allowed to be empty?

Could this stringent assertion be removed?

I guess I would have to insert a fake geometry to circumvent the assertion. And never forget to remove it when saving the row. Which does not sound like good practice.

barbalex commented 3 years ago

I just found this other library obviously allowing empty geometries as there is an isEmpty method: https://pub.dev/documentation/geocore/latest/base/GeometryCollection/isEmpty.html

chuyentt commented 3 years ago

Try it out

GeoJSONGeometryCollection? geomCollection = GeoJSONGeometryCollection([]);
barbalex commented 3 years ago

That is exactly what I had

barbalex commented 3 years ago

If I change:

GeoJSONGeometryCollection? geomCollection;

to:

GeoJSONGeometryCollection geomCollection = GeoJSONGeometryCollection([]);

I get:

════════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building MapMapWidget(dirty, state: _MapMapWidgetState#2b11e):
The coordinates MUST be one or more elements
'package:geojson_vi/src/classes/geometry_collection.dart':
Failed assertion: line 39 pos 16: 'geometries.isNotEmpty'

See why here: https://github.com/chuyentt/geojson_vi/blob/master/lib/src/classes/geometry_collection.dart#L38-L40

barbalex commented 3 years ago

It seems this was added for version 2.0.0:

Before: https://github.com/chuyentt/geojson_vi/blob/a6aa1568e76072c1a20c0582668f2c6ea518de3e/lib/src/classes/geometry_collection.dart#L8 After: https://github.com/chuyentt/geojson_vi/blob/fdca2387b89c8eb113c75d788674afd5c53c8666/lib/src/classes/geometry_collection.dart#L38-L40

chuyentt commented 3 years ago

Thank you,

I've just released geojson_vi 2.0.3

barbalex commented 3 years ago

@chuyentt thanks for giving us this great tool!