1
1
# ' Suppress messages and warnings
2
2
# ' @keywords internal
3
3
# ' @noRd
4
- quiet <- function (expr ){
4
+ quiet <- function (expr , no_cat = FALSE ){
5
5
# return(expr)
6
+ if (no_cat ){
7
+ sink(tempfile(), type = " out" )
8
+ on.exit(sink())
9
+ }
6
10
return (suppressWarnings(suppressMessages(expr )))
7
11
}
8
12
@@ -112,12 +116,11 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
112
116
113
117
# ' get map
114
118
# ' @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
121
124
# ' @importFrom methods as
122
125
# ' @keywords internal
123
126
# ' @noRd
@@ -197,7 +200,11 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
197
200
if (all(status == 403 , any(map_service == " osm_thunderforest" , map_service == " maptiler" ))) out(" Authentification failed. Is your map_token correct?" , type = 3 )
198
201
}
199
202
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 ))
201
208
}# utils::download.file(url = url, destfile = file, quiet = T)
202
209
203
210
# test if file can be loaded
@@ -217,19 +224,19 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
217
224
return (file )
218
225
})
219
226
220
- # create composite
227
+ # spatialize PNG and create TIF composite
221
228
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
233
240
234
241
# # TERRA VERSION
235
242
# 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
253
260
# RGB(r) <- 1:3
254
261
# # end temp FIX
255
262
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
+
256
280
if (isFALSE(no_transform )){ # # needed?
257
281
if (as.numeric(tg $ crs $ epsg ) != 3857 ){
258
282
# r <- st_transform(r, crs = tg$crs)
0 commit comments