1

I have 5 rasters. I computed the bounding boxes with st_bbox for each of them. I would like to put all the raster-values in one dataframe. For that I figured they should have exactly the same extent. I tried the raster::resample-method, but I'm not really aware if it's just changing the resolution or the extent or what it is acutally doing. So I thought I might simply compute the intersection of these five shapes and crop and mask every raster afterwards on this extent. The extents look like this:

> bboxes
[[1]]
     xmin      ymin      xmax      ymax 
 682878.2 5577687.4  769888.0 5661769.2 

[[2]]
     xmin      ymin      xmax      ymax 
 682883.9 5577682.1  770070.2 5661805.2 

[[3]]
     xmin      ymin      xmax      ymax 
 683022.2 5577718.4  770026.7 5661795.0 

[[4]]
     xmin      ymin      xmax      ymax 
 679258.9 5593985.4  766400.1 5678075.8 

[[5]]
     xmin      ymin      xmax      ymax 
 679306.8 5593986.0  766452.5 5678075.5 

Does anyone has an idea on how to do that?

2
  • Welcome to GIS Stack Exchange! Its best to keep your posts to one clear question so we get one clear answer that is useful to anyone searching for, for example, how to intersect bounding boxes of rasters. I've edited it down for you. Discussion about general ideas and concepts can be done on the chat system.
    – Spacedman
    Jul 3 '20 at 16:04
  • Ah ok great, thanks:)!!
    – Lenn
    Jul 5 '20 at 6:55
2

You can do this in raster by unioning extents. You don't need st_bbox. This way you get back an extent object ready for use in the raster package functions you might need:

> r1 = raster(matrix(1:12,3,4),xmn=.3,xmx=.5,ymn=.2,ymx=.8)
> r2 = raster(matrix(1:12,3,4),xmn=.4,xmx=.5,ymn=.3,ymx=.9)
> union(extent(r1), extent(r2))
class      : Extent 
xmin       : 0.3 
xmax       : 0.5 
ymin       : 0.2 
ymax       : 0.9 

union only works for two arguments, so for more than two you need to do a bit more. If you have a list of rasters, apply extent over them and then use Reduce to recursively use union:

> r3 = raster(matrix(1:12,3,4),xmn=.1,xmx=.5,ymn=.3,ymx=.9)
> Reduce(union, lapply(list(r1,r2,r3), extent))
class      : Extent 
xmin       : 0.1 
xmax       : 0.5 
ymin       : 0.2 
ymax       : 0.9 
1
  • Great this works perfectly!! Thanks a lot:)
    – Lenn
    Jul 5 '20 at 6:56
1

you can simply crop one crop to another and end up with the intersection of all of them. try

r.all <- list(r1,r2,r3,r4,r5) # your 5 rasters

for (r in r.all) {
    xmin <- c(NA,extent(r)[1])
    xmax <- c(NA,extent(r)[2])
    ymin <- c(NA,extent(r)[3])
    ymax <- c(NA,extent(r)[4])
}
#bounding box of the intersection of all rasters    
b.box <- as(extent(max(xmin,na.rm=T), min(xmax,na.rm=T), max(ymin,na.rm=T), min(ymax,na.rm=T)), 'SpatialPolygons')    
for (r in 1:length(r.all)) {
    r.all[[r]] <- crop(r.all[[r]],b.box)
}

the r.all list will contain all the rasters clipped within their intersection.

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.