|
1 | | -#' Make tiles (pyramids) using `gdal2tiles` |
| 1 | +#' Make raster tiles (pyramids) |
2 | 2 | #' |
3 | | -#' @param tilePath A director to write tiles |
| 3 | +#' If python with GDAL support is available, uses `gdal2tiles` via [tiler::tile()] |
| 4 | +#' to create tiles for use with e.g. `leaflet`. |
| 5 | +#' |
| 6 | +#' @note Requires python with GDAL support (see [tiler::tiler_options()]). |
| 7 | +#' Windows users should install 'OSGeo4W' (<https://trac.osgeo.org/osgeo4w/>). |
| 8 | +#' |
| 9 | +#' @param tilePath A directory to write tiles |
4 | 10 | #' |
5 | 11 | #' @param obj A raster object with or without file-backing |
6 | 12 | #' |
|
9 | 15 | #' |
10 | 16 | #' @param ... Arguments passed to [reproducible::projectInputs()] (e.g., `useGDAL`). |
11 | 17 | #' |
| 18 | +#' @return |
| 19 | +#' - `makeTiles()` is invoked for it's side-effect of creating tiles in `tilePath`; |
| 20 | +#' - `canMakeTiles()` returns a logical indicating the availability of python and GDAL support; |
| 21 | +#' |
12 | 22 | #' @export |
13 | | -makeTiles <- function(tilePath, obj, overwrite = FALSE, ...) { |
14 | | - stopifnot(is(obj) %in% c("Raster", "SpatRaster")) |
15 | | - |
16 | | - if (is(obj, "Raster")) { |
17 | | - obj <- terra::rast(obj) |
18 | | - } |
| 23 | +#' @rdname makeTiles |
| 24 | +makeTiles <- function(tilePath = getOption("map.tilePath", "tiles"), obj, overwrite = FALSE, ...) { |
| 25 | + if (canMakeTiles()) { |
| 26 | + stopifnot(is(obj) %in% c("Raster", "SpatRaster")) |
19 | 27 |
|
20 | | - dirNotExist <- !dir.exists(tilePath) | isTRUE(overwrite) |
| 28 | + if (is(obj, "Raster")) { |
| 29 | + obj <- terra::rast(obj) |
| 30 | + } |
21 | 31 |
|
22 | | - if (!is.na(tilePath) && dirNotExist) { |
23 | | - ## assume that tilePath is unique for that obj, via .robustDigest |
24 | | - message(" Creating tiles - reprojecting to epsg:4326 (leaflet projection)") |
25 | | - objLflt <- try( |
26 | | - { |
27 | | - ## TODO: using projectTo() fails; reproducible#355 |
28 | | - # reproducible::projectTo(obj, projectTo = sf::st_crs("epsg:4326"), ...) |
29 | | - terra::project(obj, "epsg:4326", ...) |
30 | | - }, |
31 | | - silent = TRUE |
32 | | - ) |
33 | | - fname <- reproducible::Filenames(objLflt) |
| 32 | + dirNotExist <- !dir.exists(tilePath) | isTRUE(overwrite) |
34 | 33 |
|
35 | | - if (length(fname) == 0 | nchar(fname) == 0) { |
36 | | - tmpFile <- tempfile(fileext = ".tif") |
37 | | - message(" writing to disk") |
| 34 | + if (!is.na(tilePath) && dirNotExist) { |
| 35 | + ## assume that tilePath is unique for that obj, via .robustDigest |
| 36 | + message(" Creating tiles - reprojecting to epsg:4326 (leaflet projection)") |
38 | 37 | objLflt <- try( |
39 | 38 | { |
40 | | - terra::writeRaster(objLflt, tmpFile) |
| 39 | + ## TODO: using projectTo() fails; reproducible#355 |
| 40 | + # reproducible::projectTo(obj, projectTo = sf::st_crs("epsg:4326"), ...) |
| 41 | + terra::project(obj, "epsg:4326", ...) |
41 | 42 | }, |
42 | 43 | silent = TRUE |
43 | 44 | ) |
44 | | - } else { |
45 | | - tmpFile <- fname |
46 | | - } |
| 45 | + fname <- reproducible::Filenames(objLflt) |
47 | 46 |
|
48 | | - toDo <- TRUE |
49 | | - tryNum <- 1 |
50 | | - while (toDo) { |
51 | | - print(tryNum) |
52 | | - isCorrectCRS <- terra::same.crs("epsg:4326", objLflt) |
53 | | - out <- try( |
54 | | - { |
55 | | - tiler::tile(tmpFile, tilePath, |
56 | | - zoom = "1-10", |
57 | | - crs = as(sf::st_crs("epsg:4326"), "CRS"), |
58 | | - format = "tms", viewer = FALSE, resume = TRUE |
59 | | - ) |
60 | | - }, |
61 | | - silent = TRUE |
62 | | - ) |
63 | | - toDo <- is(out, "try-error") |
64 | | - files <- dir(tilePath, recursive = TRUE) |
65 | | - if (length(files) < 5) { |
66 | | - unlink(tilePath, recursive = TRUE) |
67 | | - toDo <- TRUE |
| 47 | + if (length(fname) == 0 || nchar(fname) == 0) { |
| 48 | + tmpFile <- tempfile(fileext = ".tif") |
| 49 | + message(" writing to disk") |
| 50 | + objLflt <- try( |
| 51 | + { |
| 52 | + terra::writeRaster(objLflt, tmpFile) |
| 53 | + }, |
| 54 | + silent = TRUE |
| 55 | + ) |
| 56 | + } else { |
| 57 | + tmpFile <- fname |
68 | 58 | } |
69 | 59 |
|
70 | | - tryNum <- tryNum + 1 |
| 60 | + toDo <- TRUE |
| 61 | + tryNum <- 1 |
| 62 | + maxRetries <- getOption("map.tileRetry", 3L) |
| 63 | + while (toDo && tryNum <= maxRetries) { |
| 64 | + print(tryNum) |
| 65 | + isCorrectCRS <- terra::same.crs("epsg:4326", objLflt) |
| 66 | + out <- try( |
| 67 | + { |
| 68 | + tiler::tile( |
| 69 | + file = tmpFile, |
| 70 | + tiles = tilePath, |
| 71 | + zoom = "1-10", |
| 72 | + crs = as(sf::st_crs("epsg:4326"), "CRS"), |
| 73 | + resume = TRUE, viewer = FALSE, georef = TRUE |
| 74 | + ) |
| 75 | + }, |
| 76 | + silent = TRUE |
| 77 | + ) |
| 78 | + toDo <- is(out, "try-error") |
| 79 | + files <- dir(tilePath, recursive = TRUE) |
| 80 | + if (length(files) < 5) { |
| 81 | + unlink(tilePath, recursive = TRUE) |
| 82 | + toDo <- TRUE |
| 83 | + } |
| 84 | + |
| 85 | + tryNum <- tryNum + 1 |
| 86 | + } |
| 87 | + } else { |
| 88 | + message(" Tiles - skipping creation - directory") |
| 89 | + message(reproducible::normPath(tilePath)) |
| 90 | + message(" already exists or is not specified") |
71 | 91 | } |
72 | 92 | } else { |
73 | | - message(" Tiles - skipping creation - directory") |
74 | | - message(reproducible::normPath(tilePath)) |
75 | | - message(" already exists or is not specified") |
| 93 | + message(" Tiles - skipping creation - python or gdal support unavailable") |
76 | 94 | } |
| 95 | + |
| 96 | + return(invisible(NULL)) |
| 97 | +} |
| 98 | + |
| 99 | +#' @export |
| 100 | +#' @rdname makeTiles |
| 101 | +canMakeTiles <- function() { |
| 102 | + all(nzchar(tiler::tiler_options())) |
77 | 103 | } |
0 commit comments