如何用xy坐标从rasterstack中提取值?

时间:2021-09-07 21:23:56

I have a rasterstack (5 raster layers) that actually is a time series raster.

我有一个rasterstack(5个栅格图层)实际上是一个时间序列栅格。

r <- raster(nrow=20, ncol=200)
s <- stack( sapply(1:5, function(i) setValues(r, rnorm(ncell(r), i, 3) )) )
s

class       : RasterStack
dimensions  : 20, 200, 4000, 5  (nrow, ncol, ncell, nlayers)
resolution  : 1.8, 9  (x, y)
extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0 
names       :   layer.1,   layer.2,   layer.3,   layer.4,   layer.5
min values  : -9.012146, -9.165947, -9.707269, -7.829763, -5.332007
max values  :  11.32811,  11.97328,  15.99459,  15.66769,  16.72236

My objective is to plot each pixel and explore their behavior over time.

我的目标是绘制每个像素并探索它们随时间的行为。

How could I extract each pixels together with their x,y coordinates and plot a time series curve?

如何将每个像素与x,y坐标一起提取并绘制时间序列曲线?

2 个解决方案

#1


9  

You can use extract and pass a vector of cell numbers you wish to extract to return a matrix of values in each pixel. Each row represents a pixel, the columns are layers...

您可以使用提取并传递要提取的单元格数字向量,以返回每个像素中的值矩阵。每行代表一个像素,列是图层......

mat <- extract( s , 1:ncell(s) )
head( mat )
#        layer.1  layer.2  layer.3    layer.4   layer.5
#[1,] -0.2138718 3.114061 3.670945  1.2560295  2.881104
#[2,]  3.3580783 5.008205 2.315353  2.3247236 11.539837
#[3,]  3.2173875 2.958985 1.055389  3.1016730  4.064339
#[4,]  4.1113162 4.469828 3.113790  8.5329679  8.771459
#[5,] -2.4011283 4.747527 4.299707  2.2111643  9.457012
#[6,] -2.6159294 5.659211 1.926900 -0.3886837  5.661419

However extract is more useful when trying to get particular pixels. To get all pixels with the x / y coordinates you can just use rasterToPoints...

但是,在尝试获取特定像素时,提取更有用。要获得具有x / y坐标的所有像素,您只需使用rasterToPoints ...

head( rasterToPoints( s ) )
#          x    y    layer.1  layer.2  layer.3    layer.4   layer.5
#[1,] -179.1 85.5 -0.2138718 3.114061 3.670945  1.2560295  2.881104
#[2,] -177.3 85.5  3.3580783 5.008205 2.315353  2.3247236 11.539837
#[3,] -175.5 85.5  3.2173875 2.958985 1.055389  3.1016730  4.064339
#[4,] -173.7 85.5  4.1113162 4.469828 3.113790  8.5329679  8.771459
#[5,] -171.9 85.5 -2.4011283 4.747527 4.299707  2.2111643  9.457012
#[6,] -170.1 85.5 -2.6159294 5.659211 1.926900 -0.3886837  5.661419

#2


5  

Thanks a lot @SimonO101 !

非常感谢@ SimonO101!

This code works.

这段代码有效。

r <- raster(nrow=10, ncol=10)

s <- stack( sapply(1:5, function(i) setValues(r, rnorm(ncell(r), i, 3) )) )

s[1:3]<-NA

vals<-extract(s,1:ncell(s))

coord<-xyFromCell(s,1:ncell(s))

combine<-cbind(coord,vals)

write.table(combine,"xyvalues.txt") 

#1


9  

You can use extract and pass a vector of cell numbers you wish to extract to return a matrix of values in each pixel. Each row represents a pixel, the columns are layers...

您可以使用提取并传递要提取的单元格数字向量,以返回每个像素中的值矩阵。每行代表一个像素,列是图层......

mat <- extract( s , 1:ncell(s) )
head( mat )
#        layer.1  layer.2  layer.3    layer.4   layer.5
#[1,] -0.2138718 3.114061 3.670945  1.2560295  2.881104
#[2,]  3.3580783 5.008205 2.315353  2.3247236 11.539837
#[3,]  3.2173875 2.958985 1.055389  3.1016730  4.064339
#[4,]  4.1113162 4.469828 3.113790  8.5329679  8.771459
#[5,] -2.4011283 4.747527 4.299707  2.2111643  9.457012
#[6,] -2.6159294 5.659211 1.926900 -0.3886837  5.661419

However extract is more useful when trying to get particular pixels. To get all pixels with the x / y coordinates you can just use rasterToPoints...

但是,在尝试获取特定像素时,提取更有用。要获得具有x / y坐标的所有像素,您只需使用rasterToPoints ...

head( rasterToPoints( s ) )
#          x    y    layer.1  layer.2  layer.3    layer.4   layer.5
#[1,] -179.1 85.5 -0.2138718 3.114061 3.670945  1.2560295  2.881104
#[2,] -177.3 85.5  3.3580783 5.008205 2.315353  2.3247236 11.539837
#[3,] -175.5 85.5  3.2173875 2.958985 1.055389  3.1016730  4.064339
#[4,] -173.7 85.5  4.1113162 4.469828 3.113790  8.5329679  8.771459
#[5,] -171.9 85.5 -2.4011283 4.747527 4.299707  2.2111643  9.457012
#[6,] -170.1 85.5 -2.6159294 5.659211 1.926900 -0.3886837  5.661419

#2


5  

Thanks a lot @SimonO101 !

非常感谢@ SimonO101!

This code works.

这段代码有效。

r <- raster(nrow=10, ncol=10)

s <- stack( sapply(1:5, function(i) setValues(r, rnorm(ncell(r), i, 3) )) )

s[1:3]<-NA

vals<-extract(s,1:ncell(s))

coord<-xyFromCell(s,1:ncell(s))

combine<-cbind(coord,vals)

write.table(combine,"xyvalues.txt")