Felipe
Felipe

Reputation: 729

R : How to create multilinestring sf from a tibble of coordinates

Consider the following tibble :

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) )

I would like to create a sf multilinestring object based on that tibble :

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")

I got the following message after st_sf() : "Error in st_sf(.) : no simple features geometry column present."

I would like to understand what I am doing wrong.

Solution :

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")

Upvotes: 2

Views: 1185

Answers (1)

Felipe
Felipe

Reputation: 729

a solution :

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")

Upvotes: 1

Related Questions