Skip to content

Commit

Permalink
style: lintr pass
Browse files Browse the repository at this point in the history
  • Loading branch information
rCarto committed Dec 20, 2023
1 parent 56b75d1 commit b3dc210
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 27 deletions.
4 changes: 1 addition & 3 deletions R/get_tiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ get_tiles <- function(x,

# get cached raster if it already exists
ras <- get_cached_raster(filename, forceDownload, verbose)
if(!is.null(ras)){return(ras)}
if (!is.null(ras)) {return(ras)}

# get tile list
tile_grid <- slippymath::bbox_to_tile_grid(res$bbox_lonlat, zoom)
Expand All @@ -132,5 +132,3 @@ get_tiles <- function(x,

return(ras)
}


2 changes: 1 addition & 1 deletion R/plot_tiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ plot_tiles <- function(x, adjust = FALSE, add = FALSE, ...) {
# Add margins if the raster is smaller than the device
# Zoom-in if the raster is larger than the device
if (adjust == TRUE && add == FALSE) {
if (!terra::is.lonlat(ops$x)){
if (!terra::is.lonlat(ops$x)) {
message(paste0("The 'adjust' feature does not work with",
" an unprojected (lon/lat) raster."))
} else {
Expand Down
40 changes: 18 additions & 22 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# test if gdal version is obsolete.
test_gdal_version <- function(){
test_gdal_version <- function() {
v <- gdal()
if (v < "2.2.3") {
stop(paste0("Your GDAL version is ", v,
Expand All @@ -12,12 +12,11 @@ test_gdal_version <- function(){

# test if input is correct
test_input <- function(x) {
ok_classes <- c("sf", "sfc", "bbox","SpatRaster", "SpatVector", "SpatExtent")
ok_classes <- c("sf", "sfc", "bbox", "SpatRaster", "SpatVector", "SpatExtent")
if (!inherits(x, ok_classes)) {
stop(paste0("x should be an sf, sfc, bbox, SpatRaster, ",
"SpatVector or SpatExtent object"),
call. = FALSE
)
call. = FALSE)
}
return(invisible(NULL))
}
Expand Down Expand Up @@ -74,7 +73,7 @@ get_bbox_and_proj <- function(x) {
}

# get fle extension from url
get_extension <- function(q){
get_extension <- function(q) {
# extension management
if (length(grep(".jpg", q)) > 0) {
ext <- "jpg"
Expand Down Expand Up @@ -123,7 +122,7 @@ get_param <- function(provider) {
}

# get zoom
get_zoom <- function(zoom, bbox_lonlat){
get_zoom <- function(zoom, bbox_lonlat) {
# select a default zoom level
if (missing(zoom)) {
gz <- slippymath::bbox_tile_query(bbox_lonlat)
Expand All @@ -133,7 +132,7 @@ get_zoom <- function(zoom, bbox_lonlat){
}

# cache directory
get_cachedir <- function(cachedir, src){
get_cachedir <- function(cachedir, src) {
if (missing(cachedir)) {
cachedir <- tempdir()
}
Expand All @@ -145,25 +144,25 @@ get_cachedir <- function(cachedir, src){
}

# create a filename with hash
get_filename <- function(bbox, zoom, crop, project, cachedir, url){
get_filename <- function(bbox, zoom, crop, project, cachedir, url) {
filename <- digest::digest(paste0(bbox, zoom, crop, project, cachedir, url),
algo = "md5", serialize = FALSE)
full_filename <- file.path(cachedir, paste0(filename, ".tif"))
full_filename
}

# display info if verbose
display_infos <- function(verbose, zoom, citation, cachedir){
display_infos <- function(verbose, zoom, citation, cachedir) {
if (verbose) {
message("Zoom: ", zoom, "\n","Source(s): ", citation, "\n",
message("Zoom: ", zoom, "\n", "Source(s): ", citation, "\n",
"Cache directory: ", cachedir)
}
return(invisible(NULL))
}

# Use cache raster
get_cached_raster <- function(filename, forceDownload, verbose){
if(file.exists(filename) && isFALSE(forceDownload)){
get_cached_raster <- function(filename, forceDownload, verbose) {
if (file.exists(filename) && isFALSE(forceDownload)) {
if (verbose) {
message("The resulting raster is a previously cached raster.")
}
Expand All @@ -178,18 +177,18 @@ get_cached_raster <- function(filename, forceDownload, verbose){
download_tiles <- function(tile_grid, param, apikey, verbose,
cachedir, forceDownload) {
images <- vector("list", length = nrow(tile_grid$tiles))
if (missing(apikey)){apikey <- ""}
if (missing(apikey)) {apikey <- ""}
zoom <- tile_grid$zoom
ext <- param$ext
src <- param$src
cpt <- 0
for (i in seq_along(images)){
for (i in seq_along(images)) {
x <- tile_grid$tiles[i, ]
x <- trimws(x)
outfile <- paste0(cachedir, "/", src, "_", zoom, "_", x[1], "_",
x[2], ".", ext)
if (!file.exists(outfile) || isTRUE(forceDownload)) {
q <- gsub(pattern = "{s}", replacement = sample(param$sub, 1, T),
q <- gsub(pattern = "{s}", replacement = sample(param$sub, 1, TRUE),
x = param$q, fixed = TRUE)
q <- gsub(pattern = "{x}", replacement = x[1], x = q, fixed = TRUE)
q <- gsub(pattern = "{y}", replacement = x[2], x = q, fixed = TRUE)
Expand All @@ -207,10 +206,10 @@ download_tiles <- function(tile_grid, param, apikey, verbose,
}
images[[i]] <- outfile
}
if (verbose){
if (verbose) {
ntiles <- length(images)
message(ntiles, " tile", ifelse(ntiles > 1, "s", ""))
if (cpt != length(images)){
if (cpt != length(images)) {
message("The resulting raster is built with previously cached tiles.")
}
}
Expand Down Expand Up @@ -249,7 +248,7 @@ compose_tiles <- function(tile_grid, images) {
terra::RGB(r_img) <- c(1, 2, 3)
}
# add extent
terra::ext(r_img) <- terra::ext(bbox[c("xmin", "xmax","ymin", "ymax")])
terra::ext(r_img) <- terra::ext(bbox[c("xmin", "xmax", "ymin", "ymax")])
bricks[[i]] <- r_img
}
# if only one tile is needed
Expand All @@ -263,7 +262,7 @@ compose_tiles <- function(tile_grid, images) {
}


project_and_crop_raster <- function(ras, project, res, crop){
project_and_crop_raster <- function(ras, project, res, crop) {
# set the projection
w_mercator <- "epsg:3857"
terra::crs(ras) <- w_mercator
Expand All @@ -290,6 +289,3 @@ project_and_crop_raster <- function(ras, project, res, crop){
RGB(ras) <- 1:3
return(ras)
}



3 changes: 2 additions & 1 deletion man/plot_tiles.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b3dc210

Please sign in to comment.