Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

line_segment fails with argument segment_length #514

Closed
jmsigner opened this issue Jan 31, 2023 · 4 comments
Closed

line_segment fails with argument segment_length #514

jmsigner opened this issue Jan 31, 2023 · 4 comments

Comments

@jmsigner
Copy link

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

@Robinlovelace
Copy link
Member

Robinlovelace commented Jan 31, 2023

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

@fulliautomatix-bike
Copy link

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

@Robinlovelace
Copy link
Member

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

Robinlovelace added a commit that referenced this issue Aug 15, 2023
Robinlovelace added a commit that referenced this issue Aug 15, 2023
@Robinlovelace
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants