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

The arithmetic of a slippy-map basemap (spec/09 §7, visualize #475), copied from
visualize's `Visualize.Geo.Tiles`: whether a projection has tiles, the zoom level whose
256-pixel tiles match it, the tiles covering a plot, where each goes in plot pixels, and
the values of its address.

**It never fetches anything, and a tile is an asset, never data.** The tile layer kind
declares a proxy route whose URL template is the design's; for each tile `cover/3`
answers, the Frame asks `Macaw.Component.Proxy.path(component, "tiles",
Tiles.values(tile, subdomains))` for the same-origin path an `<image href>` uses, in
every output it writes (§7.5). visualize's `url/3`, which put a provider's URL in the
page, is not carried over.

    iex> alias Macaw.Draw.Geo.{Projection, Tiles}
    iex> {:ok, p} = Projection.new(:mercator, scale: 256 / (2 * :math.pi()), translate: {128, 128})
    iex> Tiles.zoom(p)
    0
    iex> {:ok, [tile]} = Tiles.cover(p, {256, 256})
    iex> {tile.z, tile.x, tile.y, Float.round(tile.left, 6), Float.round(tile.size, 6)}
    {0, 0, 0, 0.0, 256.0}
    iex> Tiles.values(tile, ["a", "b"])
    %{"s" => "a", "x" => "0", "y" => "0", "z" => "0"}

# `error`

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

A refusal: faults with their paths.

# `tile`

```elixir
@type tile() :: %{
  z: non_neg_integer(),
  x: non_neg_integer(),
  y: non_neg_integer(),
  left: float(),
  top: float(),
  size: float()
}
```

One tile of the covering set: its address and its square in plot pixels.

# `aligned`

```elixir
@spec aligned(Macaw.Draw.Geo.Projection.t()) ::
  :ok | {:error, {:type, atom()} | :center | :rotate} | error()
```

`:ok` when the projection is tile-aligned (spec/09 §7.1): `:mercator`, a zero center
latitude, no tilt and no roll; else `{:error, reason}`, checked in that order.

    iex> {:ok, o} = Macaw.Draw.Geo.Projection.new(:orthographic)
    iex> Macaw.Draw.Geo.Tiles.aligned(o)
    {:error, {:type, :orthographic}}

# `check_template`

```elixir
@spec check_template(String.t(), [String.t()]) :: :ok | error()
```

What the tile layer kind runs when its document compiles (spec/09 §7.4): `:ok`, or
every fault of the template at once: `{[:subdomains], :required}` for `{s}` with no
subdomains, `{[:url], {:placeholder, name}}` for a placeholder other than `{z}`, `{x}`,
`{y}` and `{s}`. Every other character is the provider's own and is not read here.

# `cover`

```elixir
@spec cover(Macaw.Draw.Geo.Projection.t(), {number(), number()}, keyword() | map()) ::
  {:ok, [tile()]}
  | {:error, {:type, atom()} | :center | :rotate}
  | {:error, {:too_many_tiles, pos_integer() | :unbounded}}
  | error()
```

The tiles covering a plot of `{width, height}` pixels (spec/09 §7.3), in row-then-column
order: `{:ok, tiles}`, each with its wrapped column `x`, its row `y` and its square in
plot pixels; `{:error, reason}` of `aligned/1` for a projection that has no tiles, and
`{:error, {:too_many_tiles, n}}` for a cover of more than `max_tiles`. Options
`max_zoom` (`19`) and `max_tiles` (a positive integer, `1024`, decision 8).

# `values`

```elixir
@spec values(%{z: integer(), x: integer(), y: integer()}, [String.t()]) ::
  %{required(String.t()) =&gt; String.t()} | error()
```

The values the proxy route's placeholders take for a tile (spec/09 §7.4): `"z"`, `"x"`
(wrapped) and `"y"` as decimal integers, and, when `subdomains` is non-empty, `"s"`,
`subdomains[rem(x + y, n)]` (Leaflet's rule, decision 9).

# `zoom`

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

The zoom level whose 256-pixel tiles are nearest the projection's scale (spec/09 §7.2):
`round(log2(2πk / 256))`, half away from zero, clamped to `[0, max_zoom]`. Option
`max_zoom` (a non-negative integer, `19`).

---

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