diff --git a/NAMESPACE b/NAMESPACE index 64a6f6a..f94719c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -19,7 +19,6 @@ export(get_tiles_dim) export(get_tiles_zoom) export(mercator_tile_extent) export(plot_tiles) -export(slippy_cache) export(tiles_to_polygon) importFrom(curl,curl_download) importFrom(dplyr,filter) @@ -33,13 +32,11 @@ importFrom(magrittr,"%>%") importFrom(purrr,pmap) importFrom(rappdirs,user_cache_dir) importFrom(raster,"projection<-") -importFrom(raster,cellsFromExtent) importFrom(raster,crop) importFrom(raster,extent) importFrom(rlang,.data) importFrom(sp,plot) importFrom(stats,approx) importFrom(stats,setNames) -importFrom(terra,rast) importFrom(tibble,tibble) importFrom(utils,askYesNo) diff --git a/NEWS.md b/NEWS.md index 7c6e8eb..1a7b733 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,10 @@ # ceramic 0.7.0 +* Removed raster handling support. + +* Removed virtual tiles (see hypertidy/grout). + * Begin move to use GDAL for the read, separate tile downloading from raster input. * Fixed `cc_kingston` location. diff --git a/R/cache.R b/R/cache.R index 50bf004..a201567 100644 --- a/R/cache.R +++ b/R/cache.R @@ -181,7 +181,8 @@ tile_zoom <- function(x) { #' ceramic_cache() #' } ceramic_cache <- function(force = FALSE) { - cache <- file.path(rappdirs::user_cache_dir(), ".ceramic") + ## normalize else gdal creates ./~/ + cache <- file.path( normalizePath(rappdirs::user_cache_dir()), ".ceramic") if (!fs::dir_exists(cache)) { if (!force) { val <- TRUE @@ -190,16 +191,16 @@ ceramic_cache <- function(force = FALSE) { } fs::dir_create(cache) } + gdalwmspath <- file.path(cache, "ceramic.gdalwmscache") + curr <- vapour::vapour_get_config("GDAL_DEFAULT_WMS_CACHE_PATH") + if (!nzchar(curr)) { + fs::dir_create(gdalwmspath) + vapour::vapour_set_config("GDAL_DEFAULT_WMS_CACHE_PATH", gdalwmspath) + } cache } -#' @name ceramic_cache -#' @keywords internal -#' @export -slippy_cache <- function(...) { - .Deprecated("ceramic_cache") - ceramic_cache(...) -} + url_to_cache <- function(x) { base_filepath <- file.path(ceramic_cache(), gsub("^//", "", gsub("^https\\:", "", gsub("^https\\:", "", x)))) ## chuck off any ? junk diff --git a/R/ceramic-package.R b/R/ceramic-package.R index a5e3229..7fab603 100644 --- a/R/ceramic-package.R +++ b/R/ceramic-package.R @@ -1,11 +1,14 @@ #' Obtain imagery tiles #' -#' The ceramic package provides tools to download and load imagery and raster tiles from online servers. +#' The ceramic package provides tools to download raster tiles from online servers. #' #' Any process that can trigger downloads will first check the [ceramic_cache()] in case the tile already exists. #' +#' It can also load raster data from online servers to obtain imagery, but we let GDAL manage that. +#' +#' If you want to deal with the tiles downloaded directly, see [ceramic_tiles()]. #' -#' The main functions are for downloading tiles and loading them as raster objects, and each accepts a spatial +#' The main functions are for downloading tiles and each accepts a spatial #' object for the first argument, alternatively a raster extent, or location: #' #' @@ -16,7 +19,7 @@ #' \code{\link{get_tiles_zoom}}\tab Download tiles base on extent and zoom level \cr #' } #' -#' Two helper functions will trigger the download of tiles and also collate the result into a raster object: +#' Two helper functions will load imagery into a raster object: #' #' #' \tabular{ll}{ diff --git a/R/format-utils.R b/R/format-utils.R deleted file mode 100644 index 662e8f1..0000000 --- a/R/format-utils.R +++ /dev/null @@ -1,14 +0,0 @@ -is_jpeg <- function(x) { - if (!file.exists(x[1])) return(FALSE) - if (file.info(x[1])$size <= 11L) return(FALSE) - rawb <- readBin(x[1], "raw", n = 11L) - all(rawb[1:2] == as.raw(c(0xff, 0xd8))) && rawToChar(rawb[7:11]) == "JFIF" -} - -is_png <- function(x) { - #"89 50 4e 47 0d 0a 1a 0a" - if (!file.exists(x[1])) return(FALSE) - if (file.info(x[1])$size <= 8L) return(FALSE) - rawb <- readBin(x[1], "raw", n = 8L) - all(rawb == as.raw(c(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a))) -} diff --git a/R/raster.R b/R/raster.R index b19ed4a..cf4fc78 100644 --- a/R/raster.R +++ b/R/raster.R @@ -1,79 +1,95 @@ -#' @importFrom terra rast -make_raster <- function(loc_data) { - files <- loc_data$files - tile_grid <- loc_data$tiles - user_extent <- loc_data$extent - files <- normalizePath(files) - if (find_format(files[1]) == "tif") { - out <- terra::merge(terra::sprc(lapply(files, terra::rast))) - #terra::crs(out) <- "EPSG:3857" - out <- raster::raster(out) - raster::projection(out) <- "+proj=merc +a=6378137 +b=6378137" - ## short circuit, the old way is not working - return(out) - } - br <- lapply(files, raster_brick) +# is_jpeg <- function(x) { +# if (!file.exists(x[1])) return(FALSE) +# if (file.info(x[1])$size <= 11L) return(FALSE) +# rawb <- readBin(x[1], "raw", n = 11L) +# all(rawb[1:2] == as.raw(c(0xff, 0xd8))) && rawToChar(rawb[7:11]) == "JFIF" +# } +# +# is_png <- function(x) { +# #"89 50 4e 47 0d 0a 1a 0a" +# if (!file.exists(x[1])) return(FALSE) +# if (file.info(x[1])$size <= 8L) return(FALSE) +# rawb <- readBin(x[1], "raw", n = 8L) +# all(rawb == as.raw(c(0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a))) +# } - for (i in seq_along(br)) { - br[[i]] <- raster::setExtent(br[[i]], - mercator_tile_extent(tile_grid$tiles$x[i], tile_grid$tiles$y[i], zoom = tile_grid$zoom)) - } - out <- fast_merge(br) - raster::crs(out) <- sp::CRS(.merc(), doCheckCRSArgs = FALSE) - if (!is.null(user_extent)) out <- raster::crop(out, user_extent , snap = "out") - out -} - -raster_brick <- function(x) { - out <- NULL - if (find_format(x) == "tif") { - #out <- raster::brick(terra::rast(x)) - #return(raster::setExtent(out, raster::extent(0, nrow(out), 0, ncol(out)))) - return(terra::rast(out)) - } - if (find_format(x)== "jpg") { - out <- jpeg::readJPEG(x) - } - if (find_format(x) == "png") { - out <- png::readPNG(x) - } - - if (is.null(out)) stop(sprintf("cannot read %s", x)) - out <- out*255 - mode(out) <- "integer" - ## in case it's greyscale ... - if (length(dim(out)) == 2L) out <- array(out, c(dim(out), 1L)) - # raster::setExtent(raster::brick(out), raster::extent(0, nrow(out), 0, ncol(out))) - raster::brick(out) -} - -raster_readAll <- function(x) { - if (!raster::hasValues(x)) x <- raster::readAll(x) - x -} - -find_format <- function(x) { - fmt <- NULL - if (grepl("tif$", x)) return("tif") - ## jpg or png - if (is_jpeg(x)) fmt <- "jpg" - if (is_png(x)) fmt <- "png" - if (is.null(fmt)) stop(sprintf("unknown format", x)) - fmt -} - - -#' @importFrom raster cellsFromExtent -fast_merge <- function(x) { - - ## about 3 times faster than reduce(, merge - crs <- raster::projection(x[[1]]) - - out <- raster::raster(purrr::reduce(lapply(x, raster::extent), raster::union), crs = crs) - raster::res(out) <- raster::res(x[[1]]) -# cells <- unlist(purrr::map(x, ~raster::cellsFromExtent(out, .x))) - cells <- unlist(lapply(x, function(.x) cellsFromExtent(out, .x)), use.names = FALSE) - vals <- do.call(rbind, lapply(x, function(.x) raster::values(raster_readAll(.x)))) - raster::setValues(raster::brick(out, out, out), vals[order(cells), ]) -} +#' #' @importFrom terra rast +#' make_raster <- function(loc_data) { +#' files <- loc_data$files +#' tile_grid <- loc_data$tiles +#' user_extent <- loc_data$extent +#' files <- normalizePath(files) +#' if (find_format(files[1]) == "tif") { +#' out <- terra::merge(terra::sprc(lapply(files, terra::rast))) +#' #terra::crs(out) <- "EPSG:3857" +#' out <- raster::raster(out) +#' raster::projection(out) <- "+proj=merc +a=6378137 +b=6378137" +#' ## short circuit, the old way is not working +#' return(out) +#' } +#' br <- lapply(files, raster_brick) +#' +#' for (i in seq_along(br)) { +#' br[[i]] <- raster::setExtent(br[[i]], +#' mercator_tile_extent(tile_grid$tiles$x[i], tile_grid$tiles$y[i], zoom = tile_grid$zoom)) +#' } +#' +#' out <- fast_merge(br) +#' raster::crs(out) <- sp::CRS(.merc(), doCheckCRSArgs = FALSE) +#' if (!is.null(user_extent)) out <- raster::crop(out, user_extent , snap = "out") +#' out +#' } +#' +#' raster_brick <- function(x) { +#' out <- NULL +#' if (find_format(x) == "tif") { +#' #out <- raster::brick(terra::rast(x)) +#' #return(raster::setExtent(out, raster::extent(0, nrow(out), 0, ncol(out)))) +#' return(terra::rast(out)) +#' } +#' if (find_format(x)== "jpg") { +#' out <- jpeg::readJPEG(x) +#' } +#' if (find_format(x) == "png") { +#' out <- png::readPNG(x) +#' } +#' +#' if (is.null(out)) stop(sprintf("cannot read %s", x)) +#' out <- out*255 +#' mode(out) <- "integer" +#' ## in case it's greyscale ... +#' if (length(dim(out)) == 2L) out <- array(out, c(dim(out), 1L)) +#' # raster::setExtent(raster::brick(out), raster::extent(0, nrow(out), 0, ncol(out))) +#' raster::brick(out) +#' } +#' +#' raster_readAll <- function(x) { +#' if (!raster::hasValues(x)) x <- raster::readAll(x) +#' x +#' } +#' +#' find_format <- function(x) { +#' fmt <- NULL +#' if (grepl("tif$", x)) return("tif") +#' ## jpg or png +#' if (is_jpeg(x)) fmt <- "jpg" +#' if (is_png(x)) fmt <- "png" +#' if (is.null(fmt)) stop(sprintf("unknown format", x)) +#' fmt +#' } +#' +#' +#' #' @importFrom raster cellsFromExtent +#' fast_merge <- function(x) { +#' +#' ## about 3 times faster than reduce(, merge +#' crs <- raster::projection(x[[1]]) +#' +#' out <- raster::raster(purrr::reduce(lapply(x, raster::extent), raster::union), crs = crs) +#' raster::res(out) <- raster::res(x[[1]]) +#' # cells <- unlist(purrr::map(x, ~raster::cellsFromExtent(out, .x))) +#' cells <- unlist(lapply(x, function(.x) cellsFromExtent(out, .x)), use.names = FALSE) +#' vals <- do.call(rbind, lapply(x, function(.x) raster::values(raster_readAll(.x)))) +#' raster::setValues(raster::brick(out, out, out), vals[order(cells), ]) +#' } diff --git a/R/spatial.R b/R/spatial.R index 5e5a7e4..cc23e01 100644 --- a/R/spatial.R +++ b/R/spatial.R @@ -63,7 +63,7 @@ spatial_bbox <- function(loc, buffer = NULL) { ## convert loc to mercator meters loc <- slippymath::lonlat_to_merc(loc) - + xp <- buffer[1] ## buffer is meant to be from a central point, so a radius yp <- buffer[2] @@ -94,6 +94,11 @@ spex_to_pt <- function(x) { srcproj <- "EPSG:4267" is_ll <- TRUE } + if (srcproj == "WGS 84") { + srcproj <- "+proj=longlat" + is_ll <- TRUE + } + if (is.na(srcproj)) { if (raster::couldBeLonLat(x, warnings = FALSE)) { warning("loc CRS is not set, assuming longlat") @@ -110,17 +115,23 @@ spex_to_pt <- function(x) { } #' @importFrom stats approx project_spex <- function(x, crs) { + ex <- c(raster::xmin(x), raster::xmax(x), raster::ymin(x), raster::ymax(x)) idx <- c(1, 1, 2, 2, 1, 3, 4, 4, 3, 3) xy <- matrix(ex[idx], ncol = 2L) afun <- function(aa) stats::approx(seq_along(aa), aa, n = 180L)$y - srcproj <- raster::projection(x) - is_ll <- raster::couldBeLonLat(x, warnings = FALSE) + srcproj <- raster::projection(x) + is_ll <- raster::couldBeLonLat(x, warnings = FALSE) if (srcproj == "NAD27") { srcproj <- "EPSG:4267" is_ll <- TRUE - } + } + if (srcproj == "WGS 84") { + srcproj <- "+proj=longlat" + is_ll <- TRUE + } + if (is.na(srcproj)) { if (is_ll) { warning("loc CRS is not set, assuming longlat") diff --git a/R/utils.R b/R/utils.R index 09472fb..3ffa6a8 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1,14 +1,14 @@ -.merc <- function() { - # if (PROJ::ok_proj6()) { - # return("EPSG:3857") - # } - - # [1] "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs" - ## testepsg - ## "PROJ.4 rendering of [+proj=merc +a=6378137] = - ##+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +R=6378137 +units=m +no_defs - - - "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +R=6378137 +units=m +no_defs" -} -.ll <- function() "+proj=longlat +datum=WGS84 +no_defs" +.merc <- function() { + # if (PROJ::ok_proj6()) { + # return("EPSG:3857") + # } + + # [1] "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs" + ## testepsg + ## "PROJ.4 rendering of [+proj=merc +a=6378137] = + ##+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +R=6378137 +units=m +no_defs + + + "+proj=merc +R=6378137" +} +.ll <- function() "+proj=longlat +datum=WGS84" diff --git a/R/virtual_tiles.R b/R/virtual_tiles.R deleted file mode 100644 index d049d77..0000000 --- a/R/virtual_tiles.R +++ /dev/null @@ -1,23 +0,0 @@ -MAXEXTENT <- 20037508 -A <- 6378137 -proj4 <- sprintf("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +R=A +units=m +no_defs") - -llproj4 <- .ll() -globalVariables(c("MAXETENT", "A", "proj4", "llproj4"), "ceramic", add = TRUE) - -virtual_tiles <- function(zoom = 0, extent = NULL) { - if (is.null(extent)) { - ex <- c(xmin = -180, xmax = 180, - ymin = -85.0511, ymax = 85.0511) - } else { - ex <- raster::extent(extent) - ex <- c(xmin = raster::xmin(ex), xmax = raster::xmax(ex), - ymin = raster::ymin(ex), ymax = raster::ymax(ex)) - } - - bb <- structure(ex, crs = structure(list(proj4string = llproj4, - epsg = NA_integer_), class = "crs"), - class = "bbox") - slippymath::bbox_to_tile_grid(bb, zoom = zoom, max_tiles = Inf) -} - diff --git a/R/zzz.R b/R/zzz.R new file mode 100644 index 0000000..e313c83 --- /dev/null +++ b/R/zzz.R @@ -0,0 +1,4 @@ +.onLoad <- function(libname, pkgname) { + cache <- ceramic_cache() + invisible(NULL) +} diff --git a/README.Rmd b/README.Rmd index e1d3462..2a60692 100644 --- a/README.Rmd +++ b/README.Rmd @@ -29,17 +29,19 @@ library(ceramic) roi <- raster::extent(100, 160, -50, 10) im <- cc_location(roi) -raster::plotRGB(im) +terra::plotRGB(im) ``` We can use raster, sp, or sf objects to define an extent. This provides a very easy way to obtain imagery or elevation data for any almost any region using our own data. ```{r extent2, fig.width=8, fig.height=10} -data("wrld_simpl", package = "maptools") -im <- cc_location(subset(wrld_simpl, NAME == "Bolivia"), - type = "mapbox.satellite") -raster::plotRGB(im) +sql <- "SELECT shapeGroup FROM geoBoundariesCGAZ_ADM0 WHERE shapeGroup IN ('BOL')" +dsn <- "/vsizip//vsicurl/https://github.com/wmgeolab/geoBoundaries/raw/main/releaseData/CGAZ/geoBoundariesCGAZ_ADM0.zip" +bol <- sf::read_sf(dsn, query = sql) + +im <- cc_location(bol) +terra::plotRGB(im) ``` Even if the data uses a map projection it will be converted into a region to match the Mercator extents used by Mapbox image servers. @@ -53,8 +55,8 @@ data("nz", package = "spData") library(sf) im_nz2 <- cc_location(nz) -raster::plotRGB(im_nz2) -plot(st_transform(nz, raster::projection(im_nz2)), add = TRUE, col = rainbow(nrow(nz), alpha = 0.5)) +terra::plotRGB(im_nz2) +plot(st_transform(nz, terra::crs(im_nz2)), add = TRUE, col = rainbow(nrow(nz), alpha = 0.5)) ``` There are basic heuristics to decide if data is projected or just in "longitude,latitude" in the usual raster package way. @@ -63,19 +65,21 @@ Raster elevation data is also available. ```{r} north <- nz[nz$Island == "North", ] -dem_nz <- cc_location(north, type = "elevation-tiles-prod" ) +dem_nz <- cc_elevation(north, type = "elevation-tiles-prod" ) ## plot elevation data for NZ north dem_nz[!dem_nz > 0] <- NA -raster::plot(dem_nz, col = grey(seq(0, 1, length = 51)), breaks = quantile(raster::values(dem_nz), seq(0, 1, length = 52), na.rm = TRUE), legend = FALSE) -plot(st_transform(st_cast(north, "MULTILINESTRING")["Name"], raster::projection(dem_nz)), add = TRUE, lwd = 5) +terra::plot(dem_nz, col = grey(seq(0, 1, length = 51)), breaks = quantile(unique(terra::values(dem_nz)), seq(0, 1, length = 52), na.rm = TRUE), legend = FALSE) +plot(st_transform(st_cast(north, "MULTILINESTRING")["Name"], terra::crs(dem_nz)), add = TRUE, lwd = 5) ``` ## I thought you said *tiles*? -Indeed, the `cc_location()` and `cc_elevation()` functions run `get_tiles()` behind the scenes. +Indeed, the `cc_location()` and `cc_elevation()` functions no longer use tiles. WAT + +But, they used to run `get_tiles()` behind the scenes. This function and its counterparts `get_tiles_zoom()`, `get_tiles_dim()` and `get_tiles_buffer()` will *only download files*. @@ -95,7 +99,7 @@ The default map provider is [Mapbox](https://www.mapbox.com/), but ceramic is wr ```{r} pt <- cbind(175.6082, -37.994) -nz_z12 <- cc_location(pt, zoom = 12, type = "elevation-tiles-prod") +nz_z12 <- cc_location(pt, buffer = 100000, type = "elevation-tiles-prod") ``` ```{r, eval=FALSE, include = FALSE} @@ -103,33 +107,9 @@ north_carolina <- sf::read_sf(system.file("gpkg/nc.gpkg", package = "sf", mustWo nc_image <- cc_location(north_carolina) rowan_dem <- cc_elevation(dplyr::filter(north_carolina, NAME == "Rowan")) -nc_contour <- raster::rasterToContour(raster::focal(cc_elevation(north_carolina), matrix(1, 3, 3)), levels = pretty(values(rowan_dem))) -``` - -```{r, eval=FALSE, include = FALSE} -op <- par(mfrow = c(2, 1)) -raster::plotRGB(nc_image) -plot(nc_contour, add = TRUE, col = "white") -plot(raster::extent(rowan_dem), add = TRUE, col = "firebrick", lwd = 3) -plot(rowan_dem, col = grey(seq(0, 1, length = 26)), add = TRUE, legend = FALSE) -plot(rowan_dem, col = grey(seq(0, 1, length = 26)), add = TRUE, legend = FALSE) -box(col= "white") -par(op) +rowan_dem ``` -Use `max_tiles` or `zoom` to increase or decrease resolution. - -```{r, max-tiles} -im1 <- cc_location(im, debug = TRUE) -im2 <- cc_location(im, zoom = 7) - -im1 - -im2 -``` - - - ## Installation @@ -152,132 +132,6 @@ Set your mapbox API key with Sys.setenv(MAPBOX_API_KEY = "") ``` -## Example - -This complete example gets tiled imagery that we can use as real data. - -The code here - -* generates a bounding box in longitude-latitude -* uses [slippymath](https://CRAN.r-project.org/package=slippymath) to find sensible tiles for the region -* downloads them to a local cache -* georeferences them and merges the tiles into a sensible raster object - -```{r example} -library(ceramic) -## a point in longlat, and a buffer with in metres -pt <- cbind(136, -34) -im <- cc_location(pt, buffer = c(1e6, 5e5), type = "mapbox.satellite") -library(raster) -plotRGB(im) - -## get the matching tiles (zoom is magic here, it's all wrapped - needs thought) - -tiles <- ceramic_tiles(zoom = 6, type = "mapbox.satellite") -library(sf) -plot(st_geometry(ceramic:::tiles_to_polygon(tiles)), add = TRUE) -middle <- function(x, y) { - x + (y - x)/2 -} -text(middle(tiles$xmin, tiles$xmax), middle(tiles$ymin, tiles$ymax), lab = sprintf("[%i,%i]", tiles$tile_x, tiles$tile_y), - col = "firebrick") -``` - -## Get imagery and DEM for use in 3D visualization - - -This code downloads a specific region as elevation and imagery at specific zoom levels to build [a 3D scene](https://hypertidy.github.io/anglr-demos/ceramic-demo01.html). - - -```{R,eval=FALSE} -library(ceramic); library(quadmesh) -library(raster); library(rgl); -library(reproj); library(htmlwidgets) -clear3d() - -## longlat extent -ex0 <- c(147.15, 147.45, -42.9, -42.6) -ex <- extent(ex0) - -## local LAEA projection, based on the centre of the extent -prj <- sprintf("+proj=laea +lon_0=%f +lat_0=%f +datum=WGS84", mean(ex0[1:2]), mean(ex0[3:4])) -## Mapbox elevation -dem <- cc_elevation(ex, zoom = 8) -## Mapbox satellite imagery -im <- cc_location(ex, zoom = 13) - -## quadmesh with texture for rgl, in local projection -qm <- reproj::reproj(quadmesh(dem, texture = im), prj) - -## plot with rgl, set the aspect ratio and backround -shade3d(qm, lit = FALSE); -aspect3d(1, 1, .1) -bg3d(grey(0.8)) -``` - -The zoom levels were chosen by first reading an automatic level for the extents, and then tweaking the zoom - higher resolution for the imagery, lower resolution for the elevation. This provides a compelling visualization in 3D as the imagery is *textured* onto the elevation data, using rgl's `mesh3d` type and `shade3d()` function. - - -## Local caching of tiles - -A key feature of ceramic is *caching*, all data is downloaded in a systematic way that is suitable for later re-use. Many tools for imagery services treat the imagery as transient, but here we take control over the raw data itself. All file names match exactly the address URL of the original source data. - -There is a helper function to find existing tiles. - -```{r files} -aa <- cc_location(loc = cbind(0, 0), buffer = 330000, type = "mapbox.satellite") -ceramic_tiles(zoom = 7, type = "mapbox.satellite") -``` - -and every row has the extent values useable directly by raster: - -```{r extent} -ceramic_tiles(zoom = 7, type = "mapbox.satellite") %>% - dplyr::slice(1:5) %>% - purrr::transpose() %>% - purrr::map(~raster::extent(unlist(.x[c("xmin", "xmax", "ymin", "ymax")]))) -``` - - -Another example - -```{r tasmania} -my_bbox <- - st_bbox(c(xmin = 144, - xmax = 147.99, - ymin = -44.12, - ymax = -40), - crs = st_crs("+proj=longlat +ellps=WGS84")) -im <- cc_location(cbind(145.5, -42.2), buffer = 5e5) -plotRGB(im) -plot(st_transform(ozmaps::abs_lga$geometry, projection(im)), add = TRUE, lwd = 2, border = "white") -``` - -An internal function sets up a plot of tiles at particular zoom levels. - -```R -ceramic::plot_tiles(ceramic_tiles(zoom = c(7, 9))) -``` - -![tile plot](man/figures/README-tile-plot.png) - -And we can add the tiles to an existing plot. - -```R -plotRGB(im) -ceramic::plot_tiles(ceramic_tiles(zoom = 7), add = TRUE) -``` - -![tile add plot](man/figures/README-tile-add-plot.png) - -# Future improvements - - -See the [Issue tab](https://github.com/hypertidy/ceramic/issues/) and please make suggestions and give feedback! - -There's an ongoing process to uncouple from the "spatial packages", currently we have an unclassy mix of sp, terra, raster, and even sf -in the readme. We'll fix it. - --- diff --git a/man/cc_location.Rd b/man/cc_location.Rd index 03d0ace..c1c55f4 100644 --- a/man/cc_location.Rd +++ b/man/cc_location.Rd @@ -18,7 +18,8 @@ cc_location( ..., zoom = NULL, max_tiles = NULL, - debug = FALSE + debug = FALSE, + dimension = NULL ) cc_macquarie( @@ -28,7 +29,8 @@ cc_macquarie( ..., zoom = NULL, max_tiles = NULL, - debug = FALSE + debug = FALSE, + dimension = NULL ) cc_davis( @@ -38,7 +40,8 @@ cc_davis( ..., zoom = NULL, max_tiles = NULL, - debug = FALSE + debug = FALSE, + dimension = NULL ) cc_mawson( @@ -48,7 +51,8 @@ cc_mawson( ..., zoom = NULL, max_tiles = NULL, - debug = FALSE + debug = FALSE, + dimension = NULL ) cc_casey( @@ -58,7 +62,8 @@ cc_casey( ..., zoom = NULL, max_tiles = NULL, - debug = FALSE + debug = FALSE, + dimension = NULL ) cc_heard( @@ -68,7 +73,8 @@ cc_heard( ..., zoom = NULL, max_tiles = NULL, - debug = FALSE + debug = FALSE, + dimension = NULL ) cc_kingston( @@ -78,7 +84,8 @@ cc_kingston( ..., zoom = NULL, max_tiles = NULL, - debug = FALSE + debug = FALSE, + dimension = NULL ) cc_elevation( @@ -88,7 +95,8 @@ cc_elevation( ..., zoom = NULL, max_tiles = NULL, - debug = FALSE + debug = FALSE, + dimension = NULL ) } \arguments{ @@ -100,11 +108,13 @@ cc_elevation( \item{...}{arguments passed to internal function, specifically \code{base_url} (see Details)} -\item{zoom}{desired zoom for tiles, use with caution - if \code{NULL} is chosen automatically} +\item{zoom}{deprecated (use \code{dimension})} -\item{max_tiles}{maximum number of tiles to be read into memory - if \code{NULL} is set by zoom constraints} +\item{max_tiles}{deprecated} -\item{debug}{optionally print out files that will be used} +\item{debug}{deprecated} + +\item{dimension}{one or two numbers, used to determine the number of pixels width, height - set one to zero to let GDAL figure it out, or leave as NULL to get something suitable} } \value{ A \code{\link[raster:brick]{raster::brick()}} object, either 'RasterBrick' with three layers (Red, Green, Blue) or with diff --git a/man/ceramic-package.Rd b/man/ceramic-package.Rd index c008572..878ae2d 100644 --- a/man/ceramic-package.Rd +++ b/man/ceramic-package.Rd @@ -6,12 +6,16 @@ \alias{ceramic} \title{Obtain imagery tiles} \description{ -The ceramic package provides tools to download and load imagery and raster tiles from online servers. +The ceramic package provides tools to download raster tiles from online servers. } \details{ Any process that can trigger downloads will first check the \code{\link[=ceramic_cache]{ceramic_cache()}} in case the tile already exists. -The main functions are for downloading tiles and loading them as raster objects, and each accepts a spatial +It can also load raster data from online servers to obtain imagery, but we let GDAL manage that. + +If you want to deal with the tiles downloaded directly, see \code{\link[=ceramic_tiles]{ceramic_tiles()}}. + +The main functions are for downloading tiles and each accepts a spatial object for the first argument, alternatively a raster extent, or location: \tabular{ll}{ @@ -21,7 +25,7 @@ object for the first argument, alternatively a raster extent, or location: \code{\link{get_tiles_zoom}}\tab Download tiles base on extent and zoom level \cr } -Two helper functions will trigger the download of tiles and also collate the result into a raster object: +Two helper functions will load imagery into a raster object: \tabular{ll}{ \code{\link{cc_location}}\tab Download tiles and build a raster object of imagery\cr diff --git a/man/ceramic_cache.Rd b/man/ceramic_cache.Rd index 34f304c..51684ce 100644 --- a/man/ceramic_cache.Rd +++ b/man/ceramic_cache.Rd @@ -2,12 +2,9 @@ % Please edit documentation in R/cache.R \name{ceramic_cache} \alias{ceramic_cache} -\alias{slippy_cache} \title{Ceramic file cache} \usage{ ceramic_cache(force = FALSE) - -slippy_cache(...) } \arguments{ \item{force}{set to \code{TRUE} to create the location without asking the user} @@ -33,4 +30,3 @@ if (interactive()) { ceramic_cache() } } -\keyword{internal} diff --git a/man/figures/README-unnamed-chunk-1-1.png b/man/figures/README-unnamed-chunk-1-1.png index 234350d..0430067 100644 Binary files a/man/figures/README-unnamed-chunk-1-1.png and b/man/figures/README-unnamed-chunk-1-1.png differ diff --git a/tests/testthat/test-tiles.R b/tests/testthat/test-tiles.R deleted file mode 100644 index 91fc0f8..0000000 --- a/tests/testthat/test-tiles.R +++ /dev/null @@ -1,20 +0,0 @@ -context("test-tiles") - -test_that("creating virtual tiles works", { - - skip_on_cran() - - z0 <- virtual_tiles() %>% expect_s3_class("tile_grid") - expect_true(nrow(z0$tiles) == 1L) - z5 <- virtual_tiles(zoom = 5) %>% expect_s3_class("tile_grid") - expect_true(nrow(z5$tiles) == 1024L) - - ze <- virtual_tiles(zoom = 5, extent = raster::extent(-1e5, 1e4, -1, 1e6)) %>% expect_s3_class("tile_grid") - expect_true(nrow(ze$tiles) == 416L) -}) - - -# test_that("creating vector tiles works", { -# #cc_location(cbind(0, 0), buffer = 1e6) -# #ct <- ceramic_tiles(zoom = 6) -# })