| Title: | Tile Math for Map Tile Pyramids |
|---|---|
| Description: | Pure tile math for map tile pyramids. Provides coordinate conversions between longitude/latitude, Mercator metres, pixel coordinates, and tile numbers; quadkey encoding and decoding; TMS y-axis flipping; URL templating; and convenience helpers for selecting tiles from an extent or a point with automatic zoom selection. The default pyramid is web Mercator but the internal structure leaves room for longlat and arbitrary raster pyramids. |
| Authors: | Michael Sumner [aut, cre] |
| Maintainer: | Michael Sumner <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.0.0.9000 |
| Built: | 2026-10-15 00:45:50 UTC |
| Source: | https://github.com/hypertidy/tilemath |
Convert between longitude/latitude (degrees, WGS84) and spherical Mercator metres (EPSG:3857).
lonlat_to_merc(lon, lat) merc_to_lonlat(x, y)lonlat_to_merc(lon, lat) merc_to_lonlat(x, y)
lon, lat
|
numeric vectors of longitude and latitude in degrees |
x, y
|
numeric vectors of Mercator coordinates in metres |
Latitudes are clipped to the Mercator limit of approximately +/-85.05113 degrees.
a data.frame with two columns
lonlat_to_merc(147.33, -42.88) merc_to_lonlat(16395637, -5300240)lonlat_to_merc(147.33, -42.88) merc_to_lonlat(16395637, -5300240)
Returns the (px, py) pixel coordinates in the global pixel space at a
given zoom level. The global pixel space is 2^zoom * tile_size pixels
on each side.
lonlat_to_pixelxy(lon, lat, zoom, tile_size = 256) pixelxy_to_lonlat(px, py, zoom, tile_size = 256)lonlat_to_pixelxy(lon, lat, zoom, tile_size = 256) pixelxy_to_lonlat(px, py, zoom, tile_size = 256)
lon, lat
|
numeric vectors of longitude and latitude in degrees |
zoom |
integer zoom level |
tile_size |
pixels per tile side |
px, py
|
numeric vectors of global pixel coordinates |
a data.frame with columns px and py
lonlat_to_pixelxy(147.33, -42.88, 12)lonlat_to_pixelxy(147.33, -42.88, 12)
Returns the (x, y) tile coordinates containing the given lon/lat at a given zoom level. Uses the standard OSM/XYZ convention where (0, 0) is the top-left tile.
lonlat_to_tilenum(lon, lat, zoom)lonlat_to_tilenum(lon, lat, zoom)
lon, lat
|
numeric vectors of longitude and latitude in degrees |
zoom |
integer zoom level |
a data.frame with integer columns x and y
lonlat_to_tilenum(147.33, -42.88, 12)lonlat_to_tilenum(147.33, -42.88, 12)
quadkey_parent() returns the quadkey of the containing tile at one
lower zoom level. quadkey_children() returns the four child quadkeys
at one deeper zoom level. quadkey_siblings() returns the three other
quadkeys sharing the same parent.
quadkey_parent(quadkey) quadkey_children(quadkey) quadkey_siblings(quadkey)quadkey_parent(quadkey) quadkey_children(quadkey) quadkey_siblings(quadkey)
quadkey |
character vector of quadkeys |
for quadkey_parent(), a character vector. For
quadkey_children() and quadkey_siblings(), a list of character
vectors, one per input quadkey.
quadkey_parent("1203") quadkey_children("12") quadkey_siblings("1203")quadkey_parent("1203") quadkey_children("12") quadkey_siblings("1203")
Decode a Bing Maps quadkey string back to (x, y, zoom). Vectorized.
quadkey_to_tilenum(quadkey)quadkey_to_tilenum(quadkey)
quadkey |
character vector of quadkeys |
a data.frame with integer columns x, y, zoom
quadkey_to_tilenum("213") quadkey_to_tilenum(c("120", "121", "122", "123"))quadkey_to_tilenum("213") quadkey_to_tilenum(c("120", "121", "122", "123"))
Compute the bounding extent of one or more tiles. Vectorized over x, y, and zoom.
tile_extent(x, y, zoom, units = c("native", "lonlat"))tile_extent(x, y, zoom, units = c("native", "lonlat"))
x, y
|
integer vectors of tile coordinates |
zoom |
integer zoom level |
units |
one of "native" or "lonlat" |
For the Mercator pyramid, "native" units are Mercator metres (EPSG:3857). The "lonlat" units are the inverse projection of the tile corners to longitude and latitude in degrees.
a data.frame with columns xmin, xmax, ymin, ymax
tile_extent(3724, 2589, 12) tile_extent(3724, 2589, 12, units = "lonlat")tile_extent(3724, 2589, 12) tile_extent(3724, 2589, 12, units = "lonlat")
Substitute tile coordinates into a URL or path template. Vectorized
over x, y, and zoom. Recognised placeholders are {x}, {y},
{z}, {-y} for the TMS y-flipped value, and {q} for the quadkey.
tile_url(x, y, zoom, template)tile_url(x, y, zoom, template)
x, y
|
integer vectors of tile coordinates (XYZ convention) |
zoom |
integer zoom level |
template |
a character scalar template string |
a character vector of formatted URLs
tile_url(3698, 2518, 12, "https://tile.openstreetmap.org/{z}/{x}/{y}.png") tile_url(3, 5, 3, "https://ecn.t0.tiles.virtualearth.net/tiles/a{q}.jpeg?g=587")tile_url(3698, 2518, 12, "https://tile.openstreetmap.org/{z}/{x}/{y}.png") tile_url(3, 5, 3, "https://ecn.t0.tiles.virtualearth.net/tiles/a{q}.jpeg?g=587")
Returns the longitude/latitude of the top-left (north-west) corner of the given tile.
tilenum_to_lonlat(x, y, zoom)tilenum_to_lonlat(x, y, zoom)
x, y
|
integer vectors of tile coordinates |
zoom |
integer zoom level |
a data.frame with columns lon and lat
tilenum_to_lonlat(3724, 2589, 12)tilenum_to_lonlat(3724, 2589, 12)
Encode tile (x, y, zoom) as a Bing Maps quadkey string. Vectorized.
tilenum_to_quadkey(x, y, zoom)tilenum_to_quadkey(x, y, zoom)
x, y
|
integer vectors of tile coordinates |
zoom |
integer zoom level (length 1 or matching x and y) |
a character vector of quadkeys
tilenum_to_quadkey(3, 5, 3) tilenum_to_quadkey(c(3698, 3699), c(2518, 2518), 12)tilenum_to_quadkey(3, 5, 3) tilenum_to_quadkey(c(3698, 3699), c(2518, 2518), 12)
Returns the tiles covering a given longitude/latitude extent. If
zoom is NULL, picks the deepest zoom level for which the number
of tiles is at most max_tiles. If zoom is supplied, uses it
directly and max_tiles is ignored.
tiles_at(extent, max_tiles = 16, zoom = NULL, tile_size = 256)tiles_at(extent, max_tiles = 16, zoom = NULL, tile_size = 256)
extent |
numeric vector of length 4 in lonlat:
|
max_tiles |
maximum number of tiles allowed when auto-picking zoom |
zoom |
optional integer zoom level. If supplied, overrides automatic zoom selection. |
tile_size |
pixels per tile side (default 256) |
The extent is expanded ("snapped") to the union of the tile extents.
The snapped extent in native (Mercator) units is attached as the
extent_snapped attribute on the returned data.frame.
a data.frame with one row per tile and columns x, y,
zoom, xmin, xmax, ymin, ymax (the last four in Mercator
metres). The extent_snapped attribute holds the union extent.
tiles_at(c(147.2, 147.5, -43.0, -42.7), max_tiles = 16) tiles_at(c(147.2, 147.5, -43.0, -42.7), zoom = 12)tiles_at(c(147.2, 147.5, -43.0, -42.7), max_tiles = 16) tiles_at(c(147.2, 147.5, -43.0, -42.7), zoom = 12)
Returns the tiles covering a buffered region around a longitude/
latitude point. The buffer is applied in Mercator metres around the
projected point, then converted back to a lonlat extent and passed to
tiles_at().
tiles_near( lon, lat, buffer = 5000, max_tiles = 16, zoom = NULL, tile_size = 256 )tiles_near( lon, lat, buffer = 5000, max_tiles = 16, zoom = NULL, tile_size = 256 )
lon, lat
|
numeric scalars of the point coordinates in degrees |
buffer |
half-width of the region in Mercator metres |
max_tiles |
maximum number of tiles allowed when auto-picking zoom |
zoom |
optional integer zoom level |
tile_size |
pixels per tile side (default 256) |
Note that "metres" here means Mercator metres, not great-circle metres on the ellipsoid. At high latitudes the two diverge, but for tile selection the difference is rarely material.
as for tiles_at()
tiles_near(147.33, -42.88, buffer = 20000) tiles_near(147.33, -42.88, buffer = 20000, zoom = 12)tiles_near(147.33, -42.88, buffer = 20000) tiles_near(147.33, -42.88, buffer = 20000, zoom = 12)
XYZ tile schemes (OSM, Google, Mapbox) place y = 0 at the top of the grid. TMS places y = 0 at the bottom. These functions flip a y value between the two conventions. They are mathematically identical but named for clarity.
xyz_to_tms(y, zoom) tms_to_xyz(y, zoom)xyz_to_tms(y, zoom) tms_to_xyz(y, zoom)
y |
integer vector of tile y coordinates |
zoom |
integer zoom level |
integer vector of flipped y coordinates
xyz_to_tms(2518, 12) tms_to_xyz(1577, 12)xyz_to_tms(2518, 12) tms_to_xyz(1577, 12)