栅格化 shps inR 列表

Rasterizing a list of shps inR

我一直在尝试栅格化 R 中导入的 shp 列表。调用该列表时出现错误,因为它不是数据框而是字符。我尝试使用 as.dataframe(list[i]) 但没有成功。

错误:无法为签名“"character"、"RasterLayer"”

的函数“rasterize”找到继承方法
## import Shp
shp_ELC <- readOGR("EconomicLandConsesions/ELC.shp")
shp_CF <- readOGR("Communityforestry/Community_forestryPolygon.shp")
shp_NPA <- readOGR("Natural-Protected-Areas/Natural Protected Areas.shp")
shp_Ecoregion <- readOGR("TerrestrialEcoRegionsWWF/KH_TerrEcoregions_Dslv.shp")

## create Raster template
utm     <- proj4string(shp_CF)
my_proj <- spTransform(shp_Ecoregion,utm)
my_ext  <- extent(my_proj)
my_res  <- 30
tempR    <- raster(resolution = my_res, ext = my_ext, crs = my_proj)
## list SHP
listShp <- ls(pattern = glob2rx("*.df"))

## looping list
for(i in 1:length(listShp)){
  rst_CF  <- rasterize(data.frame(listShp[i]),tempR)
}

这里有一些笔记

readOGR 不适用于您提供的参数。 shapefile 函数会。我更改了变量名,因为在将数据读入 R 后,它们不再是 shapefile。大概是 SpatialPolygonsDataFrame 个对象

library(raster)
p_ELC <- shapefile("EconomicLandConsesions/ELC.shp")
p_CF <- shapefile("Communityforestry/Community_forestryPolygon.shp")
p_NPA <- shapefile("Natural-Protected-Areas/Natural Protected Areas.shp")
p_Ecoregion <- shapefile("TerrestrialEcoRegionsWWF/KH_TerrEcoregions_Dslv.shp")

# you can create a list right away, but with only a few objects that might make things harder for you
# f <- c("EconomicLandConsesions/ELC.shp", "Communityforestry/Community_forestryPolygon.shp", "Natural-Protected-Areas/Natural Protected Areas.shp", "TerrestrialEcoRegionsWWF/KH_TerrEcoregions_Dslv.shp")
# pList <- lapply(f, shapefile)

显然,一些多边形具有 UTM CRS,而另一些则没有。 确保它们都转换为相同的 CRS。

utmcrs  <- crs(p_CF)
p_Ecoregion <- spTransform(p_Ecoregion, utmcrs)

# create Raster template
r <- raster(resolution=30, ext = extent(p_Ecoregion), crs = utmcrs)

# make a list of the SpatialPolygonDataFrame objects
listP <- list(p_ELC, p_CF, p_NPA, p_Ecoregion)

我不知道现在该做什么。在您的示例中,您将输出覆盖了三次。我假设你想要四个光栅

x <- list()
for(i in 1:length(listP)){
  x[[i]] <- rasterize(listP[[i]]), r)
}

s <- stack(x)
plot(s)