【问题标题】:Merging the polygons inside a spatial polygons data frame based on a field in the @data slot基于@data槽中的字段合并空间多边形数据框内的多边形
【发布时间】:2018-08-21 22:23:24
【问题描述】:

我不是 R 专业人士,也不是空间分析专家。我正在寻找一种基于@data槽中的字段将多边形组合在空间多边形数据框中的方法:相当于dplyr的spdf的“group_by”。

我不确定合并、加入或组合是否是正确的词,但我希望很清楚我在寻找什么。

library(sp)

#coordinates:
xy1 = cbind(c(1,2,2,1),c(1,1,2,2))
xy2 = cbind(c(2,3,3,2),c(1,1,2,2))
xy3 = cbind(c(1,2,2,1),c(2,2,3,3))
xy4 = cbind(c(2,3,3,2),c(2,2,3,3))

#polygons:
p1 = Polygon(xy1)
ps1 = Polygons(list(p1),ID = "a")
p2 = Polygon(xy2)
ps2 = Polygons(list(p2),ID = "b")
p3 = Polygon(xy3)
ps3 = Polygons(list(p3),ID = "c")
p4 = Polygon(xy4)
ps4 = Polygons(list(p4),ID = "d")

#spatial polygons:
sps_m = SpatialPolygons(list(ps1,ps2,ps3,ps4))

#dataframe:
data_m = data.frame(dt = c("Group A","Group B","Group A","Group C"),row.names = c("a","b","c","d"))

#spatial polygons dataframe:
spdf_m = SpatialPolygonsDataFrame(sps_m,data_m)

#plot spdf:
plot(spdf_m)

【问题讨论】:

标签: r spatial sp


【解决方案1】:

描述连接、组合、联合或合并(从多个中生成一个)多边形的正确术语似乎是分解或聚合。

对我有用的函数是 raster 包中的 aggregate()。

library(sp)
library(raster)

#coordinates:
xy1 = cbind(c(1,2,2,1),c(1,1,2,2))
xy2 = cbind(c(2,3,3,2),c(1,1,2,2))
xy3 = cbind(c(1,2,2,1),c(2,2,3,3))
xy4 = cbind(c(2,3,3,2),c(2,2,3,3))

#polygons:
p1 = Polygon(xy1)
ps1 = Polygons(list(p1),ID = "a")
p2 = Polygon(xy2)
ps2 = Polygons(list(p2),ID = "b")
p3 = Polygon(xy3)
ps3 = Polygons(list(p3),ID = "c")
p4 = Polygon(xy4)
ps4 = Polygons(list(p4),ID = "d")

#spatial polygons:
sps_m = SpatialPolygons(list(ps1,ps2,ps3,ps4))

#dataframe:
data_m = data.frame(dt = c("Group A","Group B","Group A","Group C"),row.names = c("a","b","c","d"))

#spatial polygons dataframe:
spdf_m = SpatialPolygonsDataFrame(sps_m,data_m)

groups = aggregate(spdf_m, by = "dt")

#plot spdf:
plot(spdf_m)
plot(groups)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 2015-09-06
    • 2016-06-11
    • 2018-08-01
    • 2019-02-22
    • 2019-05-15
    • 2017-09-15
    相关资源
    最近更新 更多