I have a shapefile of river segments that I've imported into R. The segments range in length from 5-115km. I want to split the segments that are over 10km into 10km chunks. So I use
st_segmentize. But this doesn't actually split a long line into multiple new shorter lines, which is what I want... Is there a way to do this? I think
st_segmentize just adds vertices every 10km. So I just need to split the lines at the vertices.
Code so far:
library("sf") rivers = st_read("./YukonMain4_copy.shp", layer = 'YukonMain4_copy') seg = st_segmentize(rivers, units::set_units(10,km))
Re. my above comment
The issue is, my resulting lines have lengths mostly from 200-500m, and then there are random onesthat are 700000m long. Plus, this data set is of multiple rivers, so now I have lines connecting places that should not be connected. Any ideas?
Here is my code:
- Import file, segmentize the data, and cast it into the point cloud
- Create an index to combine segments by (hopefully so that they are approx 10km in length
Combine things back into lines.
rivers=st_read("./YukonMain4_copy.shp", layer='YukonMain4_copy') seg = st_segmentize(rivers, units::set_units(10000,m)) seg2= st_cast(seg, 'POINT') length = length(seg2$OBJECTID)/2 index = rep(1:length, each=2) rivers.index=cbind(seg2, index) to_line <- function(points) st_cast(st_combine(points), "LINESTRING") %>% .[] rivers.nest = rivers.index %>% group_by(index) %>% nest segments = rivers.nest %>% pull(data) %>% map(to_line)%>%st_sfc(crs = 4326) lines = rivers.nest %>% select(data)%>% st_sf(geometry=segments)