根据@data槽中的字段合并空间多边形数据框内的多边形

时间:2018-03-13 15:27:19

标签: r spatial sp

我不是R的专业人士,也不是空间分析。我正在寻找一种方法,根据@data插槽中的字段组合空间多边形数据框内的多边形:相当于dplyr" group_by"对于spdf来说。

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

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)

1 个答案:

答案 0 :(得分:2)

用于描述加入,组合,联合或合并(使多个中的1个)多边形的行为的正确术语似乎是解散或聚合。

对我有用的功能是来自光栅包的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)