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

GeoJSON drawn as `Macaw.Draw.IR.Path`s and measured in projected space (spec/09 §4),
copied from visualize's `Visualize.Geo.Path` and the projection step of its
`Visualize.Chart.Transform.Geo`.

    iex> alias Macaw.Draw.Geo.Path
    iex> {:ok, planar} = Path.new(projection: nil)
    iex> line = %{"type" => "LineString", "coordinates" => [[0, 0], [10, 5]]}
    iex> planar |> Path.path(line) |> Macaw.Draw.IR.Path.to_string()
    "M0,0L10,5"

## Clipping on the sphere

With a projection, lines and polygons are clipped on the sphere before they are
projected, as d3-geo clips them (§3, visualize D-127): under a projection with a
`clip_angle` they are cut where they cross the small circle of that radius about the
center, a line into several subpaths and a polygon closed along the circle; without one
they are cut where they cross ±180° of the rotated frame and a polygon is closed along
the seam and the poles. Winding is d3's: an exterior ring runs clockwise on a north-up
map, and a ring wound the other way is the rest of the sphere (decision 10).

## Bad data

A malformed part (a position that is not two numbers, coordinates of the wrong nesting,
an unknown `"type"`) draws nothing, measures 0.0 and contributes no positions; `check/1`
answers its faults at their paths (decision 11). A malformed position spoils the line,
or the polygon, that holds it; a multi-geometry keeps its other members.

# `error`

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

A refusal: faults with their paths.

# `t`

```elixir
@type t() :: %Macaw.Draw.Geo.Path{
  point_radius: number(),
  projection: Macaw.Draw.Geo.Projection.t() | nil
}
```

# `area`

```elixir
@spec area(t(), map()) :: float() | error()
```

The projected planar area (spec/09 §4.3): for a polygon `|shoelace(exterior)|` less
each hole's, summed over multi-polygons and collections; 0.0 for points and lines. The
rings are the geometry's own, each vertex placed by `Projection.project/3` and a clipped
one dropped, not the clipped rings of §3.

# `bounds`

```elixir
@spec bounds(t(), map()) :: [float()] | nil | error()
```

`[x0, y0, x1, y1]` over every vertex of the drawn path, the clipped lines and rings and
the edge samples included, or `nil` when nothing is drawn (spec/09 §4.3).

# `centroid`

```elixir
@spec centroid(t(), map()) :: {float(), float()} | nil | error()
```

The arithmetic mean of the drawn path's vertices (a point's center for a point), or
`nil` when nothing is drawn; not area-weighted (spec/09 §4.3).

# `check`

```elixir
@spec check(term()) :: :ok | error()
```

`:ok`, or the faults of every malformed part of a GeoJSON value at its path
(spec/09 §4.1, decision 11): `{:type, :geometry}` for a part that is no geometry map,
`{:unknown, :geometry, type}` for a `"type"` of none of §4.1's, `{:type, :coordinates}`
for coordinates of the wrong nesting and `{:type, :position}` for a position that is not
two numbers.

# `new`

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

A geo path, its options checked at once (spec/09 §4.1): `projection` (a `Projection`,
or `nil`, the default, to read positions as planar `x, y`) and `point_radius`
(non-negative, `4.5`).

# `path`

```elixir
@spec path(t(), map()) :: Macaw.Draw.IR.Path.t() | error()
```

The GeoJSON object as an IR path (spec/09 §4.2): a point as a circle of `point_radius`,
each piece of a clipped line as `M` and `L`s, each ring of a clipped polygon closed with
`Z`, the sphere as the projection's outline, the parts of multi-geometries, collections
and features concatenated; an empty path for nothing to draw. Its numbers are the
projected coordinates unrounded.

# `path_and_centroid`

```elixir
@spec path_and_centroid(t(), map()) ::
  {Macaw.Draw.IR.Path.t(), {float(), float()} | nil} | error()
```

`path/2` and `centroid/2` from one walk, so the clip runs once per row (spec/09 §4.2).

# `positions`

```elixir
@spec positions(term()) :: [{number(), number()}]
```

Every position of a GeoJSON value at any depth of its `coordinates`, `geometries`,
`geometry` or `features`, as `{lon, lat}`, a `nil` or non-numeric coordinate skipped
(spec/09 §2.6): what a frame fits a slot of geometries to.

# `project_geometries`

```elixir
@spec project_geometries(Macaw.Draw.Geo.Projection.t() | nil, [term()]) ::
  %{required(String.t()) =&gt; [term()]} | error()
```

The projection step's geometry columns (spec/09 §4.4): `%{"row", "path", "x", "y"}`, one
row per input row whose value is a GeoJSON map, with its projected path and centroid; a
geometry the projection clips whole keeps its row with `"path"`, `"x"` and `"y"` `nil`.
A `{lon, lat}` `:point` value of two numbers is placed as a point, `"path"` `nil`
(visualize #984, D-291). Any other value is left out.

# `project_points`

```elixir
@spec project_points(Macaw.Draw.Geo.Projection.t() | nil, [term()], [term()]) ::
  %{required(String.t()) =&gt; [term()]} | error()
```

The projection step's point columns (spec/09 §4.4): `%{"row", "x", "y"}`, one row per
input row; a point the projection clips keeps its row with `"x"` and `"y"` `nil`; a row
whose longitude or latitude is missing or not a number is left out. `nil` reads the
positions as planar.

---

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