Guest User

Untitled

a guest
Apr 24th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. require(raster)
  2.  
  3. # round resolution to nearest arc second
  4. snap_res <- function(r) {
  5. round(res(r)*3600)/3600
  6. }
  7.  
  8. # make sure extent fits cleanly onto a grid
  9. # having its origin at -180, -90
  10. snap_extent <- function(r) {
  11. origin <- c(-180, -90)
  12.  
  13. ll <- origin - round((origin - extent(r)[c(1, 3)])/res(r))*res(r)
  14. ur <- ll + res(r)*dim(r)[c(2, 1)]
  15.  
  16. raster::extent(c(ll[1], ur[1], ll[2], ur[2]))
  17. }
  18.  
  19. footprint <- raster('footprint.tif')
  20. yield <- raster('yield.tif')
  21.  
  22. res(footprint) <- snap_res(footprint)
  23. res(yield) <- snap_res(yield)
  24.  
  25. stopifnot(res(footprint) == res(yield))
  26.  
  27. extent(footprint) <- snap_extent(footprint)
  28. extent(yield) <- snap_extent(yield)
  29.  
  30. combined_extent <- merge(extent(footprint), extent(yield))
  31.  
  32. footprint <- extend(footprint, combined_extent)
  33. yield <- extend(yield, combined_extent)
  34.  
  35. stk <- stack(footprint, yield)
Add Comment
Please, Sign In to add comment