1

I've analyzed multiple regions separately using rasters. All those regions are neighbours. That means every pixel from one region has a neighbour from another region, like the picture below implies. Now I want to merge all rasters, which I originally separated through a mask.

Through a shapefile pixels were masked for the purple region in a way, that the masked pixels in the purple area have another resolution, than the pixels in neighbouring areas while having originally the same resolution (Implied through the picture).

Originally I've used this code to merge all rasters with the same resolution:

x <- list(brick.referenz.1, brick.referenz.2) 
brick.referenz <- do.call(merge, x) 

But it doesnt work anymore, because the resolution changed for the purple region, because of the masking. The purple area contains two pixels. One pixel in the upper half of the purple area and one pixel in the lower half of the purple area, which are separated from each other

The resulting error message:

Error in compareRaster(x, extent = FALSE, rowcol = FALSE, orig = TRUE,  : 
  different resolution

Is there an easy way to solve this problem? Maybe merging only the rasters for the purple area and red area in the beginning, because the pixels are neighbours?

There is example data below. Regarding this thread the version 2.5-11 of the raster package is needed to use the output of dput().

Example data created with dput() for one layer:

Raster with 0.22° resolution:

new("RasterLayer"
    , file = new(".RasterFile"
    , name = ""
    , datanotation = "FLT4S"
    , byteorder = "little"
    , nodatavalue = -Inf
    , NAchanged = TRUE
    , nbands = 1L
    , bandorder = "BIL"
    , offset = 0L
    , toptobottom = TRUE
    , blockrows = 0L
    , blockcols = 0L
    , driver = ""
    , open = FALSE
)
    , data = new(".SingleLayerData"
    , values = c(NA, 0.283883883410455, 0.294024842556485, NA)
    , offset = 0
    , gain = 1
    , inmemory = TRUE
    , fromdisk = FALSE
    , isfactor = FALSE
    , attributes = list()
    , haveminmax = TRUE
    , min = 0.283883883410455
    , max = 0.294024842556485
    , band = 1L
    , unit = ""
    , names = "glob_CNRM.CERFACS.CNRM.CM5_historical_r1i1p1_CLMcom.CCLM4.8.17_v1_day_merged_3.1"
)
    , legend = new(".RasterLegend"
    , type = character(0)
    , values = logical(0)
    , color = logical(0)
    , names = logical(0)
    , colortable = logical(0)
)
    , title = character(0)
    , extent = new("Extent"
    , xmin = 9.37
    , xmax = 9.81
    , ymin = 52.81
    , ymax = 53.25
)
    , rotated = FALSE
    , rotation = new(".Rotation"
    , geotrans = numeric(0)
    , transfun = function () 
NULL
)
    , ncols = 2L
    , nrows = 2L
    , crs = new("CRS"
    , projargs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
)
    , history = list()
    , z = list()
)

Raster with 0.11° resolution:

new("RasterLayer"
    , file = new(".RasterFile"
    , name = ""
    , datanotation = "FLT4S"
    , byteorder = "little"
    , nodatavalue = -Inf
    , NAchanged = TRUE
    , nbands = 1L
    , bandorder = "BIL"
    , offset = 0L
    , toptobottom = TRUE
    , blockrows = 0L
    , blockcols = 0L
    , driver = ""
    , open = FALSE
)
    , data = new(".SingleLayerData"
    , values = c(NA, NA, 0.283883883410457, 0.283883883410458, 0.283883883410461, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.288954362983471, 0.288954362983471, 
0.288954362983473, 0.288954362983476, 0.28895436298348, NA, NA, 
NA, NA, NA, NA, NA, 0.294024842556486, 0.294024842556486, 0.294024842556487, 
0.294024842556488, 0.294024842556491, 0.294024842556495, 0.294024842556502, 
NA, NA, NA, NA, NA, NA, NA, 0.299095322129501, 0.299095322129502, 
0.299095322129503, 0.299095322129506, 0.29909532212951, 0.299095322129518, 
0.299095322129529, 0.299095322129546, 0.29909532212957, NA, NA, 
NA, NA, NA, NA, NA, 0.396667733373416, 0.411224740314603, 0.425609272845979, 
0.43983197866927, 0.453767877325813, 0.467589267293576, 0.481143271441349, 
0.494515652722964, NA, NA, NA, NA, NA, NA, NA, NA, 0.550928917809292, 
0.538054318616434, 0.538054318616475, 0.53805431861653, 0.538054318616608, 
0.538054318616714, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.636225899817969, 
0.630242094982257, NA)
    , offset = 0
    , gain = 1
    , inmemory = TRUE
    , fromdisk = FALSE
    , isfactor = FALSE
    , attributes = list()
    , haveminmax = TRUE
    , min = 0.283883883410457
    , max = 0.636225899817969
    , band = 1L
    , unit = ""
    , names = "glob_CNRM.CERFACS.CNRM.CM5_historical_r1i1p1_CLMcom.CCLM4.8.17_v1_day_merged_5.1"
)
    , legend = new(".RasterLegend"
    , type = character(0)
    , values = logical(0)
    , color = logical(0)
    , names = logical(0)
    , colortable = logical(0)
)
    , title = character(0)
    , extent = new("Extent"
    , xmin = 9.535
    , xmax = 10.965
    , ymin = 52.425
    , ymax = 53.195
)
    , rotated = FALSE
    , rotation = new(".Rotation"
    , geotrans = numeric(0)
    , transfun = function () 
NULL
)
    , ncols = 13L
    , nrows = 7L
    , crs = new("CRS"
    , projargs = "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
)
    , history = list()
    , z = list()
)
0

You may first want to resample your coarse resolution raster using the finer resolution one as reference.

# resample "brick.referenz.2"
brick.referenz.2.resample <- resample(brick.referenz.2, brick.referenz.1, method = "bilinear")
# merge
r3 <- merge(brick.referenz.1, brick.referenz.2.resample)
plot(r3)

You need to be sure that the extent of the reference raster covers the extent of the coarse. But, if you created them with masks, this should be the case. Otherwise, you'll need to create an empty raster with the size of the area and the resolution targeted to be used as reference.

0

I found a really simple work around for my problem:

 brick.referenz.1.mat <- rasterToPoints(brick.referenz.1)
 brick.referenz.2.mat <- rasterToPoints(brick.referenz.2)
 brick.referenz.merged <- rbind(brick.referenz.1, brick.referenz.2)

brick.referenz.merged <- rasterFromXYZ(brick.referenz.merged,  crs = crs(brick.referenz.1))

I just transformed the rasters to matrices, which include the coordinates in the first 2 columns. Then I've used rbind to merge both matrices and created a new raster.

Note: This makes only sense, if the rasters originally had the same resolution and it only changed for instance through masking grid data (e.g. nc-files). That was the case for me as the picture above shows. You also have to redefine the CRS, because the information gets lost, when you use rasterToPoints

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.