"IDs do not match" 在 SpatialLines 处提取栅格值时出错

"IDs do not match" error when extracting raster values at SpatialLines

我想在 spatialLines 对象穿过其像元的位置提取栅格值,但遇到错误。这说明:

> require(maptools); require(raster)
> data(wrld_simpl)
> nepal = as(wrld_simpl[wrld_simpl$NAME == 'Nepal',], 'SpatialLines')
> r = raster(volcano)
> extent(r) = c(79,90,26,31)
> plot(r); plot(nepal, col='red', add=T)

提取:

> border_values = extract(r, nepal, sp=T, fun=median)
Error in SpatialLinesDataFrame(y, res[, -1, drop = FALSE]) : 
  row.names of data and Lines IDs do not match

知道是什么原因造成的吗?我的光栅包是 v2.5-8.

这是一个需要在光栅包中修复的错误,但这里有一个解决方法:

row.names(nepal) <- '1'
border_values <- extract(r, nepal, sp=TRUE, fun=median, match.ID=FALSE)

或更一般地说,(如果您有多个特征)

row.names(nepal) <- as.character(1:length(nepal))