Skip to content

Commit aa16d1e

Browse files
committed
streamlined spatialization and compositing, dropped two dependencies
1 parent e4f990a commit aa16d1e

File tree

4 files changed

+69
-29
lines changed

4 files changed

+69
-29
lines changed

DESCRIPTION

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: basemaps
22
Type: Package
33
Title: Accessing Spatial Basemaps in R
4-
Version: 0.0.8
4+
Version: 0.1.0
55
Depends:
66
R (>= 3.5.0)
77
Date: 2024-10-31
@@ -14,15 +14,14 @@ Imports:
1414
sf,
1515
slippymath,
1616
httr,
17-
curl,
17+
magick,
1818
terra,
19-
stars,
2019
pbapply,
21-
magick,
2220
utils,
2321
grDevices,
2422
methods
2523
Suggests:
24+
stars,
2625
raster,
2726
ggplot2,
2827
png,
@@ -31,4 +30,5 @@ Suggests:
3130
testthat,
3231
covr
3332
BugReports: https://github.com/16eagle/basemaps/issues
34-
RoxygenNote: 7.3.1
33+
URL: https://jakob.schwalb-willmann.de/basemaps/
34+
RoxygenNote: 7.3.2

NAMESPACE

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,34 @@ export(get_maptypes)
1818
export(gg_raster)
1919
export(reset_defaults)
2020
export(set_defaults)
21-
importFrom(curl,curl_download)
2221
importFrom(grDevices,col2rgb)
2322
importFrom(grDevices,topo.colors)
2423
importFrom(graphics,plot)
2524
importFrom(httr,GET)
2625
importFrom(httr,http_error)
26+
importFrom(httr,stop_for_status)
27+
importFrom(httr,write_disk)
2728
importFrom(magick,image_convert)
29+
importFrom(magick,image_info)
2830
importFrom(magick,image_read)
2931
importFrom(magick,image_write)
3032
importFrom(methods,as)
3133
importFrom(pbapply,pbapply)
3234
importFrom(pbapply,pboptions)
3335
importFrom(sf,"st_crs<-")
36+
importFrom(sf,gdal_utils)
3437
importFrom(sf,st_as_sfc)
3538
importFrom(sf,st_bbox)
3639
importFrom(sf,st_crop)
3740
importFrom(sf,st_crs)
3841
importFrom(sf,st_transform)
3942
importFrom(slippymath,bbox_to_tile_grid)
4043
importFrom(slippymath,tile_bbox)
41-
importFrom(stars,read_stars)
42-
importFrom(stars,st_mosaic)
43-
importFrom(stars,st_set_bbox)
4444
importFrom(terra,"RGB<-")
4545
importFrom(terra,"ext<-")
4646
importFrom(terra,aggregate)
4747
importFrom(terra,as.array)
48+
importFrom(terra,as.raster)
4849
importFrom(terra,crop)
4950
importFrom(terra,ext)
5051
importFrom(terra,extend)

NEWS.md

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
***
22

3+
## basemaps 0.1.0
4+
Decreasing dependencies, minor improvements
5+
6+
**Changes:**
7+
8+
* updated partly outdated documentation examples
9+
* updated `README` to include an animated map type preview
10+
* changed the way raster tiles are geo-located and mosaiced
11+
* tidied up the dependency tree, removed two dependencies
12+
13+
<br>
14+
15+
16+
***
17+
318
## basemaps 0.0.8
419
New map service *Maptiler*, minor improvements
520

R/internal.R

