I am trying to return the vertices of a linestring using the sf
package. So far I have:
ls <- st_sfc(st_linestring(rbind(c(0,0),c(0,1))),
st_linestring(rbind(c(0,0),c(10,0))))
get_line_endings <- function(line){
res <- lapply(line, function(x){
pnts <- st_coordinates(x)
pnts <- pnts[c(1, nrow(pnts)),]
data.frame(pnts)
})
res <- do.call("rbind", res)
res <- res[!duplicated(res),]
st_as_sf(res, coords = c("X", "Y"), crs = st_crs(line))
}
pnts <- get_line_endings(ls)
plot(ls)
plot(pnts, add = TRUE)
Is this error prone? Can I rely on st_coordinates
being "in order"? Is there a more efficient way of doing this?
ls
is a vector of two line strings. Within a single line string you can be sure thatst_coordinates
is in order. What is it you are trying to do?