@@ -74,19 +74,22 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
74
74
# ' @keywords internal
75
75
# ' @noRd
76
76
.expand_ext <- function (ext.both , rg ){
77
- if (which.min(rg ) == 1 ){
78
- ext.both [[which.min(rg )]]@ xmin <- - 180 + ext.both [[which.min(rg )]]@ xmin - 180
79
- ext.both [[which.min(rg )]]@ xmax <- - 180 + ext.both [[which.min(rg )]]@ xmax - 180
80
- } else {
81
- ext.both [[which.max(rg )]]@ xmin <- 180 + ext.both [[which.max(rg )]]@ xmin + 180
82
- ext.both [[which.max(rg )]]@ xmax <- 180 + ext.both [[which.max(rg )]]@ xmax + 180
77
+
78
+ if (ext.both [[which.max(rg )]]@ xmin < 0 ){
79
+ ext.both [[which.max(rg )]]@ xmin <- ext.both [[which.max(rg )]]@ xmin - rg [which.min(rg )]
80
+ ext.both [[which.min(rg )]]@ xmax <- ext.both [[which.min(rg )]]@ xmax + rg [which.max(rg )]
81
+ }else {
82
+ ext.both [[which.max(rg )]]@ xmax <- ext.both [[which.max(rg )]]@ xmax + rg [which.min(rg )]
83
+ ext.both [[which.min(rg )]]@ xnub <- ext.both [[which.min(rg )]]@ xmin - rg [which.max(rg )]
83
84
}
85
+
84
86
return (ext.both )
85
87
}
86
88
89
+
87
90
# ' get map
88
91
# ' @importFrom slippymath bbox_to_tile_grid tile_bbox
89
- # ' @importFrom raster extent extent<- resample extend merge brick
92
+ # ' @importFrom raster extent extent<- resample extend merge brick projectRaster
90
93
# ' @importFrom magick image_read image_write image_convert
91
94
# ' @importFrom curl curl_download
92
95
# ' @importFrom httr http_error GET
@@ -99,6 +102,7 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
99
102
extras <- list (... )
100
103
if (! is.null(extras $ no_transform )) no_transform <- extras $ no_transform else no_transform <- FALSE
101
104
if (! is.null(extras $ no_crop )) no_crop <- extras $ no_crop else no_crop <- FALSE
105
+ if (! is.null(extras $ custom_crs )) custom_crs <- extras $ custom_crs else custom_crs <- NA
102
106
103
107
if (inherits(ext , " bbox" )) ext <- list (ext )
104
108
file_comp <- lapply(ext , function (y ){
@@ -226,29 +230,82 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
226
230
if (length(file_comp ) > 1 ){
227
231
228
232
# load and name
229
- r <- lapply(file_comp , brick )
230
- names(r ) <- names(ext )
233
+ r <- r_as_is <- lapply(unname(file_comp ), brick )
231
234
232
- # extend over dateline
233
- ext.both <- list (east = extent(r $ east ), west = extent(r $ west ))
234
- rg <- c(" east" = diff(c(ext.both $ east @ xmin , ext.both $ east @ xmax )), " west" = diff(c(ext.both $ west @ xmin , ext.both $ west @ xmax )))
235
-
236
- ext.both <- .expand_ext(ext.both , rg )
237
- # ext.both <- .shift_ext(ext.both)
238
- extent(r $ east ) <- ext.both $ east
239
- extent(r $ west ) <- ext.both $ west
240
-
241
- # extend lower res raster, resample higher res raster and merge both
242
- ext.combi <- .combine_ext(ext.both )
243
-
235
+ # get original extents of untouched rasters
236
+ ext.both <- lapply(r , extent )
237
+
238
+ # measure x diff, which side should be preserved, whcih side should be extended to the other?
239
+ rg <- sapply(ext.both , function (x ) diff(c(x @ xmin , x @ xmax )))
240
+
241
+ # save the x end coodinate of this grid
242
+ cc.xmin <- ext.both [[which.max(rg )]]@ xmin
243
+
244
+ # expand both extents
245
+ ext.both.exp <- .expand_ext(ext.both , rg )
246
+
247
+ # choose an extent
248
+ ext.combi <- ext.both.exp [[which.max(rg )]]
249
+
250
+ # extend the larger one
251
+ r [[which.max(rg )]] <- extend(r [[which.max(rg )]], ext.combi )
252
+
253
+ # shift the smaller one over to the other side
254
+ ext.min <- extent(r [[which.min(rg )]])
255
+ ext.min @ xmax <- cc.xmin
256
+ ext.min @ xmin <- cc.xmin - min(rg )
257
+
258
+ extent(r [[which.min(rg )]]) <- ext.min
259
+
260
+ # extent the smaller one too, resample to larger one
244
261
r [[which.min(rg )]] <- extend(r [[which.min(rg )]], ext.combi )
245
- r [[which.max(rg )]] <- resample(r [[which.max(rg )]], r [[which.min(rg )]])
246
- r <- list (merge(r [[1 ]], r [[2 ]]))
262
+ r [[which.min(rg )]] <- resample(r [[which.min(rg )]], r [[which.max(rg )]])
263
+
264
+ # fuse rasters over grid end
265
+ r <- merge(r [[1 ]], r [[2 ]])
266
+
267
+ # if another CRS is equested, we need to do some tricks, since we cannot reproject the "shifted" raster
268
+ if (! is.na(custom_crs )){
269
+
270
+ # shift extent onto one side of the coordinate line
271
+ ext.repro <- extent(r )
272
+ if (cc.xmin < 0 ){
273
+ ext.repro @ xmin <- ext.repro @ xmin + rg [which.min(rg )]
274
+ ext.repro @ xmax <- ext.repro @ xmax + rg [which.min(rg )]
275
+ } else {
276
+ ext.repro @ xmin <- ext.repro @ xmin - rg [which.min(rg )]
277
+ ext.repro @ xmax <- ext.repro @ xmax - rg [which.min(rg )]
278
+ }
279
+ # project shifted raster
280
+ extent(r ) <- ext.repro
281
+ r <- projectRaster(r , crs = custom_crs )
282
+
283
+ # now project the original extents of the two rasters
284
+ ext.before <- lapply(r_as_is , function (x ) extent(projectRaster(x , crs = custom_crs )))
285
+
286
+ # combine these two as before
287
+ rg <- sapply(ext.before , function (x ) diff(c(x @ xmin , x @ xmax )))
288
+ ext.before.exp <- .expand_ext(ext.before , rg )
289
+
290
+ # assign equivialnt extent
291
+ extent(r ) <- ext.before.exp [[which.max(rg )]]
292
+ }
247
293
248
294
file_comp <- paste0(map_dir , " basemap_" , gsub(" :" , " " , gsub(" " , " " , gsub(" -" , " " , Sys.time()))), " .tif" )
249
295
write_stars(st_as_stars(r ), file_comp )
250
296
} else {
251
- file_comp <- file_comp [[1 ]]
297
+
298
+ # custom crs?
299
+ if (! is.na(custom_crs )){
300
+ r <- brick(file_comp [[1 ]])
301
+ r <- projectRaster(r , crs = custom_crs )
302
+
303
+ file_comp <- paste0(map_dir , " basemap_" , gsub(" :" , " " , gsub(" " , " " , gsub(" -" , " " , Sys.time()))), " .tif" )
304
+ write_stars(st_as_stars(r ), file_comp )
305
+ } else {
306
+
307
+ file_comp <- file_comp [[1 ]]
308
+ }
252
309
}
253
310
254
311
# rename layers
0 commit comments