+44-20
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#' Suppress messages and warnings
22
#' @keywords internal
33
#' @noRd
4-
quiet <- function(expr){
4+
quiet <- function(expr, no_cat = FALSE){
55
#return(expr)
6+
if(no_cat){
7+
sink(tempfile(), type = "out")
8+
on.exit(sink())
9+
}
610
return(suppressWarnings(suppressMessages(expr)))
711
}
812

@@ -112,12 +116,11 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
112116

113117
#' get map
114118
#' @importFrom slippymath bbox_to_tile_grid tile_bbox
115-
#' @importFrom magick image_read image_write image_convert
116-
#' @importFrom curl curl_download
117-
#' @importFrom httr http_error GET
118-
#' @importFrom sf st_transform st_bbox st_as_sfc st_crs st_crs<- st_crop
119-
#' @importFrom stars read_stars st_set_bbox st_mosaic
120-
#' @importFrom terra rast ext ext<- mosaic project crop writeRaster extend merge RGB<-
119+
#' @importFrom magick image_read image_write image_convert image_info
120+
#' @importFrom httr http_error GET write_disk stop_for_status
121+
#' @importFrom sf st_transform st_bbox st_as_sfc st_crs st_crs<- st_crop gdal_utils
122+
#' @importFrom terra rast ext ext<- mosaic project crop writeRaster extend merge RGB<- as.raster
123+
#' @importFrom grDevices col2rgb
121124
#' @importFrom methods as
122125
#' @keywords internal
123126
#' @noRd
@@ -197,7 +200,11 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
197200
if(all(status == 403, any(map_service == "osm_thunderforest", map_service == "maptiler"))) out("Authentification failed. Is your map_token correct?", type = 3)
198201
}
199202
if(!file.exists(file)){
200-
tryCatch(curl_download(url = url, destfile = file), error = function(e) out(paste0("Tile download failed: ", e$message), type = 3))
203+
#tryCatch(curl_download(url = url, destfile = file), error = function(e) out(paste0("Tile download failed: ", e$message), type = 3))
204+
tryCatch({
205+
result <- GET(url = url, write_disk(file, overwrite=TRUE))
206+
httr::stop_for_status(result)
207+
}, error = function(e) out(paste0("Tile download failed: ", e$message), type = 3))
201208
}#utils::download.file(url = url, destfile = file, quiet = T)
202209

203210
# test if file can be loaded
@@ -217,19 +224,19 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
217224
return(file)
218225
})
219226

220-
# create composite
227+
# spatialize PNG and create TIF composite
221228

222-
## STARS VERSION
223-
r <- mapply(img = images, x = tg$tiles$x, y = tg$tiles$y, function(img, x, y){
224-
box <- tile_bbox(x, y, tg$zoom)
225-
img_st <- read_stars(img)
226-
img_st <- st_set_bbox(img_st, box)
227-
st_crs(img_st) <- tg$crs
228-
return(img_st)
229-
}, SIMPLIFY = F)
230-
r <- do.call(stars::st_mosaic, r)
231-
r <- as(r, "SpatRaster")
232-
RGB(r) <- 1:3
229+
## STARS VERSION -- works, but dependencies
230+
# r <- mapply(img = images, x = tg$tiles$x, y = tg$tiles$y, function(img, x, y){
231+
# box <- tile_bbox(x, y, tg$zoom)
232+
# img_st <- read_stars(img)
233+
# img_st <- st_set_bbox(img_st, box)
234+
# st_crs(img_st) <- tg$crs
235+
# return(img_st)
236+
# }, SIMPLIFY = F)
237+
# r <- do.call(stars::st_mosaic, r)
238+
# r <- as(r, "SpatRaster")
239+
# RGB(r) <- 1:3
233240

234241
## TERRA VERSION
235242
# r <- mapply(img = images, x = tg$tiles$x, y = tg$tiles$y, function(img, x, y){
@@ -253,6 +260,23 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
253260
# RGB(r) <- 1:3
254261
# # end temp FIX
255262

263+
## TERRA VERSION
264+
images_tif <- mapply(img = images, x = tg$tiles$x, y = tg$tiles$y, function(img, x, y){
265+
box <- tile_bbox(x, y, tg$zoom)
266+
img_mgc <- magick::image_read(img)
267+
img_inf <- magick::image_info(img_mgc)
268+
img_rst <- terra::rast(aperm(array(grDevices::col2rgb(terra::as.raster(img_mgc)), c(3,as.numeric(img_inf["width"]),as.numeric(img_inf["height"]))), c(3,2,1)))
269+
terra::crs(img_rst) <- as.character(tg$crs$wkt)
270+
terra::ext(img_rst) <- c(box[c("xmin", "xmax", "ymin", "ymax")])
271+
272+
img_tif <- gsub(".png", ".tif", img)
273+
terra::writeRaster(img_rst, filename = img_tif, overwrite = T, datatype = "INT1U") #0-255
274+
return(img_tif)
275+
}, SIMPLIFY = F, USE.NAMES = F)
276+
277+
gdal_utils("buildvrt", unlist(images_tif), file_comp, options = c("-vrtnodata", "-9999", "-srcnodata", "nan"),)
278+
r <- terra::rast(file_comp)
279+
256280
if(isFALSE(no_transform)){ ## needed?
257281
if(as.numeric(tg$crs$epsg) != 3857){
258282
#r <- st_transform(r, crs = tg$crs)

0 commit comments

Comments
 (0)