Skip to content

line_segment fails when segment_length is specified  #492

@Aeilert

Description

@Aeilert

Hi,

Thanks for a very cool package!

I think there might be a bug in your line_segment() function when using the segment_length argument.

E.g.

> l <- routes_fast_sf[2, ]
> l_split <- line_segment(l = l, segment_length = 500)
Error in Ops.units(n_segments, 1) : 
  both operands of the expression should be "units" objects

The cause appears to be that round(l_length/segment_length) returns an object in units , which causes n_segments + 1 to fail.

stplanr/R/linefuns.R

Lines 157 to 164 in 8a9c7b5

line_segment <- function(l, n_segments, segment_length = NA) {
if (!is.na(segment_length)) {
l_length <- sf::st_length(l)
n_segments <- round(l_length / segment_length)
}
# browser() # tests
# first_linestring = lwgeom::st_linesubstring(x = l, from = 0, to = 0.2)
from_to_sequence = seq(from = 0, to = 1, length.out = n_segments + 1)

Coercing n_segments to a numeric should fix this.

  if (!is.na(segment_length)) {
    l_length <- sf::st_length(l)
    n_segments <- as.numeric(round(l_length / segment_length))
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions