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

A geographic view, a projection and a plot size, as the `:point` box window of
`macaw_data` and back, and the pan and zoom gestures that move it (spec/09 §8).

A box is `%{west, south, east, north}` in degrees, latitudes in `[−90, 90]` with
`south ≤ north`, longitudes in `[−180, 180]`, `west` east of `east` when the box crosses
the antimeridian: the window `macaw_data` serves for a `:point` window field
(`Macaw.Data.Source.Request.box?/1`).

The sampled box of a projection that has no tiles is visualize's `Projection.box/2`
(visualize #1098, D-292), padded outward by one grid step (decision 7); the exact box of
a tile-aligned Mercator, `fit_box/4`, `pan/2` and `zoom/4` are new.

    iex> alias Macaw.Draw.Geo.{Projection, Viewport}
    iex> {:ok, p} = Projection.new(:equirectangular, scale: 800, rotate: {-170, 0}, translate: {300, 150})
    iex> {:ok, %{west: w, east: e}} = Viewport.box(p, {600, 300})
    iex> {round(w), round(e)}
    {167, -147}

# `box`

```elixir
@type box() :: %{west: number(), south: number(), east: number(), north: number()}
```

A `:point` box window.

# `error`

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

A refusal: faults with their paths.

# `box`

```elixir
@spec box(Macaw.Draw.Geo.Projection.t(), {number(), number()}, keyword() | map()) ::
  {:ok, box()} | {:error, :empty} | error()
```

The box a plot of `{width, height}` pixels shows through `projection` (spec/09 §8.1):
`{:ok, box}`, or `{:error, :empty}` when nothing of the sphere is in view.

A tile-aligned projection is answered exactly from its plane; any other is sampled on a
33 × 33 grid of pixels, each kept only when it round-trips within half a pixel, with a
pole in view spanning every longitude, longitudes covered by the shortest arc, and the
box padded outward by one grid step. Option `margin` (degrees, `0`) widens it further on
every side.

# `fit_box`

```elixir
@spec fit_box(
  Macaw.Draw.Geo.Projection.t(),
  {number(), number()},
  box(),
  keyword() | map()
) ::
  Macaw.Draw.Geo.Projection.t() | error()
```

The view that shows `box` in a plot of `{width, height}` (spec/09 §8.2): `Projection.fit/4`
over the box's corners and edge midpoints, `east + 360` when it crosses the antimeridian,
with `padding` as `fit/4` takes it. A tile-aligned projection first has its central
meridian moved to the box's middle longitude by `rotate`'s lambda, so a crossing box is
shown whole.

# `pan`

```elixir
@spec pan(Macaw.Draw.Geo.Projection.t(), {number(), number()}) ::
  Macaw.Draw.Geo.Projection.t() | error()
```

The view panned by `{dx, dy}` pixels (spec/09 §8.3): a flat map's `translate` moved by
it; a globe (a projection with a `clip_angle`) turned under the pointer, `lambda`
decreased by `dx · 180 / (π · scale)` and `phi` increased by `dy · 180 / (π · scale)`,
clamped to `[−90, 90]`.

# `zoom`

```elixir
@spec zoom(
  Macaw.Draw.Geo.Projection.t(),
  number(),
  {number(), number()},
  keyword() | map()
) ::
  Macaw.Draw.Geo.Projection.t() | error()
```

The view zoomed by `k` about the pixel `{px, py}` (spec/09 §8.3): `scale` multiplied by
`k`, clamped to `scale_range` (`{1, 1.0e9}`) and, with `snap: true` on a tile-aligned
projection, moved to the nearest scale whose world width `2π · scale` is `256 · 2^z`; a
flat map's `translate` moved so the point under the pixel stays fixed, a globe's center
fixed.

---

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