geokit

Coordinates, geometries and the math on them: great-circle distance, bearings, bounding boxes, centroids, Douglas-Peucker simplification and point-in-polygon tests. Latitudes and longitudes are in degrees. Planar operations treat longitude and latitude as x and y on a flat plane and do not handle the antimeridian.

The encodings live in their own modules:

Types

An axis-aligned box given by its south-west and north-east corners.

pub type BoundingBox {
  BoundingBox(south_west: LatLng, north_east: LatLng)
}

Constructors

A shape made of positions. Rings of a Polygon are the exterior first, then the holes. Rings may be closed (first position repeated at the end) or open.

pub type Geometry {
  Point(point: LatLng)
  MultiPoint(points: List(LatLng))
  LineString(points: List(LatLng))
  MultiLineString(lines: List(List(LatLng)))
  Polygon(rings: List(List(LatLng)))
  MultiPolygon(polygons: List(List(List(LatLng))))
}

Constructors

  • Point(point: LatLng)
  • MultiPoint(points: List(LatLng))
  • LineString(points: List(LatLng))
  • MultiLineString(lines: List(List(LatLng)))
  • Polygon(rings: List(List(LatLng)))
  • MultiPolygon(polygons: List(List(List(LatLng))))

A position on the Earth in degrees. Build one with lat_lng or wrap_lat_lng.

pub opaque type LatLng

The reason lat_lng rejected its input.

pub type LatLngError {
  LatitudeOutOfRange(latitude: Float)
  LongitudeOutOfRange(longitude: Float)
}

Constructors

  • LatitudeOutOfRange(latitude: Float)

    The latitude was outside [-90, 90] or was NaN.

  • LongitudeOutOfRange(longitude: Float)

    The longitude was outside [-180, 180] or was NaN.

Where a point lies relative to a polygon.

pub type Location {
  Inside
  OnBoundary
  Outside
}

Constructors

  • Inside

    Inside the exterior ring and outside every hole.

  • OnBoundary

    On an edge or a vertex of any ring.

  • Outside

    Outside the exterior ring, or inside a hole.

Values

pub fn bounding_box(
  geometry: Geometry,
) -> Result(BoundingBox, Nil)

Returns the smallest box that contains every position of the geometry. Returns Error(Nil) when the geometry has no positions. A geometry that crosses the antimeridian gets a box spanning almost every longitude.

Examples

let a = wrap_lat_lng(latitude: 35.0, longitude: 139.0)
let b = wrap_lat_lng(latitude: 36.0, longitude: 140.0)
bounding_box(LineString([a, b]))
// -> Ok(BoundingBox(south_west: a, north_east: b))
bounding_box(MultiPoint([]))
// -> Error(Nil)
pub fn centroid(geometry: Geometry) -> Result(LatLng, Nil)

Returns the centroid on the longitude/latitude plane. A Point is its own centroid. MultiPoint, LineString and MultiLineString give the mean of their vertices. A Polygon gives the area centroid with holes subtracted, and a MultiPolygon weights its polygons by area. A polygon with zero area falls back to the mean of its exterior vertices, and a MultiPolygon with zero total area to the mean of its polygons’ centroids. Returns Error(Nil) when there is nothing to average, such as an empty list or a polygon without an exterior ring.

Examples

let ring = [
  wrap_lat_lng(latitude: 0.0, longitude: 0.0),
  wrap_lat_lng(latitude: 0.0, longitude: 4.0),
  wrap_lat_lng(latitude: 2.0, longitude: 4.0),
  wrap_lat_lng(latitude: 2.0, longitude: 0.0),
  wrap_lat_lng(latitude: 0.0, longitude: 0.0),
]
centroid(Polygon([ring])) |> result.map(longitude)
// -> Ok(2.0)
centroid(LineString([]))
// -> Error(Nil)
pub fn distance(from from: LatLng, to to: LatLng) -> Float

Returns the great-circle distance in meters on a sphere of radius 6,371,008.8 m. The central angle comes from the Vincenty formula for a sphere, which keeps its precision at every distance, including antipodal points where the haversine formula loses centimeters.

Examples

let tokyo = wrap_lat_lng(latitude: 35.6812, longitude: 139.7671)
let osaka = wrap_lat_lng(latitude: 34.6937, longitude: 135.5023)
distance(from: tokyo, to: osaka) |> float.round
// -> 402_785
pub fn final_bearing(from from: LatLng, to to: LatLng) -> Float

Returns the direction of travel when arriving at to on the great-circle route from from, in degrees clockwise from true north, in [0, 360).

Examples

