Map projection for Elixir: points carried from one coordinate system to another, through the proj4rs crate in a precompiled native library, so nothing needs installing.

A coordinate system is named by its EPSG code, such as 4326 for longitude and latitude on the WGS 84 datum or 3857 for Web Mercator, or written as a proj string, such as "+proj=ortho +lon_0=-80". A point is a pair of numbers in the first system's units, longitude before latitude, in degrees, where it is longitude and latitude, and comes back as a pair of floats in the second's. A point that does not project, such as one beyond an orthographic projection's horizon, comes back as nil, and the others are projected all the same.

iex> {:ok, [{x, y}, beyond]} =
...>   Loxo.project([{-79.0, 35.5}, {120.0, 35.0}],
...>     from: 4326,
...>     to: "+proj=ortho +lon_0=-80"
...>   )
iex> {round(x), round(y), beyond}
{90725, 3683168, nil}

A system loxo cannot read makes the whole call an error: an EPSG code with no system, a proj string it cannot read, or a datum shift that needs a grid file, as NAD27's (4267) does, which loxo does not carry.

Summary

Types

A point: a pair of numbers in a coordinate system's units, longitude first in degrees.

Why a coordinate system could not be read: {kind, definition, detail}, kind one of :unknown_epsg_code, :needs_datum_grid and :invalid_definition, definition the code or string given, and detail the native library's own words.

A coordinate system: its EPSG code, a proj string, or one read already.

Functions

The coordinate system definition names, an EPSG code or a proj string, read once, for project/2 to take in its place: {:ok, system}, or {:error, reason}.

As coordinate_system/1, raising Loxo.Error for a system it cannot read.

Each of points carried from the system from: to the system to:, in order: {:ok, projected}, a point that does not project nil in its place, or {:error, reason} where a system cannot be read.

As project/2, returning the projected points and raising Loxo.Error where it errs.

Types

point()

@type point() :: {number(), number()}

A point: a pair of numbers in a coordinate system's units, longitude first in degrees.

reason()

@type reason() ::
  {:unknown_epsg_code | :needs_datum_grid | :invalid_definition,
   pos_integer() | String.t(), String.t()}

Why a coordinate system could not be read: {kind, definition, detail}, kind one of :unknown_epsg_code, :needs_datum_grid and :invalid_definition, definition the code or string given, and detail the native library's own words.

system()

@type system() :: pos_integer() | String.t() | Loxo.CoordinateSystem.t()

A coordinate system: its EPSG code, a proj string, or one read already.

Functions

coordinate_system(code)

@spec coordinate_system(pos_integer() | String.t()) ::
  {:ok, Loxo.CoordinateSystem.t()} | {:error, reason()}

The coordinate system definition names, an EPSG code or a proj string, read once, for project/2 to take in its place: {:ok, system}, or {:error, reason}.

iex> {:ok, system} = Loxo.coordinate_system(3857)
iex> {system.definition, system.longitude_latitude}
{3857, false}
iex> {:error, {kind, 4267, _detail}} = Loxo.coordinate_system(4267)
iex> kind
:needs_datum_grid

coordinate_system!(definition)

@spec coordinate_system!(pos_integer() | String.t()) :: Loxo.CoordinateSystem.t()

As coordinate_system/1, raising Loxo.Error for a system it cannot read.

project(points, options)

@spec project([point()], keyword()) ::
  {:ok, [{float(), float()} | nil]} | {:error, reason()}

Each of points carried from the system from: to the system to:, in order: {:ok, projected}, a point that does not project nil in its place, or {:error, reason} where a system cannot be read.

Options

  • :from - Required. The system the points are in: an EPSG code, a proj string, or one read.

  • :to - Required. The system to carry them to, given as from: is.

    iex> {:ok, [{x, y}]} = Loxo.project([{0, 0}], from: 4326, to: 3857) iex> {x, y} {0.0, 0.0}

Raises ArgumentError for points that are not a list of pairs of numbers, and NimbleOptions.ValidationError for a missing or unknown option or a system that is neither a positive integer, a string nor one read.

project!(points, options)

@spec project!([point()], keyword()) :: [{float(), float()} | nil]

As project/2, returning the projected points and raising Loxo.Error where it errs.