当尝试计算点周围的土地覆盖比例时,为什么缓冲区函数在 raster::extract 中不起作用?
我正在按照此处列出的步骤操作:(https://gatesdupontvignettes.com/2019/06/16/nlcd-velox-dplyr.html)从点计算比例土地覆盖。
唯一的区别是我在 SpatVector 点周围提取 nlcd。当我使用 raster::extract 和 buffer=1000 (例如)时,我仅获取每个点(而不是缓冲区中的所有点)的土地覆盖类的数据帧。我希望使用 extract 函数为 x 缓冲区内的所有点生成土地覆盖类别的数据帧。这个 raster::extract 函数一定有一些我遗漏的东西,但我不知道是什么。
library(dplyr)
library(rnaturalearth)
library(sf)
# base map
state = ne_states(iso_a2 = "US", returnclass = "sf") %>% # pull admin. bounds. for US
filter(iso_3166_2 == "US-MA") %>% # select Massachusetts
st_transform(crs = "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80
+towgs84=0,0,0,0,0,0,0 +units=m +no_defs") # nlcd crs
# points
Lon<-c(-69.97623, -69.97674)
Lat<-c(41.90238, 41.90267)
x<-cbind(Lon,Lat)
v<-vect(x, crs="+proj=longlat")
# nlcd data
library(FedData)
nlcd = get_nlcd( # fn for pulling NLCD data
template = state, # polygon template for download
label = "4pland", # this is just a character string label
year = 2011, # specify the year (2016 should be available soon)
force.redo = F
)
# plot
p<-project(v,crs(nlcd))
plot(nlcd)
plot(p, add=T)
# extract using raster::extract
library(raster)
nlcd # this is a SpatRaster
p # this a SpatVector
ex.1 = extract(nlcd, p, buffer=1000, df=T) # raster extract
ex.1 # returning classes around points only, not all points in buffer
此代码仅返回该点的类,而我正在查找该点周围缓冲区中所有单元格的类/土地覆盖类型。
最终,我正在寻找多个点周围一定缓冲区内的土地覆盖比例。我也愿意使用 terra::extract 但还没有找到包含缓冲区的好方法。
非常感谢任何帮助!
您可以通过在提取之前使用
pbuff<-buffer(p, 1000)
进行简单缓冲,然后使用 pbuff
作为 extract()
中的第二个参数来解决此问题。请注意,由于“terra”包已替换“raster”包,因此我介绍的工作流程将使用 terra::extract()
而不是 raster::extract()
。这是完整的可重现解决方案:
library(dplyr)
library(rnaturalearth)
library(sf)
library(terra)
# base map
state = ne_states(iso_a2 = "US", returnclass = "sf") %>% # pull admin. bounds. for US
filter(iso_3166_2 == "US-MA") %>% # select Massachusetts
st_transform(crs = "+proj=aea +lat_1=29.5 +lat_2=45.5 +lat_0=23 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80
+towgs84=0,0,0,0,0,0,0 +units=m +no_defs") # nlcd crs
# points
Lon<-c(-69.97623, -69.97674)
Lat<-c(41.90238, 41.90267)
x<-cbind(Lon,Lat)
v<-vect(x, crs="+proj=longlat")
# nlcd data
library(FedData)
nlcd = get_nlcd( # fn for pulling NLCD data
template = state, # polygon template for download
label = "4pland", # this is just a character string label
year = 2011, # specify the year (2016 should be available soon)
force.redo = F
)
# plot
p<-project(v,crs(nlcd))
plot(nlcd)
plot(p, add=T)
# extract using terra::extract
pbuff<-buffer(p, 1000)
ex.1 <- extract(nlcd, pbuff) # raster extract with terra and the polygons from the buffered points
ex.1 # now returning classes around all points in buffer