geokit/tile

Web Mercator (EPSG:3857) map tiles. At zoom level z the world is a grid of 2^z by 2^z tiles, with x growing east from the antimeridian and y growing south from the top edge at latitude 85.05112878. A quadkey writes a tile as one base-4 digit per zoom level.

Types

A tile at a zoom level. Build one with new, from_lat_lng or from_quadkey.

pub opaque type Tile

The reason a tile operation failed.

pub type TileError {
  ZoomOutOfRange(zoom: Int)
  CoordinateOutOfRange(zoom: Int, x: Int, y: Int)
  InvalidQuadkeyCharacter(character: String, position: Int)
}

Constructors

  • ZoomOutOfRange(zoom: Int)

    The zoom level was outside [0, 30].

  • CoordinateOutOfRange(zoom: Int, x: Int, y: Int)

    x or y was outside [0, 2^zoom).

  • InvalidQuadkeyCharacter(character: String, position: Int)

    A character other than 0, 1, 2 or 3, at a 0-based position.

Values

pub fn bounds(tile: Tile) -> geokit.BoundingBox

Returns the area covered by tile.

Examples

new(zoom: 1, x: 1, y: 0)
|> result.map(fn(tile) {
  let box = bounds(tile)
  #(geokit.latitude(box.south_west), geokit.longitude(box.south_west))
})
// -> Ok(#(0.0, 0.0))
pub fn from_lat_lng(
  point: geokit.LatLng,
  zoom zoom: Int,
) -> Result(Tile, TileError)

Returns the tile that contains point at zoom in [0, 30]. Latitudes beyond 85.05112878 degrees north or south map to the top or bottom row.

Examples

let tokyo = geokit.wrap_lat_lng(latitude: 35.6812, longitude: 139.7671)
from_lat_lng(tokyo, zoom: 10)
|> result.map(fn(tile) { #(x(tile), y(tile)) })
// -> Ok(#(909, 403))
from_lat_lng(tokyo, zoom: 31)
// -> Error(ZoomOutOfRange(zoom: 31))
pub fn from_quadkey(quadkey: String) -> Result(Tile, TileError)

Returns the tile named by quadkey. Its zoom is the length of the quadkey, at most 30, and the empty string is the zoom 0 tile.

Examples

from_quadkey("120") |> result.map(fn(tile) { #(x(tile), y(tile)) })
// -> Ok(#(4, 2))
from_quadkey("124")
// -> Error(InvalidQuadkeyCharacter(character: "4", position: 2))
pub fn new(
  zoom zoom: Int,
  x x: Int,
  y y: Int,
) -> Result(Tile, TileError)

Returns the tile at zoom in [0, 30] with x and y in [0, 2^zoom).

Examples

new(zoom: 1, x: 1, y: 0) |> result.map(to_quadkey)
// -> Ok("1")
new(zoom: 1, x: 2, y: 0)
// -> Error(CoordinateOutOfRange(zoom: 1, x: 2, y: 0))
pub fn to_quadkey(tile: Tile) -> String

Returns the quadkey of tile, one digit per zoom level. The zoom 0 tile has the empty quadkey.

Examples

new(zoom: 3, x: 4, y: 2) |> result.map(to_quadkey)
// -> Ok("120")
pub fn x(tile: Tile) -> Int

Returns the column of tile, counted east from the antimeridian.

Examples

from_quadkey("120") |> result.map(x)
// -> Ok(4)
pub fn y(tile: Tile) -> Int

Returns the row of tile, counted south from the top edge.

Examples

from_quadkey("120") |> result.map(y)
// -> Ok(2)
pub fn zoom(tile: Tile) -> Int

Returns the zoom level of tile.

Examples

from_quadkey("120") |> result.map(zoom)
// -> Ok(3)
✨ Search Document