将 POLYGON 聚合为 MULTIPOLYGON 并保留 data.frame
Aggregate POLYGONs to MULTIPOLYGONs and keep data.frame
我有一个 sf
多边形几何类型的对象。我想使用分组属性 (group_attr) 将这些多边形聚合成 MULTIPOLYGON,并加入一个具有属性 table 的新 MULTIPOLYGON 对象。因此,我将得到一个包含两行和三列的 sf
对象(group_attr、second_attr、几何)。我已经尝试使用 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))
非常老套(或幼稚)的方法:
attr_table <- df %>%
as_data_frame() %>%
select(-geometry) %>%
unique()
new_df <- df %>%
group_by(group_attr) %>%
summarise() %>%
as.data.frame(.) %>%
left_join(., attr_table, by='group_attr') %>%
st_as_sf()
new_df
Simple feature collection with 2 features and 2 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
epsg (SRID): NA
proj4string: NA
group_attr second_attr geometry
1 1 forest MULTIPOLYGON(((3 0, 3 1, 4 ...
2 2 lake MULTIPOLYGON(((4 0, 4 1, 5 ...
或:
> aggregate(df, list(df$second_attr), function(x) x[1])
Simple feature collection with 2 features and 3 fields
Attribute-geometry relationship: 0 constant, 2 aggregate, 1 identity
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
epsg (SRID): NA
proj4string: NA
Group.1 group_attr second_attr geometry
1 forest 1 forest MULTIPOLYGON(((3 0, 3 1, 4 ...
2 lake 2 lake MULTIPOLYGON(((4 0, 4 1, 5 ...
基于https://github.com/r-spatial/sf/issues/634#issuecomment-365151794的新方法:
library(dplyr)
df_new <- df %>%
group_by(group_attr) %>%
summarise_all(first)
df_new
Simple feature collection with 2 features and 2 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
epsg (SRID): NA
proj4string: NA
# A tibble: 2 x 3
group_attr second_attr geometry
<dbl> <fct> <MULTIPOLYGON>
1 1.00 forest (((3 0, 3 1, 4 1, 4 0, 3 0)), ((0 0, 1 4, 2 4, 3 2, 1 0, 0 0)))
2 2.00 lake (((4 0, 4 1, 5 1, 5 0, 4 0)), ((3 3, 4 2, 4 3, 3 3)))
我有一个 sf
多边形几何类型的对象。我想使用分组属性 (group_attr) 将这些多边形聚合成 MULTIPOLYGON,并加入一个具有属性 table 的新 MULTIPOLYGON 对象。因此,我将得到一个包含两行和三列的 sf
对象(group_attr、second_attr、几何)。我已经尝试使用 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))
非常老套(或幼稚)的方法:
attr_table <- df %>%
as_data_frame() %>%
select(-geometry) %>%
unique()
new_df <- df %>%
group_by(group_attr) %>%
summarise() %>%
as.data.frame(.) %>%
left_join(., attr_table, by='group_attr') %>%
st_as_sf()
new_df
Simple feature collection with 2 features and 2 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
epsg (SRID): NA
proj4string: NA
group_attr second_attr geometry
1 1 forest MULTIPOLYGON(((3 0, 3 1, 4 ...
2 2 lake MULTIPOLYGON(((4 0, 4 1, 5 ...
或:
> aggregate(df, list(df$second_attr), function(x) x[1])
Simple feature collection with 2 features and 3 fields
Attribute-geometry relationship: 0 constant, 2 aggregate, 1 identity
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
epsg (SRID): NA
proj4string: NA
Group.1 group_attr second_attr geometry
1 forest 1 forest MULTIPOLYGON(((3 0, 3 1, 4 ...
2 lake 2 lake MULTIPOLYGON(((4 0, 4 1, 5 ...
基于https://github.com/r-spatial/sf/issues/634#issuecomment-365151794的新方法:
library(dplyr)
df_new <- df %>%
group_by(group_attr) %>%
summarise_all(first)
df_new
Simple feature collection with 2 features and 2 fields
geometry type: MULTIPOLYGON
dimension: XY
bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
epsg (SRID): NA
proj4string: NA
# A tibble: 2 x 3
group_attr second_attr geometry
<dbl> <fct> <MULTIPOLYGON>
1 1.00 forest (((3 0, 3 1, 4 1, 4 0, 3 0)), ((0 0, 1 4, 2 4, 3 2, 1 0, 0 0)))
2 2.00 lake (((4 0, 4 1, 5 1, 5 0, 4 0)), ((3 3, 4 2, 4 3, 3 3)))