JeremyGelb / spNetwork

An R package to perform spatial analysis on networks.
GNU General Public License v2.0
34 stars 2 forks source link

encountering "Error in trim_lines_at(df1, graph_result, d, dd, i, donught) : object 'no_cut' not found" #19

Closed jdjohn215 closed 2 months ago

jdjohn215 commented 2 months ago

Hi, thanks for this remarkable package.

I'm occasionally encountering this error, "Error in trim_lines_at(df1, graph_result, d, dd, i, donught) : object 'no_cut' not found."

I think it's occurring on line 328 of isochrones_sf.R.

  if(sum(!test) > 0){ # line 285
    no_cut <- subset(df1, test)
    to_cut <- subset(df1, !test)
# lines 288-326 omitted
  }else{
    ok_lines <- no_cut[c("end_oid","start_oid","edge_id","weight" )] # this is line 328
  }

If I'm reading that code right, the no_cut object isn't defined when sum(!test) > 0 isn't TRUE. I think the object no_cut was previously created in line 208 (inside the all_multi_lignes() function). But that line is now commented out.

JeremyGelb commented 2 months ago

Hi ! Thank you for your interest in spNetwork ! I would like to see where the error is occurring. Could you share with me a reproducible minimal example (with sample of data and code) ?

I am currently working a new version, I could check this bug in the same time.

jdjohn215 commented 2 months ago

I figured out that the error occurs when distance includes a range with no data. This is demonstrated below.

It would be ideal to let the distance argument be set dynamically by a range.

Apologies, my line network is too large for dput, so my code downloads it straight from dropbox.

library(tmap)
library(tidyverse)
library(sf)
library(spNetwork)

# line network
line.network <- read_rds("https://www.dropbox.com/scl/fi/amacxec0dynqg0rvqte31/sample-lines.rds?rlkey=b0hsn5wk9wdl4jiq56ppx3aj4&dl=1")

# the center of the isochrone
target.point <- st_as_sf(tibble(x = 2547659.84632164, y = 378331.192528054), coords = c("x","y"), crs = 32054)

# visualize
tm_shape(line.network) + tm_lines() + tm_shape(target.point) + tm_bubbles(col = "red")

image

Here's an example that works because the largest range, 9240-10560, has some values.


# this works
iso_results <- calc_isochrones(lines = line.network,
                               start_points = target.point,
                               donught = T,
                               # quarter mile segments
                               dists = seq(1320,10560,1320),
                               weight = "length"
)
table(iso_results$distance)

 1320  2640  3960  5280  6600  7920  9240 10560 
  301  1026  1408  1598  1467   965   111    17 

# visualize
iso_results |>
  mutate(distance = as.factor(distance)) |>
  tm_shape() +
  tm_lines(col = "distance", palette = "viridis") +
  tm_shape(target.point) +
  tm_bubbles(col = "red")

image

This causes an error because the largest range, 10560-11880, has no values.

# this breaks
iso_results <- calc_isochrones(lines = line.network,
                               start_points = target.point,
                               donught = T,
                               # quarter mile segments
                               dists = seq(1320,11880,1320),
                               weight = "length"
)
Error in trim_lines_at(df1, graph_result, d, dd, i, donught) : 
  object 'no_cut' not found

My use-case here is creating isochrones for each of Milwaukee's 180 polling places. The area covered by each polling place varies in size, so the ranges supplied to the dists argument has to vary. It would be great to simply set the dists increment (in my example, quarter miles), and then let the function determine the number of break points.

JeremyGelb commented 2 months ago

I found the cause of the bug. It is now corrected in the branch called "newbranch" if you want to try it. I am still working on the next big release. I also added the possibility to define the parameter dists as a list of distances vectors. This was, you can specify a vector of distance for each start_points.

JeremyGelb commented 2 months ago

The correction has been included in the new version released on github (branch master).