I have a dataset featuring many linestrings which I would like to view all on one plot via R’s “sf” package.
Each linestring comes in string form from the CSV, so we must extract the numbers before we can plot them. I have figured out how to plot the linestrings one by one, as I shape them into a matrix right after I parse them, but again I would like to view them all at once. But I can’t figure out how to merge the vectors/matrices representing individual linestrings together without smooshing them all together in such a way that they cannot be separated again.
parse_points <- function(point_vec){
linestrings <- list()
for (point in point_vec) {
all_split <-str_split(str_replace_all(point, " -122.", "-122."), " ")
numeric_coord = c(numeric())
for (coord in all_split) {
numeric_coord <- c(numeric_coord, as.numeric(coord))
}
m <- matrix(numeric_coord, ncol=2, byrow=TRUE)
ls <- st_linestring(matrix(numeric_coord, ncol=2, byrow=TRUE))
# plot(ls) [this correctly plots the individual linestring]
linestrings <- c(linestrings, ls)
}
show(linestrings) # this is not correctly shaped
return(linestrings)
}
parse_linestring <- function(linestring){ # called for each CSV row, E
var1 <- str_replace_all(linestring, "LINESTRING \(", "")
var2 <- str_replace_all(var1, "\)", "")
var3 <- str_split(var2, ",") # there are X coordinates in the linestring for that row E, where X varies depending on the row E
return(parse_points(var3))
}
parsed <- parse_linestring(filtered$the_geom)