geojson_vi 1.2.2

  • Readme
  • Changelog
  • Example
  • Installing
  • 74

GeoJSON package for Dart and Flutter developers #

pub package

GeoJSON package for Dart and Flutter developers to create, read, search, update and delete the geospatial data interchange format (GIS data). This package was originally published by Chuyen, T. T. (2020). The GeoJSON package for Dart and Flutter developers. Zenodo. https://doi.org/10.5281/ZENODO.3841927.

GeoJSON package support GeoJSON objects like spatially bounded entity (a Feature), or a list of Features (a FeatureCollection). GeoJSON supports the following geometry types like Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection. Features in GeoJSON contain a Geometry object and additional properties, and a FeatureCollection contains a list of Features.

GeoJSON UML Model #

GeoJSON UML Model

Usage #

Read the GeoJSON file #

import 'package:geojson_vi/geojson_vi.dart';

void main() {
  
  // # Read the GeoJSON file
  var launchTime = DateTime.now();
  await GeoJSON.load('example/data/parcels_82mb.geojson').then((GeoJSON geoJSON) {
    print(geoJSON.featureCollection.features.length);
    print(DateTime.now().difference(launchTime));
  });
}

Read the GeoJSON file async #

import 'package:geojson_vi/geojson_vi.dart';

Future<void> main() async {

  // # Read the GeoJSON file async
  var launchTime = DateTime.now();
  var geoJSON = await GeoJSON.load('example/data/parcels_82mb.geojson');
  print(geoJSON.featureCollection.features.length);
  print(DateTime.now().difference(launchTime));
}

Create new GeoJSON with a Feature #

Create an empty GeoJSON file, then add a feature and save it.

import 'package:geojson_vi/geojson_vi.dart';

void main() {
  // ### Create new GeoJSON with a Feature
  // Create an empty GeoJSON file, then add a feature and save it.
  
  // New GeoJSON
  final geoJSON = GeoJSON.create('example/data/new.geojson');
  // TODO: your code here...
  await geoJSON.save();
}

Create the Feature #

import 'package:geojson_vi/geojson_vi.dart';

void main() {
  
  // ### Create a Feature with Point geometry
  
  // New GeoJSON
  final geoJSON = GeoJSON.create('example/data/new.geojson');

  // One position
  final pos = <double>[];
  pos.add(105.7743099);
  pos.add(21.0717561);
  // or pos.addAll([105.7743099,21.0717561]);

  // Create a Point geometry from one position
  final geom_point = GeoJSONPoint(pos);
  
  // Create a Feature
  final feature_point = GeoJSONFeature(geom_point);
  feature_point.properties['marker-color'] = '#7e7e7e';
  feature_point.properties['marker-size'] = 'medium';
  feature_point.properties['marker-symbol'] = 'college';
  feature_point.properties['title'] = 'Hanoi University of Mining and Geology';
  feature_point.properties['department'] = 'Geoinformation Technology';
  feature_point.properties['address'] = 'No.18 Vien Street - Duc Thang Ward - Bac Tu Liem District - Ha Noi, Vietnam';
  feature_point.properties['url'] = 'http://humg.edu.vn';

  // Add the feature to featureCollection
  geoJSON.featureCollection.features.add(feature_point);

  // ### Create a Feature with LineString geometry
  
  // LineString from 3 positions
  final pos1 = [105.7771289,21.0715458];
  final pos2 = [105.7745218,21.0715658];
  final pos3 = [105.7729125,21.0715358];
  
  // Create a LineString geometry from array of position
  final geom_line_string = GeoJSONLineString([pos1, pos2, pos3]);
  
  // Create a Feature 
  final feature_line_string = GeoJSONFeature(geom_line_string);
  feature_line_string.properties['stroke'] = '#7e7e7e';
  feature_line_string.properties['stroke-width'] = 2;
  feature_line_string.properties['stroke-opacity'] = 1;
  feature_line_string.properties['title'] = 'Vien St.';
  
  // Add the fearture to featureCollection
  geoJSON.featureCollection.features.add(feature_line_string);

  // ### Create a Feature with Polygon geometry
  
  // A linear ring is a closed LineString with four or more positions.
  // The first and last positions are equivalent, and they MUST contain
  // identical values; their representation SHOULD also be identical.
  final p01 = [105.7739666,21.0726795]; // The first position
  final p02 = [105.7739719,21.0721991];
  final p03 = [105.7743394,21.0721966];
  final p04 = [105.7743310,21.0725269];
  final p05 = [105.7742564,21.0726120];
  final p06 = [105.7741865,21.0726095];
  final p07 = [105.7741785,21.0726746];
  final p08 = [105.7739666,21.0726795]; // The last position

  //The exterior ring (boundary)
  final linerRing = [p01,p02,p03,p04,p05,p06,p07,p08];
  
  // Create a Polygon geometry from array of position
  final geom_polygon = GeoJSONPolygon([
    linerRing,
    // and others the interior rings (if present) bound holes within the surface
  ]);
  
  // Create a Feature
  final feature_polygon = GeoJSONFeature(geom_polygon);
  feature_polygon.properties['stroke'] = '#555555';
  feature_polygon.properties['stroke-width'] = 2;
  feature_polygon.properties['stroke-opacity'] = 1;
  feature_polygon.properties['fill'] = '#ab7942';
  feature_polygon.properties['fill-opacity'] = 0.5;
  feature_polygon.properties['title'] = 'HUMG\'s Office';

  // Add to featureCollection
  geoJSON.featureCollection.features.add(feature_polygon);

  geoJSON.save();

  // # Read the GeoJSON file
  var launchTime = DateTime.now();
  await GeoJSON.load('example/data/parcels_82mb.geojson').then((GeoJSON geoJSON) {
    print(geoJSON.featureCollection.features.length);
    print(DateTime.now().difference(launchTime));
  });

  // Calculate area of polygon
  await GeoJSON.load('example/data/polygon_with_holes.geojson').then((value) {
    value.featureCollection.features.forEach((element) {
      if (element.geometry.type == GeometryType.polygon) {
        GeoJSONPolygon pg = element.geometry;
        print('Area: ${pg.area}');
      }
    });
  });

  // Search by geometry and properties
  var polygonFeatures = geoJSON.featureCollection.features.where((element) {
    return (element.geometry.type == GeometryType.polygon && (element.properties['title']).contains('HUMG'));
  }).toList();

  print(polygonFeatures.isNotEmpty ? polygonFeatures.first.toMap : 'not found');
}

