我有一个sf
POLYGON几何类型的对象。我想使用分组属性(group_attr)将这些多边形聚合为多个多边形,并使用属性表加入一个新的MULTIPOLYGON对象。所以,因此,我将有一个具有两行三列(group_attr,second_attr,geometry)的sf
对象。我已经尝试过使用st_cast
- 它适用于sfc
对象,但不适用于sf
对象。使用sf
包可以做到这一点吗?聚合多边形到多个多边形并保留数据。框架
p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
pol1 <-st_polygon(list(p1))
p2 <- rbind(c(3,0), c(4,0), c(4,1), c(3,1), c(3,0))
pol2 <-st_polygon(list(p2))
p3 <- rbind(c(4,0), c(4,1), c(5,1), c(5,0),c(4,0))
pol3 <-st_polygon(list(p3))
p4 <- rbind(c(3,3), c(4,2), c(4,3), c(3,3))
pol4 <-st_polygon(list(p4))
d <- data.frame(group_attr = c(1, 1, 2, 2),
second_attr = c('forest', 'forest', 'lake', 'lake'))
d$geometry <- st_sfc(pol1, pol2, pol3, pol4)
df<- st_as_sf(d)
plot(df)
df
Simple feature collection with 4 features and 2 fields
geometry type: POLYGON
dimension: XY
bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
epsg (SRID): NA
proj4string: NA
group_attr second_attr geometry
1 1 forest POLYGON((0 0, 1 0, 3 2, 2 4...
2 1 forest POLYGON((3 0, 4 0, 4 1, 3 1...
3 2 lake POLYGON((4 0, 4 1, 5 1, 5 0...
4 2 lake POLYGON((3 3, 4 2, 4 3, 3 3))
谢谢 - 这工作干净。我会注意到,我不得不在这个'emp < - st_is_empty(df);因为我有一些空的多边形,所以聚合(df [!emp,],by = list(df $ second_attr [!emp]),FUN = function(x)x [1])。在发了很多头发后,我发现我需要首先排除他们。 – thelatemail
如果您认为这是一个问题,请在sf github页面上提出问题。 –