# `Loxo`
[🔗](https://github.com/giggleton/loxo/blob/v0.1.0/lib/loxo.ex#L1)

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.

# `point`

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

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

# `reason`

```elixir
@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`

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

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

# `coordinate_system`

```elixir
@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!`

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

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

# `project`

```elixir
@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!`

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

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

---

*Consult [api-reference.md](api-reference.md) for complete listing*
