Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从光栅砖或光栅堆栈提取值时出错_R_R Raster - Fatal编程技术网

R 从光栅砖或光栅堆栈提取值时出错

R 从光栅砖或光栅堆栈提取值时出错,r,r-raster,R,R Raster,从光栅堆栈或光栅砖类的多波段光栅中提取值或点时遇到问题“提取”适用于单个光栅,但在应用于rasterStack或brick时会出现错误 > all.var class : RasterBrick dimensions : 89, 180, 16020, 34 (nrow, ncol, ncell, nlayers) resolution : 2, 2 (x, y) extent : -179, 181, -89, 89 (xmin, xmax, ymin,

光栅堆栈
光栅砖
类的多波段光栅中提取值或点时遇到问题“提取”适用于单个光栅,但在应用于
rasterStack
brick
时会出现错误

> all.var
class       : RasterBrick 
dimensions  : 89, 180, 16020, 34  (nrow, ncol, ncell, nlayers)
resolution  : 2, 2  (x, y)
extent      : -179, 181, -89, 89  (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 
data source : in memory
names       :    period_max, pct_signif_periodmax, pct_coi_periodmax,    pct_ispos_signif, events_pos_periodmax, events_neg_periodmax, events_pos_all, events_neg_all,  maxpower_pos,  maxpower_neg, maxpower_events_pos, maxpower_events_neg, maxpower_pos_norm, maxpower_neg_norm, maxpower_events_pos_norm, ... 

> point
Lon      Lat
1  166.2790 -10.2690
2   26.9000 -33.6000
3  153.6209 -28.7001
4  113.8333 -28.6833
5  153.6335 -28.6591
6  153.5836 -28.4643
7   32.6833 -27.5333
8   32.6880 -27.5260
9   32.6880 -27.5260
10  32.6880 -27.5260


> point.extract<-extract(all.var, point, buffer=50000,na.rm=TRUE,fun=mean)
Error in apply(x, 2, fun2) : dim(X) must have a positive length
>all.var
类别:RasterBrick
尺寸:89、180、16020、34(nrow、ncol、ncell、nlayers)
分辨率:2,2(x,y)
范围:-179181,-89,89(xmin,xmax,ymin,ymax)
库德。参考:+proj=longlat+DATAM=WGS84+no_defs+ellps=WGS84+towgs84=0,0,0
数据源:内存中
名称:周期最大值、pct符号周期最大值、pct符号周期最大值、pct符号周期最大值、pct符号周期最大值、事件负周期最大值、事件负周期最大值、事件全值、事件负全值、事件全值、最大功率全值、最大功率负值、最大功率全值、最大功率全值、最大功率全值、最大功率全值、最大功率全值、最大功率全值。。。
>点
朗拉特
1  166.2790 -10.2690
2   26.9000 -33.6000
3  153.6209 -28.7001
4  113.8333 -28.6833
5  153.6335 -28.6591
6  153.5836 -28.4643
7   32.6833 -27.5333
8   32.6880 -27.5260
9   32.6880 -27.5260
10  32.6880 -27.5260

>要点。摘录请多花点时间问(R)个问题。只要可能,构造一个说明错误的工作R示例。像这样:

library(raster)
b <- brick(nrow=89, ncol=180, nl=34, xmn=-179, xmx=181, ymn=-89, ymx=89, crs="+proj=longlat +datum=WGS84")
b[] <- 1

p <- matrix(c(166.2790,-10.2690,26.9000,-33.6000,153.6209,-28.7001,113.8333,-28.6833,153.6335,-28.6591,153.5836,-28.4643,32.6833,-27.5333,32.6880,-27.5260,32.6880,-27.5260,32.6880,-27.5260), ncol=2, byrow=TRUE)

v <- extract(b, p, buffer=50000, na.rm=TRUE, fun=mean)
库(光栅)

b收到。这项工作非常有效。然而,输出(即v)是向量,而不是代表每层的34列数据帧/矩阵。我遗漏了什么吗?这会给出每个点的平均值(因此向量的长度为10)。要获得每个层的平均值,您可以执行
rowMeans(do.call(cbind,v))
不完全执行。这就得到了全局行的意思。理想情况下,输出应该是一个由10行(xy坐标)和34列(层)尺寸组成的矩阵。这不会得到缓冲区中每个点和层提取的值的平均值(即原始代码中的fun=平均值)……例如,如果您将上述示例中的缓冲区大小增加到150000m,则行尺寸不是10
v <- extract(b, p, buffer=15000000)
# get the mean for each point (buffer) by layer
vv <- lapply(v, function(x) ifelse(is.matrix(x), colMeans(x, na.rm=TRUE), x))
# combine
do.call(rbind, vv)