Output: new.geojson

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "marker-color": "#7e7e7e",
        "marker-size": "medium",
        "marker-symbol": "college",
        "title": "Hanoi University of Mining and Geology",
        "department": "Geoinformation Technology",
        "address": "No.18 Vien Street - Duc Thang Ward - Bac Tu Liem District - Ha Noi, Vietnam",
        "url": "http://humg.edu.vn"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          105.7743099,
          21.0717561
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "stroke": "#7e7e7e",
        "stroke-width": 2,
        "stroke-opacity": 1,
        "title": "Vien St."
      },
      "geometry": {
        "type": "LineString",
        "coordinates": [
          [
            105.7771289,
            21.0715458
          ],
          [
            105.7745218,
            21.0715658
          ],
          [
            105.7729125,
            21.0715358
          ]
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "stroke": "#555555",
        "stroke-width": 2,
        "stroke-opacity": 1,
        "fill": "#ab7942",
        "fill-opacity": 0.5,
        "title": "HUMG's Office"
      },
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              105.7739666,
              21.0726795
            ],
            [
              105.7739719,
              21.0721991
            ],
            [
              105.7743394,
              21.0721966
            ],
            [
              105.774331,
              21.0725269
            ],
            [
              105.7742564,
              21.072612
            ],
            [
              105.7741865,
              21.0726095
            ],
            [
              105.7741785,
              21.0726746
            ],
            [
              105.7739666,
              21.0726795
            ]
          ]
        ]
      }
    }
  ]
}

Quick viewing on geojson.io Quick viewing

Reference #

H. Butler, M. Daly, A. Doyle, S. Gillies, S. Hagen, and T. Schaub, "The GeoJSON Format", August 2016, https://tools.ietf.org/html/rfc7946.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

1.0.0 #

  • Initial version, created by chuyentt

1.0.1 #

  • Update the document

1.0.2 #

  • Update the package description

1.0.3 #

  • Update the package description

1.0.5 #

  • Update the package description

1.0.5+1 #

  • Update the package description

1.0.5+2 #

  • Update the package description

1.0.5+3 #

  • Update the package description

1.0.5+4 #

  • Fix formatting issues

1.0.6 #

  • Fix encode issues

1.0.6+1 #

  • Update GeoJSON UML model

1.0.6+2 #

  • Update the document

1.1.0 #

  • Added calculate area example into the readme

1.1.0+1 #

  • Update the package export

1.1.0+2 #

  • Update the document

1.1.0+3 #

  • Update the document

1.2.0 #

  • Refactoring the code, create instance from a cache if available

1.2.0+1 #

  • Format code

1.2.0+2 #

  • Compatibility issues

1.2.0+3 #

  • Compatibility issues

1.2.1 #

  • Compatiple with Flutter

1.2.1+1 #

  • Update the document

1.2.2 #

  • Compatible with runtime flutter-web of web

example/geojson_vi_example.dart

import 'package:geojson_vi/geojson_vi.dart';

