View Source SphericalMercator (Spherical Mercator v1.0.0)

This is a port of MapBox's SphericalMercator JS library to Elixir.

The API remains similar, so refer to the original project for the documentation.

The code could be prettier but it aims at following the style of original implementation.

Summary

Types

Two-element list with coordinates in the form [x,y] or [long,lat].

Four-element list with coords bounding box in the form [w, s, e, n].

Projection, either "WGS84" or "900913".

t()

Four-element list with XYZ bounding box in the form [min_x, min_y, max_x, max_y].

Functions

Convert tile xyz value to bbox of the form [w, s, e, n]

Convert projection of given bbox.

Convert lon/lat values to 900913 x/y.

Convert 900913 x/y values to lon/lat.

Convert screen pixel value to lon lat

SphericalMercator constructor: precaches calculations for fast tile lookups.

Convert lon lat to screen pixel value.

Types

@type coords() :: [float()]

Two-element list with coordinates in the form [x,y] or [long,lat].

@type coordsbox() :: [float()]

Four-element list with coords bounding box in the form [w, s, e, n].

@type srs() :: String.t()

Projection, either "WGS84" or "900913".

@type t() :: %SphericalMercator{
  cache: Map.t(),
  expansion: pos_integer(),
  size: number()
}
@type xyzbox() :: [pos_integer()]

Four-element list with XYZ bounding box in the form [min_x, min_y, max_x, max_y].

Functions

Link to this function

bbox(sm, x, y, zoom, tms_style \\ false, srs \\ "WGS84")

View Source
@spec bbox(t(), pos_integer(), pos_integer(), number(), boolean(), srs()) ::
  coordsbox()

Convert tile xyz value to bbox of the form [w, s, e, n]

  • sm - a SphericalMercator struct
  • x - x (longitude) number
  • y - y (latitude) number
  • zoom - zoom
  • tms_style - whether to compute using tms-style
  • srs - projection for resulting bbox (WGS84|900913)

Returns a bbox list of values in form [w, s, e, n].

Link to this function

convert(sm, list, binary)

View Source
@spec convert(t(), coordsbox(), srs()) :: coordsbox()

Convert projection of given bbox.

  • bbox - bbox in the form [w, s, e, n].
  • to - projection of output bbox (WGS84|900913). Input bbox
     assumed to be the "other" projection.

Returns bbox with reprojected coordinates.

@spec forward(t(), coords()) :: coords()

Convert lon/lat values to 900913 x/y.

@spec inverse(t(), coords()) :: coords()

Convert 900913 x/y values to lon/lat.

@spec ll(t(), coords(), number()) :: coords()

Convert screen pixel value to lon lat

  • sm - a SphericalMercator struct
  • px - a list ([x, y]) array of geographic coordinates.
  • zoom - zoom level.
@spec new(Keyword.t()) :: t()

SphericalMercator constructor: precaches calculations for fast tile lookups.

@spec px(t(), coords(), number()) :: coords()

Convert lon lat to screen pixel value.

  • sm - a SphericalMercator struct
  • ll - a list ([lon, lat]) of geographic coordinates.
  • zoom - zoom level.
Link to this function

xyz(sm, bbox, zoom, tms_style \\ false, srs \\ "WGS84")

View Source
@spec xyz(t(), coordsbox(), number(), boolean(), srs()) :: xyzbox()

Convert bbox to xyx bounds

  • bbox - bbox in the form [w, s, e, n].
  • zoom - zoom.
  • tms_style - whether to compute using tms-style.
  • srs - projection of input bbox (WGS84|900913).

Returns XYZ bounding box with list [minX, maxX, minY, maxY].