Skip to contents

This function keeps the attributes. Note: results differ when use_rsgeo is TRUE: the {rsgeo} implementation is faster and more reliably keeps returned linestrings below a the segment_length value, but does not always return the number of segments requested.

Usage

line_segment(l, segment_length = NA, use_rsgeo = NULL, debug_mode = FALSE)

Arguments

l

A spatial lines object

segment_length

The approximate length of segments in the output (overides n_segments if set)

use_rsgeo

Should the rsgeo package be used? If rsgeo is available, this faster implementation is used by default. If rsgeo is not available, the lwgeom package is used.

debug_mode

Should debug messages be printed? Default is FALSE.

Examples

library(sf)
l <- routes_fast_sf[2:4, ]
l_seg_multi <- line_segment(l, segment_length = 1000, use_rsgeo = FALSE)
# Number of subsegments
table(l_seg_multi$ID)
#> 
#> 3 4 5 
#> 3 2 2 
plot(l_seg_multi, col = seq_along(l_seg_multi), lwd = 5)
#> Warning: plotting the first 9 out of 16 attributes; use max.plot = 16 to plot all
#> Warning: col is not of length 1 or nrow(x): colors will be recycled; use pal to specify a color palette
#> Warning: col is not of length 1 or nrow(x): colors will be recycled; use pal to specify a color palette
#> Warning: col is not of length 1 or nrow(x): colors will be recycled; use pal to specify a color palette
#> Warning: col is not of length 1 or nrow(x): colors will be recycled; use pal to specify a color palette
#> Warning: col is not of length 1 or nrow(x): colors will be recycled; use pal to specify a color palette
#> Warning: col is not of length 1 or nrow(x): colors will be recycled; use pal to specify a color palette
#> Warning: col is not of length 1 or nrow(x): colors will be recycled; use pal to specify a color palette
#> Warning: col is not of length 1 or nrow(x): colors will be recycled; use pal to specify a color palette
#> Warning: col is not of length 1 or nrow(x): colors will be recycled; use pal to specify a color palette

round(st_length(l_seg_multi))
#> Units: [m]
#> [1] 1013 1014  955 1294  998  986  881
# rsgeo implementation:
rsmulti = line_segment(l, segment_length = 1000, use_rsgeo = TRUE)
#> Warning: The CRS of the input object is latlon.
#> This may cause problems with the rsgeo implementation of line_segment().
# plot(rsmulti, col = seq_along(l_seg_multi), lwd = 5)
# round(st_length(rsmulti))
# waldo::compare(l_seg_multi, rsmulti)