R:如何从一小段坐标中创建多行字符串sf

时间:2018-10-01 12:59:52

标签: r dplyr sf multilinestring

考虑以下技巧:

my_tible <- tibble("ID" = c("A","A","A","B","B", "C","C") , X=  c(647851,647875,647875,647766,647766,647826,647822) , 
               Y=c(6859335,6859318,6859319,6859028,6859030,6859314,6859316) )

我想基于该提示创建一个sf multilinestring对象:

my_tible %>% 
   group_by(ID) %>% 
   summarise("geometry" = c(X,Y) %>% 
          as.numeric() %>% 
          matrix(ncol = 2, byrow = F) %>% 
          list() %>% 
          st_multilinestring()) %>% 
   st_sf() %>% 
   st_set_crs("+init=epsg:2154") %>% 
   st_transform(crs="+proj=longlat +datum=WGS84")

我在st_sf()之后收到以下消息: “ st_sf(。)中的错误:不存在简单要素几何列。”

我想了解我在做错什么。

解决方案:

my_tible %>% 
  st_as_sf( coords = c("X", "Y")) %>% 
  group_by(ID) %>% 
  summarize() %>%
  st_cast("MULTILINESTRING") %>% 
  st_set_crs("+init=epsg:2154") %>% 
  st_transform(crs="+proj=longlat +datum=WGS84")

1 个答案:

答案 0 :(得分:1)

解决方案:

my_tible %>% 
  st_as_sf( coords = c("X", "Y")) %>% 
  group_by(ID) %>% 
  summarize() %>%
  st_cast("MULTILINESTRING") %>% 
  st_set_crs("+init=epsg:2154") %>% 
  st_transform(crs="+proj=longlat +datum=WGS84")