Macaw.Draw.Geo.Viewport (macaw_draw v0.1.0)

Copy Markdown View Source

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}

Summary

Types

A :point box window.

A refusal: faults with their paths.

Functions

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.

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.

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].

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.

Types

box()

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

A :point box window.

error()

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

A refusal: faults with their paths.

Functions

box(projection, size, opts \\ [])

@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(projection, size, box, opts \\ [])

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(proj, delta)

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(projection, k, pointer, opts \\ [])

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.