First we are goin to create a raster
# create a raster from the matrix - a "blank" raster of 4x4
myRaster1 <- raster(nrow=4, ncol=4)
# assign "data" to raster: 1 to n based on the number of cells in the raster
myRaster1[]<- 1:ncell(myRaster1)
# view attributes of the raster
myRaster1
## class : RasterLayer
## dimensions : 4, 4, 16 (nrow, ncol, ncell)
## resolution : 90, 45 (x, y)
## extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : memory
## names : layer
## values : 1, 16 (min, max)
# what is the raster extent?
myRaster1@extent
## class : Extent
## xmin : -180
## xmax : 180
## ymin : -90
## ymax : 90
# plot raster
plot(myRaster1, main="Original Raster with 16 pixels")
Now we are goin to resample this raster to a higer resolution (more pixels)
## HIGHER RESOLUTION
# Create 32 pixel raster
myRaster2.b <- raster(nrow=8, ncol=8)
myRaster2.n <- raster(nrow=8, ncol=8)
# resample 16 pix raster with 32 pix raster
# use bilinear interpolation with our numeric data
myRaster2.b <- resample(myRaster1, myRaster2.b, method='bilinear')
myRaster2.n <- resample(myRaster1, myRaster2.n, method="ngb" )
# notice new dimensions, resolution, & min/max
myRaster2.b
## class : RasterLayer
## dimensions : 8, 8, 64 (nrow, ncol, ncell)
## resolution : 45, 22.5 (x, y)
## extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : memory
## names : layer
## values : -0.25, 17.25 (min, max)
myRaster2.n
## class : RasterLayer
## dimensions : 8, 8, 64 (nrow, ncol, ncell)
## resolution : 45, 22.5 (x, y)
## extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : memory
## names : layer
## values : 1, 16 (min, max)
# plot
par(mfrow=c(1,2))
plot(myRaster2.b, main="Raster with 32 pixels bilinear")
plot(myRaster2.n, main="Raster with 32 pixels nearest neighbor")
Note that in the raster with 32 pixels bilinear we have a gradient in the pixels created by the interpolation, this information is extrapolated (not real)
## LOWER RESOLUTION
myRaster3.b <- raster(nrow=2, ncol=2)
myRaster3.n <- raster(nrow=2, ncol=2)
myRaster3.b <- resample(myRaster1, myRaster3.b, method='bilinear')
myRaster3.n <- resample(myRaster1, myRaster3.n, method="ngb")
myRaster3.b
## class : RasterLayer
## dimensions : 2, 2, 4 (nrow, ncol, ncell)
## resolution : 180, 90 (x, y)
## extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : memory
## names : layer
## values : 3.5, 13.5 (min, max)
myRaster3.n
## class : RasterLayer
## dimensions : 2, 2, 4 (nrow, ncol, ncell)
## resolution : 180, 90 (x, y)
## extent : -180, 180, -90, 90 (xmin, xmax, ymin, ymax)
## crs : +proj=longlat +datum=WGS84 +no_defs
## source : memory
## names : layer
## values : 6, 16 (min, max)
# plot
par(mfrow=c(1,2))
plot(myRaster3.b, main="Raster with 4 pixels bilinear")
plot(myRaster3.n, main="Raster with 4 pixels nearest neighbor")
In this case the different methods don´t have differences
# change graphical parameter to 2x3 grid
par(mfrow=c(2,3))
# arrange plots in order you wish to see them
plot(myRaster2.b, main="Raster with 32 pixels bilinear")
plot(myRaster1, main="Original Raster with 16 pixels")
plot(myRaster3.b, main="Raster with 4 pixels bilinear")
plot(myRaster2.n, main="Raster with 32 pixels nearest neighbor")
plot(myRaster1, main="Original Raster with 16 pixels")
plot(myRaster3.n, main="Raster with 4 pixels nearest neighbor")
We have to explore the incidences of using Donwnscaling or Upscaling in the transformation of our environmental layers, also the effect of this transformation in continuous and discrete variables is different.
Last modified “17 agosto, 2021”.