【问题标题】:Convert SpatialPointsDataframe to SpatialPolygons in R在 R 中将 SpatialPointsDataframe 转换为 SpatialPolygons
【发布时间】:2014-03-12 14:53:11
【问题描述】:

我在 R 中有一个 SpatialPointsDataFrame,如下所示:

              coordinates id order  hole piece group box_id
    326  (-94.4, 27.6586) 47     1 FALSE     1  47.1      1
    327 (-93.64, 27.6232) 47     2 FALSE     1  47.1      1
    328 (-92.04, 27.7649) 47     3 FALSE     1  47.1      1
    329 (-90.36, 27.0903) 47     4 FALSE     1  47.1      1
    330 (-91.12, 25.6929) 47     5 FALSE     1  47.1      1
    331 (-92.92, 25.6569) 47     6 FALSE     1  47.1      1
    332 (-93.44, 26.0169) 47     7 FALSE     1  47.1      1
    333  (-94.4, 25.9809) 47     8 FALSE     1  47.1      1
    334  (-94.4, 27.6586) 47     9 FALSE     1  47.1      1
    335 (-92.04, 27.7649) 48     1 FALSE     1  48.1      2
    336 (-93.64, 27.6232) 48     2 FALSE     1  48.1      2
    337  (-94.4, 27.6586) 48     3 FALSE     1  48.1      2
    338  (-94.4, 27.8356) 48     4 FALSE     1  48.1      2
    339 (-93.64, 27.7649) 48     5 FALSE     1  48.1      2
    340 (-90.28, 28.1182) 48     6 FALSE     1  48.1      2
    341 (-90.56, 27.9417) 48     7 FALSE     1  48.1      2
    342 (-92.04, 27.7649) 48     8 FALSE     1  48.1      2
    100  (-94.4, 27.8356) 20     1 FALSE     1  20.1      3
    101  (-94.4, 28.0829) 20     2 FALSE     1  20.1      3
    102 (-90.28, 28.1182) 20     3 FALSE     1  20.1      3
    103 (-93.64, 27.7649) 20     4 FALSE     1  20.1      3
    104  (-94.4, 27.8356) 20     5 FALSE     1  20.1      3

(行名/编号不按顺序排列,因为我按 box_id 列排序)

这些点是多边形的节点(由 box_id 标识)。我想将其写为多边形形状文件以读入 GIS 程序,但我无法将其转换为 SpatialPolygonDataFrame(然后使用 writeOGR)或直接将其写入 shp 文件。任何帮助将不胜感激。我是 R 的 GIS 新手,如果我遗漏了一些明显的东西,请道歉。

【问题讨论】:

    标签: r gis spatial polygons


    【解决方案1】:

    所以这是另一个解决方案,在概念上类似于 @JoshO'Brien 的。这个容纳子多边形(给定 id 的多个组)并创建一个 SpatialPolygonsDataFrame 类的对象,其中包含数据部分(例如,属性表)。这完全可以导出到 ESRI shapefile。

    首先,作为示例,我们创建一个与您的格式相同的 SpatialPointsDataFrame。 id 列标识多边形IDgroup 列标识给定多边形的子多边形。在这个例子中,我们有 3 个多边形,其中两个有 1 个子多边形,第三个有 3 个子多边形。我们还创建了一个数据框data(属性表),在这种情况下将多边形 ID 与 box_id 相关联。

    # this code just creates a sample SpatialPointsDataFrame 
    x <- c(-10,-10,10,10,-10)
    y <- c(-10,10,10,-10,-10)
    df.1 <- data.frame(x,y,id=47, order=1:5,hole=F,piece=1,group=47.1,box_id=1)
    coordinates(df.1)=c("x","y")
    x <- c(+15+3*cos(2*pi/5*(0:5)))
    y <- c(-15+3*sin(2*pi/5*(0:5)))
    df.2 <- data.frame(x,y,id=48, order=1:6,hole=F,piece=1,group=48.1,box_id=2)
    coordinates(df.2)=c("x","y")
    x <- c(-15+2*cos(2*pi/8*(0:8)))
    y <- c(+15+2*sin(2*pi/8*(0:8)))
    df.3.1 <- data.frame(x,y,id=20, order=1:9,hole=F,piece=1,group=20.1,box_id=3)
    coordinates(df.3.1)=c("x","y")
    x <- c(0+2*cos(2*pi/8*(0:8)))
    y <- c(+15+2*sin(2*pi/8*(0:8)))
    df.3.2 <- data.frame(x,y,id=20, order=1:9,hole=F,piece=1,group=20.2,box_id=3)
    coordinates(df.3.2)=c("x","y")
    x <- c(+15+2*cos(2*pi/8*(0:8)))
    y <- c(+15+2*sin(2*pi/8*(0:8)))
    df.3.3 <- data.frame(x,y,id=20, order=1:9,hole=F,piece=1,group=20.3,box_id=3)
    coordinates(df.3.3)=c("x","y")
    df <- rbind(df.1,df.2,df.3.1,df.3.2,df.3.3)
    df
    #              coordinates id order  hole piece group box_id
    # 1             (-10, -10) 47     1 FALSE     1  47.1      1
    # 2              (-10, 10) 47     2 FALSE     1  47.1      1
    # 3               (10, 10) 47     3 FALSE     1  47.1      1
    # 4              (10, -10) 47     4 FALSE     1  47.1      1
    # 5             (-10, -10) 47     5 FALSE     1  47.1      1
    # 6              (18, -15) 48     1 FALSE     1  48.1      2
    # 7  (15.92705, -12.14683) 48     2 FALSE     1  48.1      2
    # 8  (12.57295, -13.23664) 48     3 FALSE     1  48.1      2
    # ...
    data <- data.frame(box_id=unique(df$box_id),row.names=unique(df$id))
    

    现在,这是points2polygons(...) 的代码。使用您现有的 SpatialPointsDataFrame,这就是您所需要的。

    points2polygons <- function(df,data) {
      get.grpPoly <- function(group,ID,df) {
             Polygon(coordinates(df[df$id==ID & df$group==group,]))
             }
      get.spPoly  <- function(ID,df) {
             Polygons(lapply(unique(df[df$id==ID,]$group),get.grpPoly,ID,df),ID)
             }
      spPolygons  <- SpatialPolygons(lapply(unique(df$id),get.spPoly,df))
      SpatialPolygonsDataFrame(spPolygons,match.ID=T,data=data)
    }
    spDF <- points2polygons(df,data)
    plot(spDF,col=spDF$box_id+1)
    
    library(rgdal)
    writeOGR(spDF,dsn=".",layer="myShapefile", driver="ESRI Shapefile")
    

    【讨论】:

    • 感谢您的反馈。
    • 我选择这个是为了包含使用 writeOGR 进行写作的完整过程。我认为第 2 行末尾的“(”需要是一个“{”,否则它运行完美
    【解决方案2】:

    检查这个:

    来自:SpatialPointsDataFrame --> SpatialPolygons --> Spatial PolygonsDataFrame

    https://stackoverflow.com/a/66565985/14361772

    【讨论】:

      猜你喜欢
      • 2014-08-08
      • 1970-01-01
      • 2021-01-30
      • 2014-08-19
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      • 1970-01-01
      • 2013-12-17
      相关资源
      最近更新 更多