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

Copy Markdown View Source

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.

Summary

Types

A refusal: faults with their paths.

A projection type: one of types/0.

t()

Functions

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

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

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

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

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.

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.

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.

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

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.

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.

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.

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.

Sets the scale, a positive number.

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.

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

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

Types

error()

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

A refusal: faults with their paths.

projection_type()

@type projection_type() :: atom()

A projection type: one of types/0.

t()

@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()
}

Functions

bounds(proj)

@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(proj, lon, lat)

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

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

clip_angle(proj, a)

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

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

family(type)

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

@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(proj, extent, geo)

@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(proj, x, y)

@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(type \\ :mercator, opts \\ [])

@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(proj, phi1, phi2)

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

@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(proj, lon, lat)

@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(proj, lambda, phi, gamma \\ 0)

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

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

Sets the scale, a positive number.

sphere(proj)

@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(proj, x, y)

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

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

types()

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

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