MyNixOS website logo
Description

Slippy Map Tile Tools.

Provides functions for performing common tasks when working with slippy map tile service APIs e.g. Google maps, Open Street Map, Mapbox, Stamen, among others. Functionality includes converting from latitude and longitude to tile numbers, determining tile bounding boxes, and compositing tiles to a georeferenced raster image.

slippymath

Travis build status AppVeyor build status lifecycle CRAN status

R functions for dealing with slippy map tile servers (Google maps, Open Street Map, Mapbox, Stamen et. al.)

The goal of this package is to take a bunch of logic that was lying around in defunct packages and obscure wikis and make it #rstats cannon in a way that is decoupled from any particular tile service or visualisation front end. Happy mapping!

Installation

## Dev version
remotes::install_github("milesmcbain/slippymath")

## CRAN version
install.packages('slippymath')

Usage

Tile maths

Look at how many tiles you might need:

library(sf)

uluru_bbox <-
  st_bbox(c(xmin = 131.02084,
            xmax = 131.0535,
            ymin = -25.35461,
            ymax = -25.33568),
          crs = st_crs("+proj=longlat +ellps=WGS84"))

bbox_tile_query(uluru_bbox)

# A tibble: 17 x 8
#    x_min  y_min  x_max  y_max y_dim x_dim total_tiles  zoom
#    <dbl>  <dbl>  <dbl>  <dbl> <dbl> <dbl>       <dbl> <int>
# 1      3      2      3      2     1     1           1     2
# 2      6      4      6      4     1     1           1     3
# 3     13      9     13      9     1     1           1     4
# 4     27     18     27     18     1     1           1     5
# 5     55     36     55     36     1     1           1     6
# 6    110     73    110     73     1     1           1     7
# 7    221    146    221    146     1     1           1     8
# 8    442    293    442    293     1     1           1     9
# 9    884    586    884    586     1     1           1    10
#10   1769   1173   1769   1173     1     1           1    11
#11   3538   2346   3539   2346     1     2           2    12
#12   7077   4692   7078   4692     1     2           2    13
#13  14154   9384  14156   9385     2     3           6    14
#14  28309  18769  28312  18771     3     4          12    15
#15  56619  37538  56625  37542     5     7          35    16
#16 113239  75076 113251  75084     9    13         117    17
#17 226478 150153 226502 150168    16    25         400    18

Get a grid of slippy map tile coordinates for a bounding box, given a zoom, or guess a zoom given a max_tiles:

bbox_to_tile_grid(uluru_bbox, max_tiles = 15)

#$tiles
#       x     y
#1  28309 18769
#2  28310 18769
#3  28311 18769
#4  28312 18769
#5  28309 18770
#6  28310 18770
#7  28311 18770
#8  28312 18770
#9  28309 18771
#10 28310 18771
#11 28311 18771
#12 28312 18771
#
#$zoom
#[1] 15
#
#attr(,"class")
#[1] "tile_grid"

Fetch a grid using your favourite tile server API. Creep on many tile providers here.

Here's a Mapbox example:


library(purrr)
library(curl)
library(glue)

tile_grid <- bbox_to_tile_grid(uluru_bbox, max_tiles = 15)

mapbox_query_string <-
  paste0("https://api.mapbox.com/v4/mapbox.satellite/{zoom}/{x}/{y}.jpg90",
         "?access_token=",
         Sys.getenv("MAPBOX_API_KEY"))

images <-
  pmap(tile_grid$tiles,
       function(x, y, zoom){
         outfile <- glue("{x}_{y}.jpg")
         curl_download(url = glue(mapbox_query_string),
                       destfile = outfile) 
         outfile 
       },
       zoom = tile_grid$zoom)

Tile compositing

You can compose a list of images and a corresponding tile grid to a spatially referenced raster, meaning you can plot over it with tmap etc.

library(raster)
library(rgdal)

raster_out <- compose_tile_grid(tile_grid, images)

## A convenient wrapper for raster image exports using png::writePNG.
raster_to_png(raster_out, "uluru.png")

Result:

© Mapbox © OpenStreetMapImprove this map.

Metadata

Version

0.3.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows