geodart 0.3.9
geodart: ^0.3.9 copied to clipboard
A geospatial library for Dart. Designed primarily around vector features (like, with coordinate points), this library provides a simple interface for working with geographic data.
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.
Conversions #
You can convert between different units with the conversions library.
Types of conversions:
- Distance: conversions between meters, kilometers, and miles, etc.
- Area: conversions between square meters, square kilometers, and square miles, etc.
- Angle: conversions between degrees, radians, and gradians, etc.
Here's an example:
import 'package:geodart/conversions.dart';
// Ten miles to kilometers
convertDistance(10, DistanceUnits.miles, DistanceUnits.kilometers); // returns 16.09344
// Ten degrees to radians
convertAngle(10, AngleUnits.degrees, AngleUnits.radians); // returns 0.17453292519943295
// Ten acres to square miles
convertArea(10, AreaUnits.acres, AreaUnits.squareMiles); // returns 0.004046856
There are some static units that are available with the type of DistanceUnit, AngleUnit and AreaUnit. There are some preset units that are available with these types:
DistanceUnit #
The following are the available units for length and distance:
DistanceUnits.metersDistanceUnits.kilometersDistanceUnits.milesDistanceUnits.feetDistanceUnits.yardsDistanceUnits.nauticalMilesDistanceUnits.millimetersDistanceUnits.centimeters
AngleUnit #
The following are the available units for angles:
AngleUnits.degreesAngleUnits.radiansAngleUnits.gradiansAngleUnits.turnsAngleUnits.arcSecondsAngleUnits.arcMinutesAngleUnits.milliradians
AreaUnit #
The following are the available units for area:
AreaUnits.squareMetersAreaUnits.squareKilometersAreaUnits.squareMilesAreaUnits.acresAreaUnits.hectaresAreaUnits.squareFeetAreaUnits.squareYardsAreaUnits.squareInches
Geometries #
To use this library, you'll need to get familiar with the feature types. All other actions taken depend on using the proper feature types. These feature types are very similar to the ones used in the geojson specification.
I'll admit, the feature coordinates are a bit of a mess. I'm working on a better way to handle this.
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.
Bounding boxes (generally used as the minimum surrounding area of a Feature) are stored
using BoundingBox objects, which has a variety of convenience methods
for working with bounding boxes.
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.FeatureCollection.fromWKT(String wkt)- Creates aFeatureCollectionfrom a Well-Known Text string.
Methods
toJson()- Returns a JSON object representing theFeatureCollection. Automatically converts features to GeoJSON.nearestPointTo(Point point)- Returns the nearestPointin theFeatureCollectionto the given point.explode()- Returns aFeatureCollectionofPointobjects, one for each coordinate in each geometry in theFeatureCollection.isCollectionOf(String type)- Returns true if theFeatureCollectioncontains only features of the given type.
Properties
features- AListofFeatureobjects.type- The type of theFeatureCollection. Always"FeatureCollection".bbox- The [BoundingBox] of theFeatureCollection.envelope- APolygonrepresenting the envelope of theFeatureCollection.center- ThePointrepresenting the center of theFeatureCollection.isEmpty- Whether or not theFeatureCollectionis empty.convexHull- The smallestPolygonrepresenting the convex hull of theFeatureCollection.
Feature #
A Feature is an abstract class that can be extended to create new feature types.
Because this class is abstract, you cannot instantiate it directly.
Methods
explode()- Returns a [List] ofPointobjects, one for each coordinate in theFeature.toJson()- Returns a JSON object representing theFeature. Automatically converts features to GeoJSON.toWKT()- Returns a WKT string representing theFeature.
Properties
type- The type of theFeature.bbox- The [BoundingBox] of theFeature.center- ThePointrepresenting the center of theFeature.
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.Point.random()- Creates aPointat a random location within the [-180, 180] x [-90, 90] bounding box.
Methods
explode()- Returns aListof itself.toJson()- Returns aMapof GeoJSON data.toWKT()- Returns a Well-Known Text string representing thePoint.isContainedIn(Feature feature)- Returns true if thePointis contained within the givenPolygonorMultiPolygon.buffer(double distance, {DistanceUnit unit = DistanceUnits.meters, int steps = 40})- Returns aPolygonthat is the buffer of thePointof the given distance.
Properties
coordinates- TheCoordinateof thePoint.type- The type of thePoint. Always"Point".properties- AMapof properties.bbox- aBoundingBoxof thePoint.center- ThePointrepresenting the center of thePoint, which is same as the point itself.lat- The latitude of thePoint.lng- The longitude of thePoint.
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? multi, Point? point})- Merges theMultiPointwith anotherMultiPointorPoint.flatten()- Returns aListofPointobjects.
Properties
coordinates- AListofCoordinateobjects.type- The type of theMultiPoint. Always"MultiPoint".properties- AMapof properties.bbox- aBoundingBoxof theMultiPoint.center- ThePointrepresenting the center of theMultiPoint.
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.LineString.random({int length})- Creates aLineStringat a random location within the [-180, 180] x [-90, 90] bounding box with a given number of points (defaults to 2).
Methods
along(distance)- Returns aPointat the specified distance along the line.explode()- Returns aListofPointobjects that make up 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.pointAt(double percentage)- Returns aPointat the specified percentage along the line.reverse()- Returns aLineStringthat is the reverse of the original LineString.isParallelTo(LineString line)- Returnstrueif the LineString is parallel to the specified LineString, or all segments are parallel in order or reverse order.intersections(LineString line)- Returns aFeatureCollectionofPointobjects where the LineString intersects the specified LineString.contains(Point point)- Returnstrueif the LineString contains the specifiedPoint.
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.bbox- aBoundingBoxof theLineString.segments- AListofLineStringobjects that make up the LineString.midpoint- Returns aPointat the midpoint of the LineString.center- ThePointrepresenting the center of theLineString's coordinates.bearing- The bearing of theLineString. If more than 2 coordinates are provided, 0.0 is returned.slope- The slope of theLineString. If more than 2 coordinates are provided, it uses the first and last coordinates.
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.MultiLineString.random({int count, int length})- Creates aMultiLineStringat a random location within the [-180, 180] x [-90, 90] bounding box, with a line count (count) and a number of points in each line (length).
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.bbox- aBoundingBoxof theMultiLineString.
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.contains(Point point)- Returnstrueif thePolygoncontains the specifiedPoint.intersects(Polygon poly)- Returnstrueif thePolygonintersects the specifiedPolygon.
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.bbox- aBoundingBoxof thePolygon.
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? multi, Polygon? poly})- Merges theMultiPolygonwith anotherMultiPolygonand/orPolygon.flatten()- Returns aListofPolygonobjects.toMultiLineString()- Returns aMultiLineStringthat is the same geometry as theMultiPolygon.contains(Point point)- Returnstrueif theMultiPolygoncontains thePoint.intersects(poly: Polygon, multi: MultiPolygon)- Returnstrueif thisMultiPolygonintersects the passedPolygonorMultiPolygon.
Properties
coordinates- AListofPolygonobjects.type- The type of theMultiPolygon. Always"MultiPolygon".properties- AMapof properties.area- The are (in square meters) of the MultiPolygon.bbox- aBoundingBoxof theMultiPolygon.hasSelfIntersections- Returnstrueif any of the containedLinearRinggeometries intersect.
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.Coordinate.random()- Creates aCoordinateat a random location.
Methods
toJson()- Returns aMapof GeoJSON data.toWKT()- Returns a Well-Known Text string representing theCoordinate.toENU(Coordinate origin)- Returns the difference in 3 dimensional space (asList<double>[xEast, yNorth, zUp]) between theCoordinateand the passedCoordinateorigin.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".
LinearRing #
A [LinearRing](#Linear Ring) is a closed LineString that is closed because the first and last coordinate are the same.
Constructors
LinearRing.random()- Creates a [LinearRing](#Linear Ring) with 3 randomCoordinates.
Methods
toLineString()- Returns aLineStringthat is the same geometry as the [LinearRing](#Linear Ring).reverse()- Returns a [LinearRing](#Linear Ring) that is the same geometry as the [LinearRing](#Linear Ring) but in reverse order.explode()- Returns aFeatureCollectionofPointobjects.
Properties
coordinates- AListofCoordinateobjects.type- The type of the [LinearRing](#Linear Ring). Always"LinearRing".area- The are (in square meters) of the [LinearRing](#Linear Ring).centroid- TheCoordinatethat is the centroid of the [LinearRing](#Linear Ring).bbox- aBoundingBoxof the [LinearRing](#Linear Ring).
BoundingBox #
A BoundingBox is a rectangular area of the two-dimensional Cartesian coordinate system.
Generally, it is used to represent the smallest possible bounds of a Feature in lat/long space.
Constructors
BoundingBox.fromCoordinates(List<Coordinate> points)- Creates aBoundingBoxfrom aListofCoordinateobjects.BoundingBox.empty()- Creates an emptyBoundingBox.BoundingBox.fromPoints(List<Point> points)- Creates aBoundingBoxfrom aListofPointobjects.
Methods
toList()- Returns aListof GeoJSON data.toPolygon()- Returns aPolygonthat is the same geometry as theBoundingBox.
Properties
minLong- The minimum longitude of theBoundingBox.minLat- The minimum latitude of theBoundingBox.maxLong- The maximum longitude of theBoundingBox.maxLat- The maximum latitude of theBoundingBox.type- The type of theBoundingBox. Always"BoundingBox".center- TheCoordinatecenter of theBoundingBox.square- TheBoundingBox, but made square.
Usage #
Measure the distance between two points.
import 'package:geodart/measurements.dart';
import 'package:geodart/geometries.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/geometries.dart';
double distanceBetween = Coordinate(1.0, 1.0).distanceTo(Coordinate(2.0, 2.0));
Measure the area of a polygon.
import 'package:geodart/geometries.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/geometries.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.