ropensci / stplanr

Sustainable transport planning with R
https://docs.ropensci.org/stplanr
Other
416 stars 66 forks source link

`line_segment` fails with argument `segment_length` #514

Closed jmsigner closed 11 months ago

jmsigner commented 1 year ago

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)
}

https://github.com/ropensci/stplanr/blob/bdbdd983e07300baa58e48816a6e758d28b495df/R/linefuns.R#L157

Robinlovelace commented 1 year ago

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

fulliautomatix-bike commented 1 year ago

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

Robinlovelace commented 1 year ago

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

Robinlovelace commented 11 months ago

@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")