ropensci / stplanr

Sustainable transport planning with R

Home Page:https://docs.ropensci.org/stplanr

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`line_segment` fails with argument `segment_length`

jmsigner opened this issue · comments

When using the function line_segment() with the argument segment_length I get an error:

Error in Ops.units(n_segments, 1) : 
  both operands of the expression should be "units" objects

This is solved when changing

    n_segments <- round(l_length / segment_length)

to

    n_segments <- units::drop_units(round(l_length / segment_length))

See full example below:

l <- routes_fast_sf[2, ]
l_seg2 <- stplanr::line_segment(l = l, segment_length = 500)
l_seg2 <- line_segment1(l = l, segment_length = 500)



line_segment1 <- function(l, n_segments, segment_length = NA) {
  if (!is.na(segment_length)) {
    l_length <- sf::st_length(l)
    n_segments <- units::drop_units(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)
  line_segment_list = lapply(seq(n_segments), function(i)
    lwgeom::st_linesubstring(
      x = l,
      from = from_to_sequence[i],
      to = from_to_sequence[i + 1]
    )
  )
  do.call(rbind, line_segment_list)
}

line_segment <- function(l, n_segments, segment_length = NA) {

Thanks for raising the issue, and reproducible example. Will take a look and get back to you.

I had the same issue and could solve it with a similar change, fwiw.

Thanks again. Can check at some point soon...

@fulliautomatix-bike and @jmsigner finally got round to fixing it, please test with

remotes::install_github("ropensci/stplanr@515-rnet_split_lines-fails-removes-segments")
  • checkout #516 if you get a chance 🙏