Future<void> main() async {
  // ### Create a Feature with Point geometry

  // New GeoJSON
  final geoJSON = GeoJSON.create('example/data/new.geojson');

  // One position
  final pos = <double>[];
  pos.add(105.7743099);
  pos.add(21.0717561);
  // or pos.addAll([105.7743099,21.0717561]);

  // Create a Point geometry from one position
  final geom_point = GeoJSONPoint(pos);

  // Create a Feature
  final feature_point = GeoJSONFeature(geom_point);
  feature_point.properties['marker-color'] = '#7e7e7e';
  feature_point.properties['marker-size'] = 'medium';
  feature_point.properties['marker-symbol'] = 'college';
  feature_point.properties['title'] = 'Hanoi University of Mining and Geology';
  feature_point.properties['department'] = 'Geoinformation Technology';
  feature_point.properties['address'] =
      'No.18 Vien Street - Duc Thang Ward - Bac Tu Liem District - Ha Noi';
  feature_point.properties['url'] = 'http://humg.edu.vn';

  // Add the feature to featureCollection
  geoJSON.featureCollection.features.add(feature_point);

  // ### Create a Feature with LineString geometry

  // LineString from 3 positions
  final pos1 = [105.7771289, 21.0715458];
  final pos2 = [105.7745218, 21.0715658];
  final pos3 = [105.7729125, 21.0715358];

  // Create a LineString geometry from array of position
  final geom_line_string = GeoJSONLineString([pos1, pos2, pos3]);

  // Create a Feature
  final feature_line_string = GeoJSONFeature(geom_line_string);
  feature_line_string.properties['stroke'] = '#7e7e7e';
  feature_line_string.properties['stroke-width'] = 2;
  feature_line_string.properties['stroke-opacity'] = 1;
  feature_line_string.properties['title'] = 'Vien St.';

  // Add the fearture to featureCollection
  geoJSON.featureCollection.features.add(feature_line_string);

  // ### Create a Feature with Polygon geometry

  // A linear ring is a closed LineString with four or more positions.
  // The first and last positions are equivalent, and they MUST contain
  // identical values; their representation SHOULD also be identical.
  final p01 = [105.7739666, 21.0726795]; // The first position
  final p02 = [105.7739719, 21.0721991];
  final p03 = [105.7743394, 21.0721966];
  final p04 = [105.7743310, 21.0725269];
  final p05 = [105.7742564, 21.0726120];
  final p06 = [105.7741865, 21.0726095];
  final p07 = [105.7741785, 21.0726746];
  final p08 = [105.7739666, 21.0726795]; // The last position

  //The exterior ring (boundary)
  final linerRing = [p01, p02, p03, p04, p05, p06, p07, p08];

  // Create a Polygon geometry from array of position
  final geom_polygon = GeoJSONPolygon([
    linerRing,
    // and others the interior rings (if present) bound holes within the surface
  ]);

  // Create a Feature
  final feature_polygon = GeoJSONFeature(geom_polygon);
  feature_polygon.properties['stroke'] = '#555555';
  feature_polygon.properties['stroke-width'] = 2;
  feature_polygon.properties['stroke-opacity'] = 1;
  feature_polygon.properties['fill'] = '#ab7942';
  feature_polygon.properties['fill-opacity'] = 0.5;
  feature_polygon.properties['title'] = 'HUMG\'s Office';

  // Add to featureCollection
  geoJSON.featureCollection.features.add(feature_polygon);

  await geoJSON.save();

  // # Read the GeoJSON file
  var launchTime = DateTime.now();
  await GeoJSON.load('example/data/parcels_82mb.geojson')
      .then((GeoJSON geoJSON) {
    print(geoJSON.featureCollection.features.length);
    print(DateTime.now().difference(launchTime));
  });

  // Calculate area of polygon
  await GeoJSON.load('example/data/polygon_with_holes.geojson').then((value) {
    value.featureCollection.features.forEach((element) {
      if (element.geometry.type == GeometryType.polygon) {
        GeoJSONPolygon pg = element.geometry;
        print('Area: ${pg.area}');
      }
    });
  });

  // Search by geometry and properties
  var polygonFeatures = geoJSON.featureCollection.features.where((element) {
    return (element.geometry.type == GeometryType.polygon &&
        (element.properties['title']).contains('HUMG'));
  }).toList();

  print(polygonFeatures.isNotEmpty ? polygonFeatures.first.toMap : 'not found');

  // # Read the GeoJSON file (cache applied)
  launchTime = DateTime.now();
  await GeoJSON.load('example/data/parcels_82mb.geojson')
      .then((GeoJSON geoJSON) {
    print(geoJSON.featureCollection.features.length);
    print(DateTime.now().difference(launchTime));
  });
}

Use this package as a library

1. Depend on it

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


dependencies:
  geojson_vi: ^1.2.2

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_vi/geojson_vi.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
47
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]
74
Learn more about scoring.

We analyzed this package on Jul 2, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.8.1 <3.0.0
universal_io ^1.0.1 1.0.1
Transitive dependencies
meta 1.1.8
zone_local 0.1.2
Dev dependencies
pedantic ^1.8.0
test ^1.6.0
test_coverage ^0.4.1