R 如何为光栅对象分配分辨率?

How does R assign a resolution to raster objects?

假设运行以下 R 代码

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

res函数的输出是

[1] 20 10

这些值是如何定义的? raster 函数如何计算它们?它们以什么单位表示?

据我了解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 范围默认为 -180 到 +180 度(共 360 度),360 度/18 点 = a x分辨率为20度。

r y 默认范围从 -90 度到 +90 度,180 度/18 点导致 y分辨率为10度。

正如 Guillaume Devailly 所指出的,水平分辨率是水平范围除以列数。垂直分辨率是垂直范围除以行数。单位是坐标参考系的单位。默认值为度(对于 longitude/latitude)。要向 Guillaume 的回答添加更多内容:

创建一个包含 10 行和 0 到 10 列的栅格。分辨率为 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 

将分辨率改为0.5;行数和列数加倍

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 

您可以通过调整范围间接改变分辨率

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

x 和 y 分辨率可以设置为不同的值

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

当您通过 res 直接更改分辨率时,与 Raster* 对象关联的所有单元格值都将丢失;因为行数或列数必须更改。如果您间接更改它,通过更改范围,值将保留。