1
votes

I got a raster layer 'w_wgs' of climatic values. On the other side, I created an empty raster 'grid'. I want to assign the values stored in 'w_wgs' into my empty raster grid. I tried merge(), overlay(), over() but these don't work.

How can I do this ? assuming these two rasters don't have the same extent nor resolution (nor equal number of cells, obviously)...but both have the same CRS (WGS84).

Here, a description of my 2 objects :

> extent(w_wgs)
class       : Extent 
xmin        : -64.50344 
xmax        : 74.07016 
ymin        : 12.93039 
ymax        : 72.72534 
> res(w_wgs)
[1] 0.01320 0.00895

> res(grid)
[1] 0.08 0.08
> extent(grid)
class       : Extent 
xmin        : 5 
xmax        : 17.96 
ymin        : 40 
ymax        : 50 
1
Would the extract function be useful to you in this situation?Mike.Gahan
I thought about it, but it seems that this function cannot work using as arguments 2 'RasterLayer' objects. Here what I get : > extract(w_wgs,grid) Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘extract’ for signature ‘"RasterLayer", "RasterLayer"’Remssssss
Can you put all the points you want values for into a SpatialPoints dataframe object and then use the extract function? Can I ask why you want to do this?Mike.Gahan

1 Answers

0
votes

I think you need to use resample

grid <- resample(w_wgs, grid)

A small example that should be similar to your case:

x <- matrix(1:100, nr = 10, nc = 10)
a <- raster(x)

x2 <- matrix(NA, nr = 3, nc = 3)
b <- raster(x2)
# Manually changing the extent and resolution of b
b@extent@xmax <- 0.5
b@extent@ymin <- 0.3

b <- resample(a, b)

par(mfrow = c(2, 1))
plot(a)
plot(extent(b), add = T)
plot(b)