Skip to content

Commit 4bf728c

Browse files
committed
Merge branch 'development'
2 parents 185cdbc + 21b06a1 commit 4bf728c

19 files changed

Lines changed: 231 additions & 136 deletions

DESCRIPTION

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ URL:
99
https://predictiveecology.github.io/map/,
1010
https://github.com/PredictiveEcology/map
1111
Date: 2025-07-28
12-
Version: 1.1.1
12+
Version: 1.1.2
1313
Authors@R: c(
1414
person("Eliot J B", "McIntire", email = "eliot.mcintire@nrcan-rncan.gc.ca",
1515
role = c("aut"), comment=c(ORCID = "0000-0002-6914-8316")),
@@ -21,7 +21,6 @@ Authors@R: c(
2121
Depends:
2222
R (>= 4.3)
2323
Imports:
24-
backports,
2524
data.table (>= 1.10.4),
2625
digest,
2726
methods,

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export("studyArea<-")
2828
export(.rasterToMemory)
2929
export(addColumnNameForLabels)
3030
export(areaAndPolyValue)
31+
export(canMakeTiles)
3132
export(fasterize2)
3233
export(leafletTiles)
3334
export(makeTiles)
@@ -54,7 +55,6 @@ exportMethods(rasterToMatch)
5455
exportMethods(show)
5556
exportMethods(studyArea)
5657
import(methods)
57-
importFrom(backports,import)
5858
importFrom(data.table,copy)
5959
importFrom(data.table,data.table)
6060
importFrom(data.table,rbindlist)
@@ -119,6 +119,7 @@ importFrom(terra,vect)
119119
importFrom(terra,writeRaster)
120120
importFrom(terra,writeVector)
121121
importFrom(tiler,tile)
122+
importFrom(tiler,tiler_options)
122123
importFrom(tools,file_path_sans_ext)
123124
importFrom(utils,capture.output)
124125
importFrom(utils,getS3method)

NEWS.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
# map 1.1.2
2+
3+
* remove dependency `backports`, as `isFALSE()` available in all supported R versions;
4+
* `makeTiles()` now checks for python and GDAL support, i.e., for Windows, which requires 'OSGeo4W' (#19);
5+
* new option `map.tileRetry` with default value `3L`, specifying the number of attempts to make tiles (#19);
6+
* improved documentation, examples, and tests;
7+
18
# map 1.1.1
29

310
* drop support for R 4.1 and 4.2 due to changes in dependency packages;
411
* move `withr` from Suggests to Imports, as it's used in examples;
512
* improved documentation
613

7-
# map v1.1.0
14+
# map 1.1.0
815

916
* drop support for R 4.0 due to changes in dependency packages;
1017
* remove retiring dependency packages `sp`, `rgeos`, `rgdal` in favour of `terra` + `sf`;
@@ -13,10 +20,11 @@
1320
* use `digest::digest(x, algo = "spooky")` instead of `fastdigest` (which was archived on CRAN);
1421
* remove unused dependencies `knitr` and `rmarkdown`;
1522

16-
# map v0.0.3
23+
# map 0.0.3
1724

18-
* maximum number of threads for parallel operations limited by package option `map.maxNumCores`, which defaults to `min(getOption("Ncpus"), parallel::detectCores())`.
25+
* maximum number of threads for parallel operations limited by package option `map.maxNumCores`,
26+
which defaults to `min(getOption("Ncpus"), parallel::detectCores())`.
1927

20-
# map v0.0.1
28+
# map 0.0.1
2129

2230
* initial development version

R/GIS.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ gdal_polygonizeR <- function(x, outshape = NULL, gdalformat = "ESRI Shapefile",
9999
#'
100100
#' A simple wrapper around [terra::rasterize()].
101101
#'
102-
#' @param emptyRaster An empty `RasterLayer` or`SpatRaster` to use as a template.
102+
#' @param emptyRaster An empty `RasterLayer` or `SpatRaster` to use as a template.
103103
#'
104104
#' @param polygonToFasterize an `sf` or `SpatVector` object, which will be cropped first
105105
#' if `extent(emptyRaster) < extent(polygonToFasterize)`.

R/makeTiles.R

Lines changed: 78 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
#' Make tiles (pyramids) using `gdal2tiles`
1+
#' Make raster tiles (pyramids)
22
#'
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
410
#'
511
#' @param obj A raster object with or without file-backing
612
#'
@@ -9,69 +15,89 @@
915
#'
1016
#' @param ... Arguments passed to [reproducible::projectInputs()] (e.g., `useGDAL`).
1117
#'
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+
#'
1222
#' @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"))
1927

20-
dirNotExist <- !dir.exists(tilePath) | isTRUE(overwrite)
28+
if (is(obj, "Raster")) {
29+
obj <- terra::rast(obj)
30+
}
2131

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)
3433

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)")
3837
objLflt <- try(
3938
{
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", ...)
4142
},
4243
silent = TRUE
4344
)
44-
} else {
45-
tmpFile <- fname
46-
}
45+
fname <- reproducible::Filenames(objLflt)
4746

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
6858
}
6959

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")
7191
}
7292
} 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")
7694
}
95+
96+
return(invisible(NULL))
97+
}
98+
99+
#' @export
100+
#' @rdname makeTiles
101+
canMakeTiles <- function() {
102+
all(nzchar(tiler::tiler_options()))
77103
}

