Skip to content

GeoJSON client

The GeoJSON client allows fetching and reading geospatial feature collections with their geometry objects (ie. point, line string, polygon, multi point, multi line string, multi polygon and geometry collection) from following resource types:

  • a web resource (by URL) containing GeoJSON content - data is fetched using the HTTP client (as provided by the http package)
  • custom resources, ie. a local file or an app bundled containing valid GeoJSON data

Please note that this client is not related to OGC API Features or any other API protocol either, but you can access any (static) web or local resource with GeoJSON data.

πŸ“ƒ Read GeoJSON features

The sample below shows to read GeoJSON features from a web resource using the HTTP client.

import 'package:geodata/geojson_client.dart';
Future<void> main(List<String> args) async {
// read GeoJSON for earthquakes from web using HTTP(S)
await _readFeatures(
GeoJSONFeatures.http(
// API address
location: Uri.parse(
'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/'
'2.5_day.geojson',
),
),
);
}
Future<void> _readFeatures(BasicFeatureSource source) async {
// read features with error handling
try {
// get items or features from a source, maximum 5 features returned
final items = await source.itemsAll(limit: 5);
// do something with features, in this sample just print them out
for (final f in items.collection.features) {
print('Feature with id: ${f.id}');
print(' geometry: ${f.geometry}');
print(' properties:');
for (final key in f.properties.keys) {
print(' $key: ${f.properties[key]}');
}
}
} on ServiceException<FeatureFailure> catch (e) {
print('Reading GeoJSON resource failed: ${e.failure.name}');
print('Cause: ${e.cause}');
} catch (e) {
print('Reading GeoJSON resource failed: $e');
}
}

The full sample for accessing GeoJSON feature sources is available in geojson_example.dart.

␀ Newline-delimited GeoJSON

GeoJSON or newline-delimited GeoJSON (or GeoJSON Text Sequences) is an optimized variant of GeoJSON to encode sequences of geospatial features. A text file conforming to this format represents one feature collection (without FeatureCollection element encoded). Such a file may contain any number of features that are separated by the newline character (\n).

Decoding and encoding data for this format is supported by the geobase package just like it’s supporting the standard GeoJSON too.

When accessing newline-delimited GeoJSON data with the geodata package, you should assign format: GeoJSONL.feature when creating a feature source either with GeoJSONFeatures.http() or GeoJSONFeatures.any(). Otherwise the usage patterns introduced for the standard GeoJSON applies also here.