Matching the resolution of two rasters

会有一股神秘感。 提交于 2020-01-15 03:11:28

问题


Im working with two rasters each with a different resolution. Im wondering if there is a more efficient way of matching the coarser raster resolution to the finer raster resolution. Right now I am using the mask function to save some time, clip to the correct extent and change the resolution:

library(raster)
#the raster template with the desired resolution        
r <- raster(extent(-180, 180, -64, 84), res=0.04166667) 
# set some pixels to values, others to NA
r <- setValues(r, sample(c(1:3, NA), ncell(r), replace=TRUE))

#load the raster 
lc_r1 <- raster(r)
res(lc_r1) <- 0.5
values(lc_r1) <- 1:ncell(lc_r1)
lc_r1
##class       : RasterLayer 
##dimensions  : 296, 720, 213120  (nrow, ncol, ncell)
##resolution  : 0.5, 0.5  (x, y)
##extent      : -180, 180, -64, 84  (xmin, xmax, ymin, ymax)
##coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
##data source : in memory
##names       : layer 
##values      : 1, 213120  (min, max)

#create the new finer resolution raster.
lc_r2 <- mask (lc_r1, r2)
Error in compareRaster(x, mask) : different number or columns

Im also trying the disaggregate function in raster but I get this odd error!

lc_r2 <- disaggregate (lc_r1, nrows=3600 )
Error: !is.null(fact) is not TRUE

This seems to work for the time being but not sure if its correct:

lc_r2 <- disaggregate (lc_r1, fact=c(12,12 ), method='bilinear')

回答1:


Why would this Error: !is.null(fact) is not TRUE be odd? If you look at ?disaggregate you will see that there is no argument nrows, but there is a required argument fact, which you did not supply.

You can do

lc_r2a <- disaggregate (lc_r1, fact=12)

Or

lc_r2b <- disaggregate(lc_r1, fact=12, method='bilinear')

which is equivalent to

lc_r2c <- resample(lc_r1, r)

Why are you not sure that this is correct?

However, given that you want to mask lc_r1, the logical approach would be to go the opposite direction and change the resolution of your mask, r,

ra <- aggregate(r, fact=12, na.rm=TRUE) 
lcm <- mask(lc_r1, ra)


来源:https://stackoverflow.com/questions/35624884/matching-the-resolution-of-two-rasters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!