let tokyo = wrap_lat_lng(latitude: 35.6812, longitude: 139.7671)
let osaka = wrap_lat_lng(latitude: 34.6937, longitude: 135.5023)
final_bearing(from: tokyo, to: osaka) |> float.round
// -> 253
pub fn initial_bearing(from from: LatLng, to to: LatLng) -> Float

Returns the direction of travel when leaving from on the great-circle route to to, in degrees clockwise from true north, in [0, 360).

Examples

let tokyo = wrap_lat_lng(latitude: 35.6812, longitude: 139.7671)
let osaka = wrap_lat_lng(latitude: 34.6937, longitude: 135.5023)
initial_bearing(from: tokyo, to: osaka) |> float.round
// -> 255
pub fn lat_lng(
  latitude latitude: Float,
  longitude longitude: Float,
) -> Result(LatLng, LatLngError)

Returns a position for a latitude in [-90, 90] and a longitude in [-180, 180]. Returns an error naming the first value out of range.

Examples

lat_lng(latitude: 35.6812, longitude: 139.7671)
|> result.map(latitude)
// -> Ok(35.6812)
lat_lng(latitude: 91.0, longitude: 0.0)
// -> Error(LatitudeOutOfRange(latitude: 91.0))
pub fn latitude(point: LatLng) -> Float

Returns the latitude in degrees, in [-90, 90].

Examples

wrap_lat_lng(latitude: 35.0, longitude: 139.0) |> latitude
// -> 35.0
pub fn locate(
  point: LatLng,
  in geometry: Geometry,
) -> Result(Location, Nil)

Returns where point lies relative to a Polygon or MultiPolygon. A point on an edge or vertex of any ring, holes included, is OnBoundary, and a point inside a hole is Outside. For a MultiPolygon the answer is Inside if any polygon has the point inside, otherwise OnBoundary if any has it on its boundary. Edges are straight lines on the longitude/latitude plane. Returns Error(Nil) for the other geometries.

Coordinates are only subtracted and multiplied, so the answer is exact for whole-degree coordinates. Elsewhere a point within rounding error of an edge may be reported on either side of it.

Examples

let square = Polygon([[
  wrap_lat_lng(latitude: 0.0, longitude: 0.0),
  wrap_lat_lng(latitude: 0.0, longitude: 10.0),
  wrap_lat_lng(latitude: 10.0, longitude: 10.0),
  wrap_lat_lng(latitude: 10.0, longitude: 0.0),
]])
locate(wrap_lat_lng(latitude: 5.0, longitude: 5.0), in: square)
// -> Ok(Inside)
locate(wrap_lat_lng(latitude: 0.0, longitude: 5.0), in: square)
// -> Ok(OnBoundary)
let point = wrap_lat_lng(latitude: 0.0, longitude: 0.0)
locate(point, in: Point(point))
// -> Error(Nil)
pub fn longitude(point: LatLng) -> Float

Returns the longitude in degrees, in [-180, 180].

Examples

wrap_lat_lng(latitude: 35.0, longitude: 139.0) |> longitude
// -> 139.0
pub fn simplify(
  geometry: Geometry,
  tolerance tolerance: Float,
) -> Result(Geometry, Nil)

Returns the geometry with Douglas-Peucker applied to every line and ring. tolerance is a distance in degrees on the longitude/latitude plane: a vertex is dropped when it lies within tolerance of the chord between the vertices kept around it, so 0.0 removes only collinear vertices. Every line keeps its first and last vertex, and a closed ring stays closed with at least four positions. Points are returned unchanged. Returns Error(Nil) when tolerance is negative.

Examples

let a = wrap_lat_lng(latitude: 0.0, longitude: 0.0)
let b = wrap_lat_lng(latitude: 0.0, longitude: 0.5)
let c = wrap_lat_lng(latitude: 0.0, longitude: 1.0)
simplify(LineString([a, b, c]), tolerance: 0.001)
// -> Ok(LineString([a, c]))
simplify(LineString([]), tolerance: -1.0)
// -> Error(Nil)
pub fn wrap_lat_lng(
  latitude latitude: Float,
  longitude longitude: Float,
) -> LatLng

Returns a position for any input by clamping the latitude to [-90, 90] and wrapping the longitude into [-180, 180].

Examples

let point = wrap_lat_lng(latitude: 91.0, longitude: 181.0)
#(latitude(point), longitude(point))
// -> #(90.0, -179.0)

Target specific behavior

Only JavaScript can produce infinite and NaN floats. There, an infinite latitude clamps to the pole on its side, a NaN latitude becomes 0.0 and an infinite or NaN longitude becomes 0.0.

✨ Search Document