gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 01:03:08 24 4
gpt4 key购买 nike

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

我不确定 merge、join 或 combine 是否是正确的词,但我希望我在寻找什么很清楚。

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)

最佳答案

描述连接、组合、统一或合并(从多个中取一个)多边形的行为的正确术语似乎是溶解或聚集。

对我有用的函数是 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)

关于r - 根据@data 插槽中的字段合并空间多边形数据框中的多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49259960/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com