R/map-class.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#'
1717
#' @slot analyses A `data.table` or `data.frame` of the types of analyses to perform.
1818
#'
19-
#' @slot analysesData A `data.table` or `data.frame` of the results of the analyses.
19+
#' @slot analysesData A list of `data.table` or `data.frame` of the results of the analyses.
2020
#'
2121
#' @exportClass map
2222
#' @rdname map-class
@@ -38,21 +38,21 @@ setMethod(
3838
function(.Object, ...) {
3939
.Object <- callNextMethod()
4040
.Object@metadata <- data.table(
41-
layerName = character(),
42-
layerType = character(),
43-
columnNameForLabels = character(),
44-
leaflet = asPath(character()),
45-
studyArea = numeric(),
46-
rasterToMatch = logical()
41+
layerName = character(0),
42+
layerType = character(0),
43+
columnNameForLabels = character(0),
44+
leaflet = asPath(character(0)),
45+
studyArea = numeric(0),
46+
rasterToMatch = logical(0)
4747
)
4848
.Object@CRS <- sf::st_crs()
4949
.Object@paths <- list(
50-
dataPath = getOption("map.dataPath", file.path(getwd(), "data")),
51-
tilePath = getOption("map.tilePath", file.path(getwd(), "tiles"))
50+
dataPath = getOption("map.dataPath", "data"),
51+
tilePath = getOption("map.tilePath", "tiles")
5252
## TODO : scratch/raster/terra path?
5353
)
5454
.Object@analyses <- data.table::data.table(
55-
functionName = character()
55+
functionName = character(0)
5656
)
5757
.Object@analysesData <- list()
5858

R/map-package.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
## usethis namespace: start
55
#' @import methods
6-
#' @importFrom backports import
76
#' @importFrom data.table copy data.table rbindlist set setDT
87
#' @importFrom digest digest
98
#' @importFrom parallel stopCluster
@@ -19,8 +18,7 @@
1918
#' @importFrom stats na.omit
2019
#' @importFrom terra as.polygons ext disagg is.factor levels rast same.crs setValues
2120
#' @importFrom terra values vect writeRaster writeVector
22-
#' @importFrom terra vect
23-
#' @importFrom tiler tile
21+
#' @importFrom tiler tile tiler_options
2422
#' @importFrom tools file_path_sans_ext
2523
#' @importFrom utils capture.output getS3method
2624
#' @importFrom withr deferred_run local_dir local_options local_tempdir

0 commit comments

Comments
 (0)