我有一个POLYGON几何类型的`public interfce IHandleMessage<T> where T:BaseMessage
{
void Handle(T msg); // do some stuff
}
public class HandleMessageOne:IHandleMessage<MessageOne>{
void Handle(MessageOne msg){//do something}
}
public class HandleMessageTwo:IHandleMessage<MessageTwo>{
void Handle(MessageTwo msg){//do something}
}
public class HandleMessageThree:IHandleMessage<MessageThree>{
void Handle(MessageThree msg){//do something}
}`
对象。我想使用分组属性(group_attr)将这些多边形聚合到MULTIPOLYGON中,并使用属性表连接新的MULTIPOLYGON对象。因此,我将得到一个sf
对象,其中包含两行和三列(group_attr,second_attr,geometry)。我已经尝试使用sf
- 它适用于st_cast
个对象,但不适用于sfc
个对象。是否可以使用sf
包执行此操作?
sf
答案 0 :(得分:2)
或:
> 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 ...
答案 1 :(得分:2)
基于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)))
答案 2 :(得分:0)
非常hacky(或天真)的方法:
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 ...