geojson 0.6.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 87

Geojson #

pub package Build Status Coverage Status

Utilities to work with geojson data in Dart. Features:

  • Parser with a reactive api: streams are available to retrieve the geojson features as soon as they are parsed
  • Search: search for properties
  • Geofencing: geofence points in a polygon or from a distance

Note: the data is parsed in an isolate to avoid slowing down the main thread

Simple functions #

featuresFromGeoJson: get a FeaturesCollection from geojson string data. Parameters:

  • data: a string with the geojson data, required
  • nameProperty: the property used for the geoserie name, automaticaly set if null
  • verbose: print the parsed data if true

featuresFromGeoJsonFile: get a FeaturesCollection from a geojson file. Parameters:

  • file: the file to load, required
  • nameProperty: the property used for the geoserie name, automaticaly set if null
  • verbose: print the parsed data if true

Reactive api #

Typed streams are available to retrieve the features as soon as they are parsed. Example: add assets on a Flutter map:

  import 'dart:math' as math;
  import 'package:pedantic/pedantic.dart';
  import 'package:flutter/services.dart' show rootBundle;
  import 'package:geojson/geojson.dart';
  import 'package:flutter_map/flutter_map.dart';

  /// Data for the Flutter map polylines layer
  final lines = <Polyline>[];

  Future<void> parseAndDrawAssetsOnMap() async {
    final data = await rootBundle
        .loadString('assets/railroads_of_north_america.geojson');
    final geojson = GeoJson();
    geojson.processedLines.listen((GeoJsonLine line) {
      final color = Color((math.Random().nextDouble() * 0xFFFFFF).toInt() << 0)
          .withOpacity(0.3);
      setState(() => lines.add(Polyline(
          strokeWidth: 2.0, color: color, points: line.geoSerie.toLatLng())));
    });
    geojson.endSignal.listen((_) => geojson.dispose());
    unawaited(geojson.parse(data, verbose: true));
  }

Check the examples for more details

Available streams #

  • processedFeatures: the parsed features: all the geometries
  • processedPoints: the parsed points
  • processedMultipoints: the parsed multipoints
  • processedLines: the parsed lines
  • processedMultilines: the parsed multilines
  • processedPolygons: the parsed polygons
  • processedMultipolygons: the parsed multipolygons
  • endSignal: parsing is finished indicator

Search in a geojson file:

final geo = GeoJson();
await geo.searchInFile("countries.geojson",
    query: GeoJsonQuery(
        geometryType: GeoJsonFeatureType.multipolygon,
        matchCase: false,
        property: "name",
        value: "Zimbabwe"),
    verbose: true);
List<GeoJsonMultiPolygon> result = geo.multipolygons;

A search method is also available, taking string data in parameter instead of a file path. The streams are available to retrieve the data as soon as it is found

Geofencing #

Geofence points within a distance of a given point:

   final geo = GeoJson();
   /// `point` is the [GeoJsonPoint] to search from
   /// `points` is the list of [GeoJsonPoint] to search in
   /// `distance` is the distance to search in meters
   await geo.geofenceDistance(
         point: point, points: points, distance: distance);
    List<GeoPoint> foundPoints = geo.points;

Geofence points in a polygon:

   final geo = GeoJson();
   /// `polygon` is the [GeoJsonPolygon] to check
   /// `points` is the list of [GeoJsonPoint] to search in
   await geo.geofencePolygon(polygon: polygon, points: points);
    List<GeoPoint> foundPoints = geo.points;

Note: the processedPoints stream is available to retrieve geofenced points as soon as they are found

Supported geojson features #

All the data structures use GeoPoint and GeoSerie from the GeoPoint package to store the geometry data. Data structures used:

GeoJsonFeatureCollection:

  • String name
  • List<GeoJsonFeature> collection

GeoJsonFeature:

  • GeoJsonFeatureType type: types

  • Map<String, dynamic> properties: the json properties of the feature

  • dynamic geometry: the geometry data, depends on the feature type, see below

GeoJsonPoint:

  • String name
  • GeoPoint geoPoint: the geometry data

GeoJsonMultiPoint:

  • String name
  • GeoSerie geoSerie: the geometry data: this will produce a geoSerie of type GeoSerieType.group

GeoJsonLine:

  • String name
  • GeoSerie geoSerie: the geometry data: this will produce a geoSerie of type GeoSerieType.line

GeoJsonMultiLine:

  • String name
  • List<GeoJsonLine> lines

GeoJsonPolygon:

  • String name
  • List<GeoSerie> geoSeries: the geometry data: this will produce a list of geoSerie of type GeoSerieType.polygon*

GeoJsonMultiPolygon:

  • String name
  • List<GeoJsonPolygon> polygons

Note: none of the parameters is final for all of these data structures

Changelog #

0.6.0 #

  • Add search methods
  • Add geofencing methods
  • Add serializers

0.5.0 #

Breaking change: prefix all classes names by GeoJson

0.4.0 #

Add a reactive api with streams to retrive features as soon as they are parsed

0.3.0 #

  • Run the parser in an isolate
  • Add a feature.length getter
  • Upgrade dependencies
  • Add examples for drawing on a Flutter map

0.2.3 #

  • Add tests
  • Fix coordinates in getPoint
  • Fix name parameter for Point
  • Fix unsupported feature exception throw

0.2.2 #

  • Add custom exception for unsupported features
  • Use type parameter for feature geometry instead of dynamic
  • Avoid failure when no json properties are found

0.2.1 #

Fix latitude and longitude invertion

0.2.0 #

Api change: add models and dedicated data structures

0.1.0 #

Initial release

example/README.md

Examples #

Dart examples #

  • Functions: shows how to use the basic functions
  • Stream: shows how to use the streams to retrive data as soon as it is parsed
  • Geofencing from polygons: find all the airports in a country
  • Geofencing from distance: find all the airports within a given distance

Flutter examples #

  • Display polygons on a map: parse a file and display polygons as soon as they are found
  • Display lines on a map: parse a file and display lines as soon as they are found
  • Geofencing from polygons: find all the airports in a country and display them on a map as soon as they are found
  • Geofencing from distance: find all the airports within a given distance and display them on a map as soon as they are found

Screenshot

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  geojson: ^0.6.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:geojson/geojson.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
74
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
87
Learn more about scoring.

We analyzed this package on Oct 19, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, other

Primary library: package:geojson/geojson.dart with components: io, isolate.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.3.0 <3.0.0
geodesy ^0.3.1 0.3.2
geopoint ^0.5.0 0.5.0
iso ^0.2.3 0.2.3
meta ^1.1.6 1.1.7
pedantic ^1.6.0 1.8.0+1
rxdart ^0.22.1+1 0.22.4
Transitive dependencies
ansicolor 1.0.2
console_log_handler 1.1.6
intl 0.16.0
latlong 0.6.1
logging 0.11.3+2
path 1.6.4
slugify2 0.2.1
validate 1.7.0
Dev dependencies
test ^1.6.5
test_coverage ^0.3.0+1