feature<T extends Geometry> method
Parses a feature from a data
object.
Throws FormatException if parsing fails.
Implementation
@override
Feature<T> feature<T extends Geometry>(dynamic data) {
// expects data of Map<String, dynamic> as returned by json.decode()
final json = _ensureDecodedMap(data);
if (json['type'] != 'Feature') {
throw const FormatException('Not valid GeoJSON Feature.');
}
// parse id as String?
// (GeoJSON allows num and String types for ids - both represented here
// as String)
final dynamic id = json['id'];
if (!(id == null || id is String || id is int || id is BigInt)) {
throw const FormatException('Id should be null, int, BigInt or String');
}
// parse optional geometry for this feature
final dynamic geomJson = json['geometry'];
final geom = geomJson != null ? geometry<T>(geomJson) : null;
// parse optional bbox
// (bbox is not required on GeoJSON and not on Feature either)
final bboxJson = json['bbox'] as Iterable?;
final bbox = bboxJson != null ? bounds(bboxJson) : null;
// create a feature using the factory function
return featureFactory<T>(
// nullable id
id: id,
// map of properties may be missing on GeoJSON, but for a feature
// non-null map (even empty) is required
properties: json['properties'] as Map<String, Object?>? ?? {},
// nullable geometry object
geometry: geom,
// nullable bounds object
bounds: bbox,
// the JSON Object from source data provided as-is
// (this lets custom feature factories to parse properties not known by
// the JSON specification)
jsonObject: json,
);
}