Skip to content

Instantly share code, notes, and snippets.

@jhollist
Created July 20, 2018 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhollist/d4a3997b961eb5699c8ffdcc0e80d5d2 to your computer and use it in GitHub Desktop.
Save jhollist/d4a3997b961eb5699c8ffdcc0e80d5d2 to your computer and use it in GitHub Desktop.
Combine multiple, sequential linestrings into one
library(sf)
#What I have
l1 <- st_linestring(matrix(c(1,1,2,2,3,1),ncol = 2, byrow = T))
l2 <- st_linestridng(matrix(c(3,1,3,2,4,2),ncol = 2, byrow = T))
l3 <- st_linestring(matrix(c(4,2,5,1,6,1),ncol = 2, byrow = T))
all_lines <- st_sfc(l1,l2,l3)
all_lines
#What I want
goal_line <- st_linestring(matrix(c(1,1,2,2,3,1,3,2,4,2,5,1,6,1),
ncol = 2, byrow = T))
goal_line <- st_sfc(goal_line)
goal_line
#What I did
goal_coords <- unique(st_coordinates(all_lines)[,-3])
goal_line_solution <- st_linestring(goal_coords)
goal_line_solution <- st_sfc(goal_line_solution)
goal_line_solution
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment