Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the resolution of a raster layer in R

Tags:

I have several high resolution raster layers in R that I am working with. The level of detail is excessive for some of the analyses I am running, so I would like to speed things up by reducing the resolution.

The coordinate system is UTM so the units are meters. The resolution says it is 30, 30 (x, y). So it seems that the resolution here is 30m.

Could someone please advise me on how to change the resolution to 120m instead? I have read the help for the resample() and projectRaster() functions but they seem to require a template raster with the desired resolution, which I don't have.

Here is an example of one of my raster layers:

alt.utm
class : RasterLayer
dimensions : 4572, 2495, 11407140 (nrow, ncol, ncell)
resolution : 30, 30 (x, y)
extent : 421661, 496511, 4402939, 4540099 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=13 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs
data source : in memory
names : layer
values : 1485.127, 4275.202 (min, max)

like image 391
NRP Avatar asked Aug 28 '15 19:08

NRP


People also ask

What is the resolution of a raster in R?

The resolution ( res ) is 1x1 degree. The raster's extent ranges from -100 to -60 degrees longitude and 25 to 50 degrees latitude.

What is raster resolution?

The spatial resolution of a raster refers to the size of the cells in a raster dataset and the ratio of screen pixels to image pixels at the current map scale. For example, one screen pixel can be the result of nine image pixels resampled into one—a raster resolution of 1:9.

How data quality is related to raster cell size?

The higher the resolution of a raster, the smaller the cell size and, thus, the greater the detail.


2 Answers

You can use aggregate or disaggregate.

library(raster)  #get some sample data data(meuse.grid) gridded(meuse.grid) <- ~x+y meuse.raster <- raster(meuse.grid) res(meuse.raster) #[1] 40 40  #aggregate from 40x40 resolution to 120x120 (factor = 3) meuse.raster.aggregate <- aggregate(meuse.raster, fact=3) res(meuse.raster.aggregate) #[1] 120 120  #disaggregate from 40x40 resolution to 10x10 (factor = 4) meuse.raster.disaggregate <- disaggregate(meuse.raster, fact=4) res(meuse.raster.disaggregate) #[1] 10 10 
like image 193
jhhwilliams Avatar answered Sep 23 '22 18:09

jhhwilliams


I've tried 3 different options for upscaling a DEM file. First I used gdal_translate like this:

from CMD: gdal_translate -tr 0.1 0.1 "C:\dem.tif" "C:\dem_0.1.tif" 

In R:

res(raster('C:\dem_0.1.tif') [1] 0.1 0.1 

Then I tried both aggregate and resample commands from raster package in R:

# r <- raster("dem.tif") > res(r) [1] 0.0002777778 0.0002777778 # original dem resolution # r_up_0.1 <- aggregate(r, fact = 0.1/res(r)) # aggregate output > res(r_up_0.1) [1] 0.1 0.1 # s <- raster(nrow = 10, ncol = 10) extent(s) <- extent(r) s <- resample(r, s, method = 'bilinear') # resample output > res(s) [1] 0.1000278 0.1000278 

These are the outputs: The 3 outputs were upscaled to a res = 0.1x0.1 and there are some differences between them but I intend to use the gdal output.

Hope this helps.

like image 25
noriega Avatar answered Sep 21 '22 18:09

noriega