我正在寻找一种从 SpatialPolygons 列表创建 SpatialPolygonsDataFrame 的方法?
下面是一个多边形列表示例,应从中创建包含列表中所有多边形的 SpatialPolygonsDataFrame。
编辑:必须从 SpatialPolygons 列表创建 SpatialPolygonsDataFrame!因为我的原始数据不包含 SpatialPolygons 作为单独的值,而是包含 SpatialPolygons 列表。我收到此列表的方式与示例中的不同。我发布了示例以显示列表的数据结构。
取自https://stat.ethz.ch/pipermail/r-sig-geo/2013-January/017225.html的 SpatialPolygons 列表示例:
library(sp)
grd <- GridTopology(c(0.5, 0.5), c(1, 1), c(6, 6))
Spol <- as(grd, "SpatialPolygons")
list_of_SPolsu <- lapply(slot(Spol, "polygons"), function(x)
SpatialPolygons(list(x)))
list_of_SPols <- lapply(slot(Spol, "polygons"), function(x) {
Pol <- x
slot(Pol, "ID") <- "1"
SpatialPolygons(list(Pol))
})
Run Code Online (Sandbox Code Playgroud)
问候!
这很简单...
尝试这个:
#Creating a dataframe with Spol IDs
Spol_df<- as.data.frame(sapply(slot(Spol, "polygons"), function(x) slot(x, "ID")))
#Making the IDs row names
row.names(Spol_df) <- sapply(slot(Spol, "polygons"), function(x) slot(x, "ID"))
# Making the spatial polygon data frame
Spol_SPDF <- SpatialPolygonsDataFrame(Spol, data =Spol_df)
Run Code Online (Sandbox Code Playgroud)
让我知道它是否有效
只是为了根据问题中的编辑完成答案......有关如何从多边形列表制作 SpatialPolygon 的解决方案来自提供的链接:
#Getting polygon IDs
IDs <- sapply(list_of_SPols, function(x)
slot(slot(x, "polygons")[[1]], "ID"))
#Checking
length(unique(IDs)) == length(list_of_SPols)
#Making SpatialPolygons from list of polygons
Spol <- SpatialPolygons(lapply(list_of_SPols,
function(x) slot(x, "polygons")[[1]]))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4562 次 |
| 最近记录: |