hypertidy / ceramic

read image server tiles direct with GDAL, or download image server tiles to a local cache
https://hypertidy.github.io/ceramic/
92 stars 6 forks source link

mapview for XYZ vs TMS #17

Closed mdsumner closed 4 years ago

mdsumner commented 5 years ago

The difference between XYZ and TMS: https://gist.github.com/tmcw/4954720

Mapview uses TMS by default, but Mapbox is XYZ

so

d <- file.path(ceramic::slippy_cache(), "api.mapbox.com", "v4", "mapbox.satellite")
## needs mapview since 45d432c1
mapview(d, tms = FALSE)
mdsumner commented 5 years ago

When mapview is updated, do

tt <- tiles_mapview(extent(147.22881, 147.37782, -42.96346, -42.85105), type = "mapbox.outdoors")
Preparing to download: 16 tiles at zoom = 13 from 
https://api.mapbox.com/v4/mapbox.outdoors/

and then

 tt <- tiles_mapview(extent(147.22881, 147.37782, -42.96346, -42.85105), type = "mapbox.outdoors", zoom = 14)
Preparing to download: 64 tiles at zoom = 14 from 
https://api.mapbox.com/v4/mapbox.outdoors/

etc.

mdsumner commented 5 years ago

Here's the tiles_mapview function:

## remotes::install_github("hypertidy/ceramic")
## remotes::install_github("r-spatial/mapview@develop")

tiles_mapview <- function(x, type = "mapbox.satellite", zoom = NULL, ...) {
  ## we aren't loading the tiles, but they are indexed minimally in this object
  if (is.null(zoom)) {
    tiles <- ceramic::get_tiles_dim(x, dim = c(1024, 1024), type = type, ..., format = "png")
  } else {
    tiles <- ceramic::get_tiles_zoom(x, zoom = zoom, type = type, ..., format = "png")
  }
  dp <- file.path(ceramic::slippy_cache(), "api.mapbox.com", "v4", type)
  mapview(dp, tms = FALSE)
}

library(mapview)
library(ceramic)

## it doesn't zoom in ...
tiles_mapview(raster::extent(breweries), type = "mapbox.satellite")
# Preparing to download: 9 tiles at zoom = 8 from 
# https://api.mapbox.com/v4/mapbox.satellite/

## get all lower zooms (for this extent)
purrr::walk(0:9, ~get_tiles_zoom(raster::extent(breweries), zoom = .x, type = "mapbox.satellite"))

tiles_mapview(raster::extent(breweries), zoom = 10, type = "mapbox.satellite")
# Preparing to download: 64 tiles at zoom = 10 from 
# https://api.mapbox.com/v4/mapbox.satellite/

## and then zoom in ...
mdsumner commented 5 years ago

Another example

## remotes::install_github("hypertidy/ceramic")
## remotes::install_github("r-spatial/mapview@develop")

tiles_mapview <- function(x, type = "mapbox.satellite", zoom = NULL, ...) {
  ## we aren't loading the tiles, but they are indexed minimally in this object
  if (is.null(zoom)) {
    tiles <- ceramic::get_tiles_dim(x, dim = c(1024, 1024), type = type, ..., format = "png")
  } else {
    tiles <- ceramic::get_tiles_zoom(x, zoom = zoom, type = type, ..., format = "png")
  }
  dp <- file.path(ceramic::slippy_cache(), "api.mapbox.com", "v4", type)
  mapview(dp, tms = FALSE)
}

library(mapview)
library(ceramic)
ex <- raster::extent(breweries)
purrr::walk(0:11, ~get_tiles_zoom(ex, type = "mapbox.outdoors", zoom = .x))
tiles_mapview(ex, type = "mapbox.outdoors")

That extent needs about 300 tiles all up to zoom 11 (at zoom 12 it needs another 812).

image

EDIT: needed format = "png"!!!

mdsumner commented 4 years ago

Example above updated for new syntax, and required dev versions:

#remotes::install_github("r-spatial/leafem")
#remotes::install_github("r-spatial/mapview@develop")

tiles_mapview <- function(x, type = "mapbox.satellite", zoom = NULL, ...) {
  ## we aren't loading the tiles, but they are indexed minimally in this object
  if (is.null(zoom)) {
    tiles <- ceramic::get_tiles_dim(x, dim = c(1024, 1024), type = type, ..., format = "png")
  } else {
    tiles <- ceramic::get_tiles_zoom(x, zoom = zoom, type = type, ..., format = "png")
  }
  dp <- file.path(ceramic::ceramic_cache(), "api.mapbox.com", "v4", type)
  mapview(dp, tms = FALSE)
}

library(mapview)
library(ceramic)

## it doesn't zoom in ...
tiles_mapview(raster::extent(breweries), type = "mapbox.satellite")
# Preparing to download: 9 tiles at zoom = 8 from 
# https://api.mapbox.com/v4/mapbox.satellite/

## get all lower zooms (for this extent)
purrr::walk(0:9, ~get_tiles_zoom(raster::extent(breweries), zoom = .x, type = "mapbox.satellite"))

tiles_mapview(raster::extent(breweries), zoom = 10, type = "mapbox.satellite")
mdsumner commented 4 years ago

also see #20 and #32