getGeoJSON method

Future<Map<String, dynamic>?> getGeoJSON([
  1. String? id,
  2. Map<String, dynamic>? options
])

Retrieve geometry data in GeoJSON format.

If id is provided, returns the specific Feature. Otherwise, returns a FeatureCollection containing all features. Use options to specify format: { "format": "standard" | "imdf" }.

Implementation

Future<Map<String, dynamic>?> getGeoJSON([
  String? id,
  Map<String, dynamic>? options,
]) async {
  final format = options?['format'] ?? 'standard';
  if (id != null) {
    for (var sourceData in _geoJsonCache.values) {
      if (sourceData['type'] == 'FeatureCollection') {
        final features = sourceData['features'] as List<dynamic>? ?? [];
        for (var feature in features) {
          if (feature['id']?.toString() == id ||
              feature['properties']?['user_id']?.toString() == id) {
            if (format == 'standard') {
              return feature;
            }
            final fc = <String, dynamic>{'type': 'FeatureCollection', 'features': [feature]};
            final imdfFc = await IMDFConverter.toIMDF(fc);
            if ((imdfFc['features'] as List).isNotEmpty) {
              return imdfFc['features'][0];
            }
            return feature;
          }
        }
      }
    }
    return null;
  }

  final List<dynamic> allFeatures = [];
  for (var sourceData in _geoJsonCache.values) {
    if (sourceData['type'] == 'FeatureCollection') {
      allFeatures.addAll(sourceData['features'] ?? []);
    } else if (sourceData['type'] == 'Feature') {
      allFeatures.add(sourceData);
    }
  }

  final fc = <String, dynamic>{
    'type': 'FeatureCollection',
    'features': allFeatures,
  };
  if (format == 'standard') {
    return fc;
  }
  return await IMDFConverter.toIMDF(fc);
}