Easymode44
Easymode44

Reputation: 148

How does R assign a resolution to raster objects?

Suppose one runs the following R code

install.packages("raster")
library(raster)
r <- raster(ncol=18, nrow=18)
res(r)

The output of the res function is

[1] 20 10

How are these values defined? How does the raster function calculate them? In what units are they expressed?

Upvotes: 0

Views: 552

Answers (2)

Robert Hijmans
Robert Hijmans

Reputation: 47146

As pointed out by Guillaume Devailly, the horizontal resolution is the horizontal extent divided by the number of columns. The vertical resolution is the vertical extent divided by the number of rows. The units are the units of your coordinate reference system. The default is degrees (for longitude/latitude). To add more to Guillaume's answer:

Create a raster with 10 rows and columns that goes from 0 to 10. The resolution is 1.

library(raster)
r <- raster(ncol=10, nrow=10, xmn=0, xmx=10, ymn=0, ymx=10)
r
#class      : RasterLayer 
#dimensions : 10, 10, 100  (nrow, ncol, ncell)
#resolution : 1, 1  (x, y)
#extent     : 0, 10, 0, 10  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

Change the resolution to 0.5; the number of rows and columns double

res(r) <- 0.5
r
#class      : RasterLayer 
#dimensions : 20, 20, 400  (nrow, ncol, ncell)
#resolution : 0.5, 0.5  (x, y)
#extent     : 0, 10, 0, 10  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 

You can change the resolution indirectly by adjusting the extent

extent(r) <- c(0,5,0,5)
r
#class      : RasterLayer 
#dimensions : 20, 20, 400  (nrow, ncol, ncell)
#resolution : 0.25, 0.25  (x, y)
#extent     : 0, 5, 0, 5  (xmin, xmax, ymin, ymax)
#crs        : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0

The x and y resolution can be set to a different value

res(r) <- c(1, 0.5)

When you change the resolution directly, via res any cell values associated with the Raster* object are lost; because the number of rows or columns has to change. If you change it indirectly, by changing the extent, the values stay.

Upvotes: 2

Guillaume Devailly
Guillaume Devailly

Reputation: 191

From what I understand from the vignette

The default settings will create a global raster data structure with a longitude/latitude coordinate reference system and 1 by 1 degree cells.

r
# class       : RasterLayer 
# dimensions  : 18, 18, 324  (nrow, ncol, ncell)
# resolution  : 20, 10  (x, y)
# extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 

r x extent goes to from -180 to +180 degrees by default (a total of 360 degrees), and 360 degrees / 18 points = a x resolution of 20 degrees.

r y extent goes form -90 to +90 degrees by default, and 180 degrees / 18 points results in a y resolution of 10 degrees.

Upvotes: 1

Related Questions