geodart 0.0.2
geodart: ^0.0.2 copied to clipboard
A geospatial toolkit written in dart. Designed to make it easier to work with vector features.
Geodart #
A geospatial library for Dart. Designed primarily around vector features, this library provides a simple interface for working with geographic data.
It's based heavily on the geojson specification, but has been extended to add functionality directly to the feature types.
Getting started #
Add it to your dependencies:
dependencies:
geodart: '^0.0.1'
OR run this command to install it:
flutter pub add geodart
Then import it:
import 'package:geodart/features.dart';
import 'package:geodart/measure.dart';
Features #
The following features are included in this library:
The package also provides a Feature class, which is a
abstract class that can be extended to create new feature types.
Positions are stored using Coordinate objects, which has a variety of
convenience methods for working with coordinates.
Feature Collection #
A FeatureCollection is a collection of
Feature objects.
It functions very similarly to a List of Feature objects,
but might extended to include additional properties in the future.
Constructors
FeatureCollection.fromJson(Map<String, dynamic> json)- Creates aFeatureCollectionfrom a JSON object. Automatically converts features from GeoJSON to their respective types.
Methods
toJson()- Returns a JSON object representing theFeatureCollection. Automatically converts features to GeoJSON.
Properties
Point #
A Point is a single position. It is represented by a Coordinate object.
Constructors
Point.fromJson(Map<String, dynamic> json)- Creates aPointfrom aMapof GeoJSON data.Point.fromLngLat(num lng, num lat)- Creates aPointfrom anumlongitude and latitude.Point.fromWKT(String wkt)- Creates aPointfrom a Well-Known Text string.
Methods
explode()- Returns aListof itself.toJson()- Returns aMapof GeoJSON data.toWKT()- Returns a Well-Known Text string representing thePoint.
Properties
coordinates- TheCoordinateof thePoint.type- The type of thePoint. Always"Point".properties- AMapof properties.
Multi Point #
A MultiPoint is a collection of Coordinate objects, represented by individual and non-connected points.
Constructors
MultiPoint.fromJson(Map<String, dynamic> json)- Creates aMultiPointfrom aMapof GeoJSON data.MultiPoint.fromWKT(String wkt)- Creates aMultiPointfrom a Well-Known Text string.
Methods
explode()- Returns aListofPointobjects.toJson()- Returns aMapof GeoJSON data.toWKT()- Returns a Well-Known Text string representing theMultiPoint.union(MultiPoint other)- Merges theMultiPointwith anotherMultiPoint.flatten()- Returns aListofPointobjects.
Properties
coordinates- AListofCoordinateobjects.type- The type of theMultiPoint. Always"MultiPoint".properties- AMapof properties.
Line String #
A LineString is a collection of Coordinate objects that form a line.
Constructors
LineString.fromJson(Map<String, dynamic> json)- Creates aLineStringfrom aMapof GeoJSON data.LineString.fromWkt(String wkt)- Creates aLineStringfrom a Well-Known Text string.
Methods
along(distance)- Returns aPointat the specified distance along the line.explode()- Returns aListofPointobjects that make up the LineString.midpoint()- Returns aPointat the midpoint of the LineString.toJson()- Returns a GeoJSONMapof the LineString.toPolygon()- Returns aPolygonthat is the same as the LineString. LineString must be closed, or an exception will be thrown.toWKT()- Returns aStringof the LineString in WKT format.
Properties
coordinates- AListofCoordinateobjects that make up the LineString.type- The type of the LineString. Always"LineString".isClosedRing- A boolean indicating whether the LineString is a closed ring.length- The length (in meters) of the LineString.properties- AMapof properties.
Multi Line String #
A MultiLineString is a collection of Coordinate objects that form multiple separate LineStrings with one set of shared properties.
Constructor
MultiLineString.fromJson(Map<String, dynamic> json)- Creates aMultiLineStringfrom aMapof GeoJSON data.MultiLineString.fromWkt(String wkt)- Creates aMultiLineStringfrom a Well-Known Text string.
Methods
explode()- Returns aListofLineStringobjects.toJson()- Returns aMapof GeoJSON data.toWKT()- Returns a Well-Known Text string representing theMultiLineString.union(MultiLineString other)- Merges theMultiLineStringwith anotherMultiLineString.flatten()- Returns aListofLineStringobjects.
Properties
coordinates- A nestedListofCoordinateobjects.type- The type of theMultiLineString. Always"MultiLineString".properties- AMapof properties.
Polygon #
A Polygon is a collection of [LinearRing](#Linear Ring) objects that form a closed ring. The first LinearRing in the list is the outer ring, and any subsequent LinearRings are holes. Holes should be contained within the outer ring - if they are not, some algorithms may not work correctly. A Polygon should also not intersect itself - again, some algorithms may not work correctly if this is not the case.
Constructors
Polygon.fromJson(Map<String, dynamic> json)- Creates aPolygonfrom aMapof GeoJSON data.Polygon.fromWkt(String wkt)- Creates aPolygonfrom a Well-Known Text string.
Methods
explode()- Returns aListofPointobjects.toJson()- Returns aMapof GeoJSON data.toWKT()- Returns a Well-Known Text string representing thePolygon.toLineString()- Returns aLineStringthat is the same geometry as thePolygon.
Properties
coordinates- AListof [LinearRing](#Linear Ring) objects.type- The type of thePolygon. Always"Polygon".area- The are (in square meters) of the Polygon.properties- AMapof properties.
Multi Polygon #
A MultiPolygon is a collection of Polygon geometries forming one MultiPolygon with shared properties.
Constructors
MultiPolygon.fromJson(Map<String, dynamic> json)- Creates aMultiPolygonfrom aMapof GeoJSON data.MultiPolygon.fromWkt(String wkt)- Creates aMultiPolygonfrom a Well-Known Text string.
Methods
explode()- Returns aListofPointobjects.toJson()- Returns aMapof GeoJSON data.toWKT()- Returns a Well-Known Text string representing theMultiPolygon.union(MultiPolygon other)- Merges theMultiPolygonwith anotherMultiPolygon.flatten()- Returns aListofPolygonobjects.toMultiLineString()- Returns aMultiLineStringthat is the same geometry as theMultiPolygon.
Properties
coordinates- AListofPolygonobjects.type- The type of theMultiPolygon. Always"MultiPolygon".properties- AMapof properties.area- The are (in square meters) of the MultiPolygon.
Coordinate #
A Coordinate is a point in a two-dimensional Cartesian coordinate system.
Constructors
Coordinate.fromJson(Map<String, dynamic> json)- Creates aCoordinatefrom aMapof GeoJSON data.Coordinate.fromWkt(String wkt)- Creates aCoordinatefrom a Well-Known Text string.
Methods
toJson()- Returns aMapof GeoJSON data.toWKT()- Returns a Well-Known Text string representing theCoordinate.distanceTo(Coordinate other)- Returns the distance (in meters) between theCoordinateand anotherCoordinate.bearingTo(Coordinate other)- Returns the bearing (in degrees) between theCoordinateand anotherCoordinate.destination(num distance, num bearing)- Returns aCoordinatethat is the same geometry as theCoordinatebut moved a given distance and bearing.interpolate(Coordinate other, num fraction)- Returns aCoordinatethat is the same geometry as theCoordinatebut moved a given fraction of the distance and bearing to anotherCoordinate.
Properties
latitude- The latitude of theCoordinate.longitude- The longitude of theCoordinate.type- The type of theCoordinate. Always"Coordinate".
Usage #
Measure the distance between two points.
import 'package:geodart/measure.dart';
import 'package:geodart/features.dart';
double distanceBetween = distance(
Point.fromLngLat(1.0, 1.0),
Point.fromLngLat(2.0, 2.0),
);
Under the hood, this uses the coordinate distanceTo() function, and could be very easily replaced with a different algorithm.
I prefer the distanceTo() function because it is more explicit and easier to read.
import 'package:geodart/features.dart';
double distanceBetween = Coordinate(1.0, 1.0).distanceTo(Coordinate(2.0, 2.0));
Measure the area of a polygon.
import 'package:geodart/features.dart';
Polygon polygon = Polygon.fromJson(
{
'type': 'Polygon',
'coordinates': [
[
[1.0, 1.0],
[2.0, 1.0],
[2.0, 2.0],
[1.0, 1.0],
],
],
}
);
print(polygon.area);
Measure the length of a LineString.
LineString length is calculated on the fly.
import 'package:geodart/features.dart';
LineString lineString = LineString.fromJson(
{
'type': 'LineString',
'coordinates': [
[1.0, 1.0],
[2.0, 2.0],
],
}
);
print(lineString.length);
Additional information #
License #
This library is free software under the terms of the MIT license. See the LICENSE file for more details.
Contributing #
If you have any questions or comments, please open an issue on the Github repository.
If you want to make a pull request, please open a pull request on the Github repository. Please make sure to include a test suite. I make no promises that I will accept pull requests, but I will try my best to keep the code up to date.