Skip to content

Commit 64716e6

Browse files
committed
fixed issues with combining two maps across the dateline with each other
1 parent e1a8835 commit 64716e6

File tree

3 files changed

+85
-24
lines changed

3 files changed

+85
-24
lines changed

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ importFrom(raster,extent)
3636
importFrom(raster,merge)
3737
importFrom(raster,ncell)
3838
importFrom(raster,nlayers)
39+
importFrom(raster,projectRaster)
3940
importFrom(raster,raster)
4041
importFrom(raster,resample)
4142
importFrom(sf,"st_crs<-")

R/internal.R

+81-24
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,22 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
7474
#' @keywords internal
7575
#' @noRd
7676
.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)]
8384
}
85+
8486
return(ext.both)
8587
}
8688

89+
8790
#' get map
8891
#' @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
9093
#' @importFrom magick image_read image_write image_convert
9194
#' @importFrom curl curl_download
9295
#' @importFrom httr http_error GET
@@ -99,6 +102,7 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
99102
extras <- list(...)
100103
if(!is.null(extras$no_transform)) no_transform <- extras$no_transform else no_transform <- FALSE
101104
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
102106

103107
if(inherits(ext, "bbox")) ext <- list(ext)
104108
file_comp <- lapply(ext, function(y){
@@ -226,29 +230,82 @@ out <- function(input, type = 1, ll = NULL, msg = FALSE, sign = "", verbose = ge
226230
if(length(file_comp) > 1){
227231

228232
# load and name
229-
r <- lapply(file_comp, brick)
230-
names(r) <- names(ext)
233+
r <- r_as_is <- lapply(unname(file_comp), brick)
231234

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
244261
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+
}
247293

248294
file_comp <- paste0(map_dir, "basemap_", gsub(":", "", gsub(" ", "", gsub("-", "", Sys.time()))), ".tif")
249295
write_stars(st_as_stars(r), file_comp)
250296
} 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+
}
252309
}
253310

254311
# rename layers

tests/testthat/test-basemap.R

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ test_that("basemap()", {
2727
# test ext warning on different CRS
2828
expect_warning(basemap(ext_eur, map_dir = map_dir, verbose = T))
2929

30+
# test multiple extents
31+
expect_is(basemap_raster(list(ext), map_dir = map_dir, verbose = F), "RasterBrick")
32+
3033
})
3134

3235
test_that("basemap_stars()", {

0 commit comments

Comments
 (0)