# `Macaw.Draw.Geo.Projection`
[🔗](https://github.com/dcoai/macaw_draw/blob/v0.1.0/lib/macaw/draw/geo/projection.ex#L1)

Map projections: `{longitude, latitude}` degrees to `{x, y}` pixels and back
(spec/09 §2), copied from visualize's `Visualize.Geo.Projection`.

A projection is built by `new/2`, its type an atom of `types/0` or the string of its
spelling, every option checked at once; the setters (`scale/2`, `translate/3`, …) change
one field of a checked projection, for the viewport's interactive transforms (§8.3), and
answer `{:error, faults}` for a value of the wrong type (spec/09 decision 1). The raw
mathematics of each type is in one of five internal family modules (§2.2).

    iex> alias Macaw.Draw.Geo.Projection
    iex> {:ok, p} = Projection.new("equirectangular", scale: 100, translate: {0, 0})
    iex> Projection.project(p, 90, 0)
    {157.07963267948966, -0.0}

`project/3` is rotation (§2.4), the center subtracted, the clip test (§2.5), the raw
projection, scale and translate with y flipped so north is up (§2.3); `invert/3` the
reverse, without the clip test. `bounds/1`, `sphere/1`, `fit_extent/3` and `fit/4` are
§2.6's.

# `error`

```elixir
@type error() :: {:error, [Macaw.Base.Fault.t()]}
```

A refusal: faults with their paths.

# `projection_type`

```elixir
@type projection_type() :: atom()
```

A projection type: one of `types/0`.

# `t`

```elixir
@type t() :: %Macaw.Draw.Geo.Projection{
  center: {number(), number()},
  clip_angle: number() | nil,
  parallels: {number(), number()},
  precision: number(),
  rotate: {number(), number(), number()},
  scale: number(),
  translate: {number(), number()},
  type: projection_type()
}
```

# `bounds`

```elixir
@spec bounds(t()) :: [number()] | error()
```

The extent `[x0, y0, x1, y1]` of the corners `{±180, ±85}` that project; every corner
clipped answers `[tx - scale, ty - scale, tx + scale, ty + scale]` (spec/09 §2.6).

# `center`

```elixir
@spec center(t(), number(), number()) :: t() | error()
```

Sets the center, `{lon, lat}` degrees subtracted after rotation (§2.4).

# `clip_angle`

```elixir
@spec clip_angle(t(), number() | nil) :: t() | error()
```

Sets the clip angle: a number in `(0, 180]`, or `nil` for none (§2.5).

# `family`

```elixir
@spec family(term()) :: {:ok, module()} | :error
```

The internal family module of a type: `{:ok, module}`, or `:error` for anything that
is not one of `types/0` (spec/09 §2).

# `fit`

```elixir
@spec fit(t(), [[number()]], [{number(), number()}], keyword() | map()) ::
  t() | error()
```

The projection-aware fit, d3-geo's `fitExtent` over `{lon, lat}` points (spec/09 §2.6,
visualize #475): `scale` and `translate` set so the points' projected bounds fill the
pixel box `[[x0, y0], [x1, y1]]` on the tighter axis, centered in it. `center`, `rotate`,
`clip_angle` and `parallels` are untouched, so a fitted Mercator stays tile-aligned.

Options: `padding` (pixels, `0`), the box inset on every side; `min_span` (meters, `0`):
above 0, four points about the center of the points' bounds widen them to at least
that span on both axes (visualize #828, D-212). A point that is no `{lon, lat}` of
numbers is skipped; with no projectable point the projection is answered unchanged.

    iex> alias Macaw.Draw.Geo.Projection
    iex> {:ok, p} = Projection.new(:mercator)
    iex> p = Projection.fit(p, [[0, 0], [100, 100]], [{-10, 0}, {10, 0}])
    iex> {x0, _} = Projection.project(p, -10, 0)
    iex> {x1, _} = Projection.project(p, 10, 0)
    iex> {Float.round(x0, 6), Float.round(x1, 6), p.center}
    {0.0, 100.0, {0, 0}}

# `fit_extent`

```elixir
@spec fit_extent(t(), [[number()]], [[number()]]) :: t() | error()
```

The approximate, projection-independent fit (spec/09 §2.6): `scale` to
`min(width / Δlon, height / Δlat) · 0.95` (deltas in radians), `center` to the
geographic midpoint and `translate` to the extent's midpoint; `rotate` untouched. A
geographic box with no extent on either axis does not bound the scale; with none on
both the scale is kept.

# `invert`

```elixir
@spec invert(t(), number(), number()) :: {float(), float()} | nil | error()
```

The `{lon, lat}` degrees at pixel `{x, y}`, or `nil` where the raw inverse answers none
(spec/09 §2.3): the inverse of scale and translate, the raw inverse, the center added
and the inverse rotation. The clip test is not applied.

# `new`

```elixir
@spec new(projection_type() | String.t(), keyword() | map()) :: {:ok, t()} | error()
```

A projection of `type`, every option checked at once (spec/09 §2.1): `{:ok, t}` or
`{:error, faults}`.

`type` is an atom of `types/0` or its spelling; an unknown one is
`{[:type], {:unknown, :projection, spelling}}`. Options, as a keyword list or a map with
atom or string keys: `scale` (positive, `150`), `translate` (`{x, y}`, `{480, 250}`),
`center` (`{lon, lat}`, `{0, 0}`), `rotate` (`{lambda, phi}` or `{lambda, phi, gamma}`
degrees, `{0, 0, 0}`), `clip_angle` (a number in `(0, 180]` or `nil`, the type's
default), `precision` (non-negative, `0.5`) and `parallels` (`{phi1, phi2}`,
`{29.5, 45.5}`). A pair may be a two-element list. `new/0` is `new(:mercator, [])`.

# `parallels`

```elixir
@spec parallels(t(), number(), number()) :: t() | error()
```

Sets the standard parallels of the conic types (`phi1` alone for `:bonne` and
`:loximuthal`), latitudes strictly between −90 and 90; one outside is
`{[n], {:type, :latitude}}` at its position.

# `precision`

```elixir
@spec precision(t(), number()) :: t() | error()
```

Sets the precision, a non-negative number (§2.1): `0` draws an edge a path is clipped
along straight between the clip's own points; any other value samples it every degree.

# `project`

```elixir
@spec project(t(), number(), number()) :: {float(), float()} | nil | error()
```

The pixel position of `{lon, lat}` degrees, or `nil` when the point is clipped or the
raw projection cannot place it (spec/09 §2.3). Output is floats.

# `rotate`

```elixir
@spec rotate(t(), number(), number(), number()) :: t() | error()
```

Sets the rotation `{lambda, phi, gamma}` in degrees: d3-geo's `projection.rotate` at
`[-lambda, phi, gamma]` (spec/09 §2.4). `lambda` is the central meridian, `phi` tilts
the globe toward or away from the viewer, `gamma` rolls it; `rotate/3` sets gamma 0.

# `scale`

```elixir
@spec scale(t(), number()) :: t() | error()
```

Sets the scale, a positive number.

# `sphere`

```elixir
@spec sphere(t()) :: [{float(), float()}] | error()
```

The projection's outline as a closed list of `{x, y}` pixels, d3's `{type: "Sphere"}`
(spec/09 §2.6): with a `clip_angle`, the small circle at that distance from the center
at 360 bearings, projected without the clip test; without one, the antimeridian on either
side and the two poles every degree, at ±85 for the Mercators and ±90 otherwise. A point
the raw projection cannot place is dropped.

# `translate`

```elixir
@spec translate(t(), number(), number()) :: t() | error()
```

Sets the translation, the pixel position of the raw origin.

# `types`

```elixir
@spec types() :: [projection_type(), ...]
```

The 38 projection types in family order (spec/09 §2.2): the list a document's type is
read by.

---

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