Visualize.Geo.Path (Visualize v0.2.25)

Copy Markdown View Source

Renders GeoJSON geometries as SVG path strings.

Takes GeoJSON data and a projection to produce SVG paths suitable for rendering maps.

Examples

projection = Visualize.Geo.Projection.new(:mercator)
  |> Visualize.Geo.Projection.scale(100)
  |> Visualize.Geo.Projection.translate(200, 150)

geo_path = Visualize.Geo.Path.new(projection)

# Render a GeoJSON feature
feature = %{
  "type" => "Feature",
  "geometry" => %{
    "type" => "Polygon",
    "coordinates" => [[[-122, 37], [-122, 38], [-121, 38], [-121, 37], [-122, 37]]]
  }
}

path_data = Visualize.Geo.Path.render(geo_path, feature)

Clipping on the sphere

With a projection, lines and polygons are clipped on the sphere before they are projected, as d3-geo clips them (spec/07 §2.2.1, D-127): under a projection with a clip_angle they are cut where they cross the small circle of that radius about the centre — the orthographic's horizon — a line into several subpaths and a polygon closed along the circle, so a globe never closes its land by a chord across the disc; under a projection without one they are cut where they cross ±180° of the rotated frame and a polygon is closed along the seam and the poles, so a rotated map never joins a ring the long way round. Winding is d3's: a polygon's exterior ring runs clockwise on a north-up map, and a ring wound the other way is the rest of the sphere.

Summary

Functions

Computes the projected planar area of a GeoJSON geometry.

Computes the bounding box of a GeoJSON geometry.

Computes the centroid of a GeoJSON geometry.

Creates a new GeoPath with the given projection

The GeoJSON object as a Visualize.IR.Path (spec/07 §2.2): the same walk as render/2, its numbers the projected coordinates unrounded, and an empty path for a geometry with nothing to draw.

Sets the radius for Point geometries

Sets the projection

Renders GeoJSON to an SVG path string.

Types

t()

@type t() :: %Visualize.Geo.Path{
  context: any(),
  point_radius: number(),
  projection: Visualize.Geo.Projection.t() | nil
}

Functions

area(geo_path, geojson)

@spec area(t(), map()) :: float()

Computes the projected planar area of a GeoJSON geometry.

bounds(geo_path, geojson)

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

Computes the bounding box of a GeoJSON geometry.

Returns [x0, y0, x1, y1] in projected coordinates.

centroid(geo_path, geojson)

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

Computes the centroid of a GeoJSON geometry.

Returns {x, y} in projected coordinates.

new(projection \\ nil)

@spec new(Visualize.Geo.Projection.t() | nil) :: t()

Creates a new GeoPath with the given projection

path(geo_path, geojson)

@spec path(t(), map()) :: Visualize.IR.Path.t()

The GeoJSON object as a Visualize.IR.Path (spec/07 §2.2): the same walk as render/2, its numbers the projected coordinates unrounded, and an empty path for a geometry with nothing to draw.

point_radius(geo_path, radius)

@spec point_radius(t(), number()) :: t()

Sets the radius for Point geometries

projection(geo_path, proj)

@spec projection(t(), Visualize.Geo.Projection.t()) :: t()

Sets the projection

render(geo_path, geojson)

@spec render(t(), map()) :: String.t()

Renders GeoJSON to an SVG path string.

Accepts GeoJSON Feature, FeatureCollection, or raw Geometry objects.