从多边形列表创建空间多边形数据框 [英] Creating spatialpolygons dataframe from list of polygons

查看:160
本文介绍了从多边形列表创建空间多边形数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试根据多边形列表(用于生物多样性研究的研究区域)创建多边形shapefile.

I am currently trying to create a polygon shapefile from a list of polygons (study areas for biodiversity research).

当前,这些多边形以以下格式存储在列表中:

Currently these polygons are stored in a list in this format:

$SEW22
     [,1]    [,2]
[1,] 427260.4 5879458
[2,] 427161.4 5879472
[3,] 427175.0 5879571
[4,] 427273.9 5879557
[5,] 427260.4 5879458

$SEW23
     [,1]    [,2]
 [1,] 418011.0 5867216
 [2,] 417912.0 5867230
 [3,] 417925.5 5867329
 [4,] 418024.5 5867315
 [5,] 418011.0 5867216

我尝试使用 writeOGR 将它们简单地写为shpfile,但是发生以下错误:

I tried to simply write them as shpfile with writeOGR but the following error occurs:

> #write polygons to shp
> filenameshp <- paste('Forestplots')
> layername <- paste('Forestplots')
> writeOGR(obj=forest, dsn = filenameshp, 
+          layer=layername, driver="ESRI Shapefile", overwrite_layer =     TRUE)
Error in writeOGR(obj = forest, dsn = filenameshp, layer = layername,  : 
 inherits(obj, "Spatial") is not TRUE

我阅读了Barry Rowlingson撰写的教程,以创建空间多边形和认为我应该先创建一个数据框并执行以下操作:

I read this tutorial by Barry Rowlingson to create spatialpolygons and thought I should probably first create a dataframe and did this:

forestm<-do.call(rbind,forest)

但是这没有返回您所能想象的有用的东西,而且它丢失了绘图的名称.

but this returned nothing useful as you can imagine, plus it lost the names of the plots.

由于我还是R语言的新手,所以我也尝试了许多其他方法,这些方法我无法完全判断,但没有一种方法能返回我希望的结果,因此我不愿使用这些随机方法....

As I am still new to R I also tried lots of different other approaches which sensefulness I could not fully judge but none returned what I hoped for and so I spare you with these random approaches.....

我期待着你的主张.

非常感谢

P.S.我还按照spacepolygons {sp} 程序包:

P.S. I also tried the following as described in the spatialpolygons{sp} package:

> Polygons(forest, ID)
Error in Polygons(forest, ID) : srl not a list of Polygon objects

推荐答案

您可以按照此答案中所述的方法进行操作:

You can follow the approach described in this answer: https://gis.stackexchange.com/questions/18311/instantiating-spatial-polygon-without-using-a-shapefile-in-r.

以下是将这种方法应用于您的案件的方法.首先,在您的示例数据中创建一个矩阵列表:

Here's how to apply the approach to your case. First, I create a list of matrices as in your sample data:

forest <- list(
  "SEW22" = matrix(c(427260.4, 5879458, 427161.4, 5879472, 427175.0, 5879571, 427273.9, 5879557, 427260.4, 5879458),
                   nc = 2, byrow = TRUE),
  "SEW23" = matrix(c(418011.0, 5867216, 417912.0, 5867230, 417925.5, 5867329, 418024.5, 5867315, 418011.0, 5867216),
                   nc = 2, byrow = TRUE)
  )

现在

library(sp)
p <- lapply(forest, Polygon)
ps <- lapply(seq_along(p), function(i) Polygons(list(p[[i]]), ID = names(p)[i]))
sps <- SpatialPolygons(ps)
sps_df <- SpatialPolygonsDataFrame(sps, data.frame(x = rep(NA, length(p)), row.names = names(p)))

第一步,我们遍历矩阵列表,并对每个矩阵应用Polygon函数,以创建Polygon对象的列表.在第二步中,我们遍历此列表以创建Polygons对象,并将此对象中每个元素的ID设置为原始列表中的对应名称(例如"SEW22","SEW23").第三步创建一个SpatialPolygons对象.最后,我们创建一个SpatialPolygonsDataFrame对象.在这里,我有一个用NA填充的虚拟数据框(请注意,行名称必须与多边形ID对应).

In the first step, we iterate through the list of matrices and apply the Polygon function to each matrix to create a list of Polygon objects. In the second step, we iterate through this list to create a Polygons object, setting the ID of each element in this object to the corresponding name in the original list (e.g. "SEW22", "SEW23"). The third step creates a SpatialPolygons object. Finally, we create a SpatialPolygonsDataFrame object. Here I have a dummy dataframe populated with NAs (note that the row names must correspond to the polygon IDs).

最后,写入数据

rgdal::writeOGR(obj = sps_df,
                dsn = "Forestplots",
                layer = "Forestplots",
                driver = "ESRI Shapefile",
                overwrite_layer = TRUE)

这将在您的工作目录中创建一个新文件夹:

This creates a new folder in your working directory:

list.files()
# [1] "Forestplots"
list.files("Forestplots")
# [1] "Forestplots.dbf" "Forestplots.shp" "Forestplots.shx"

请咨询链接的答案以获取更多详细信息.

Consult the linked answer for more details.

这篇关于从多边形列表创建空间多边形数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