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

[QUESTION] Routing algorithm for non transit mode (ex : car, bike) #317

Closed
ichsan2895 opened this issue Feb 8, 2023 · 9 comments
Closed

Comments

@ichsan2895
Copy link

ichsan2895 commented Feb 8, 2023

  1. Why detailed_itineraries() give different route in two ways trip? Its also has different trip duration with travel_time_matrix() too.
library(r5r)
library(mapview)
library(sf)
library(tidygeocoder)
library(dplyr)
library(tidyverse)

options(java.parameters = "-Xmx2G")

library(r5r)
path <- "r5rv1-highway-riau"
r5r_core <- setup_r5(data_path = path, verbose = FALSE)

some_addresses <- tibble::tribble(
~name,                  ~addr,
"Rokan Hulu",      "Pasir Pengaraian, Kecamatan Rambah, Rokan Hulu Regency, Riau, Indonesia",
"Rokan Hilir",      "Bagan siapi-api",
)

points <- some_addresses %>%
  tidygeocoder::geocode(addr, method = 'osm', lat = "lat" , long = "lon")

points <- points %>% mutate(id = row_number())
points_XY <- points
points <- st_as_sf(points, coords=c("lon","lat")) %>% 
  sf::st_set_crs("EPSG:4326")
points <- inner_join(x=points, y=points_XY[c("lat","lon", "id")], by="id", all.x=TRUE)

# set inputs
mode <- "CAR"
max_walk_time <- 5 # in minutes
max_trip_duration <- 9999 # in minutes
departure_datetime <- as.POSIXct("13-05-2019 14:00:00",
                                 format = "%d-%m-%Y %H:%M:%S")

# TTM has same trip duration from "Rokan Hulu" to "Rokan Hilir" regency (vice versa)
ttm <- expanded_travel_time_matrix(r5r_core = r5r_core,
                          origins = points,
                          destinations = points,
                          mode = mode,
                          departure_datetime = departure_datetime,
                          max_trip_duration = max_trip_duration,
                          verbose = FALSE)

ttm

image

# First, I will route it from Rokan Hulu to Rokan Hilir regency
# But detailed itineraries has different result and different trip duration with travel_time_matrix()
DIT <- detailed_itineraries(r5r_core = r5r_core,
                            origins = points[1,],
                            destinations = points[2,],
                            mode = mode,
                            max_walk_time = max_walk_time,
                            departure_datetime = departure_datetime,
                             shortest_path = FALSE,
                            max_trip_duration = 9999,
                           verbose = FALSE)

DIT$fromPlace <- as.character(st_drop_geometry(points[1,"name"]))
DIT$toPlace <- as.character(st_drop_geometry(points[2,"name"]))

# Second, I will reverse that O-D
DIT_2nd <- detailed_itineraries(r5r_core = r5r_core,
                            origins = points[2,],
                            destinations = points[1,],
                            mode = mode,
                            max_walk_time = max_walk_time,
                            departure_datetime = departure_datetime,
                             shortest_path = FALSE,
                            max_trip_duration = 9999,
                           verbose = FALSE)

DIT_2nd$fromPlace <- as.character(st_drop_geometry(points[2,"name"]))
DIT_2nd$toPlace <- as.character(st_drop_geometry(points[1,"name"]))
DIT <- rbind(DIT, DIT_2nd)
DIT

image

st_write(DIT, "r5rv1-result.geojson", crs="WGS84", driver="Geojson", delete_dsn = TRUE)
mapview(DIT, zcol = 'from_id')

image

  1. Why detailed_itineraries() is not choose the road that has higher highways tags such as "trunk" or "primary"?
    In this case,
    yellow = "Rokan Hilir" to "Rokan Hulu"
    purple = "Rokan Hulu" to "Rokan Hilir"
    green = "Rokan Hulu" to Rokan Hilir" with Graphhopper (mostly choose the ways in the primary and secondary road)

image

  1. How much the default speed for each road type (trunk, primary, secondary, etc)?

  2. Is there are any factor that affecting vehicle speed such as road smoothness, road density, and slope? It would be nice if it can be customize too. Unfortunately, in my country, the primary road still has broken road in some segment (of course that affect the vehicle speed too). Some inter cities road are unpaved too.

I appreciate this idea #290 and #289

network_settings.json

{"r5_version":"6.7", "r5_network_version":"nv2", "r5r_version":"1.0.0", "creation_date":"2023-02-05T21:08:02.213288", "pbf_file_name":"r5rv1-highway-riau/Highway_Riau_20230123.osm.pbf", "gtfs_1":"r5rv1-highway-riau/angkots-v4-gtfs.zip", "use_elevation":"false", "elevation_cost_function":"NONE", "tiff_file_name":""}

BTW, Thank you for a great package in R

@ichsan2895
Copy link
Author

ichsan2895 commented Feb 8, 2023

Here is it the file for reproducibility
Routing-example-r5rv1.zip

@rafapereirabr
Copy link
Member

Dear @ichsan2895 ,

  1. Both functions return the route option with earliest arrival ("fastest route"), but the route can differ if you're going from A -> B , or from B -> A. Furthermore, the detailed_itineraries() and the travel_time_matrix() functions can return different results because they use different routing algorithms, as explained in the documentation of the functions.

  2. As far as I know, R5 chooses the faste route based on the distance and road speed limits. Because speed limits are usually higher in roads with high hierarchy, the hierarchy is implicitly considered.

  3. R5 considers that cars will drive at the max speed limit registered for the road segment in OSM data

  4. As far as I know, car speed is solely determined by the max speed limit but it would indeed be good if the speed could be slightly slower for unpaved roads.

@ichsan2895
Copy link
Author

ichsan2895 commented Feb 8, 2023

Thanks again for fast reply

  1. Both functions return the route option with earliest arrival ("fastest route"), but the route can differ if you're going from A -> B , or from B -> A. Furthermore, the detailed_itineraries() and the travel_time_matrix() functions can return different results because they use different routing algorithms, as explained in the documentation of the functions.

I see.. I will see it later

  1. As far as I know, R5 chooses the faste route based on the distance and road speed limits. Because speed limits are usually higher in roads with high hierarchy, the hierarchy is implicitly considered.

In my case, the r5r doesn't choose the route which has higher road hierarchy. Sometimes it choose lower hierarchy that implied it has lower speed limit => slower trip duration

  1. R5 considers that cars will drive at the max speed limit registered for the road segment in OSM data

What happen if the road doesn't have any max_speed limit in OSM data? Will it be infinity? or maybe capped to 100 km/hour?

  1. As far as I know, car speed is solely determined by the max speed limit but it would indeed be good if the speed could be slightly slower for unpaved roads.

Yes, off course. It would be cool if it can mimic the vehicle speed based on road condition and situation

@rafapereirabr
Copy link
Member

rafapereirabr commented Feb 8, 2023

  1. R5, and hence r5r, will choose the route with shortest travel time regardless of road hierarchy.

  2. What happen if the road doesn't have any max_speed limit in OSM data? Good question. Perhaps @mvpsaraiva could have a quick look into the Java code to find this info ?

@ichsan2895
Copy link
Author

Hello, I'm back

I want to clarify

street_net <- street_network_to_sf(r5r_core)

The I change some value in speed column, then, can I put back the updated value to r5r_core?

@rafapereirabr
Copy link
Member

Hello, I'm back

I want to clarify

street_net <- street_network_to_sf(r5r_core)

The I change some value in speed column, then, can I put back the updated value to r5r_core?

No. Right now, the only way to change the speed of road segments for cars in r5r is to change the speed information in the OpenStreetMap .pbf file, which can be done for example with JOSM (https://wiki.openstreetmap.org/wiki/JOSM).

@abyrd
Copy link

abyrd commented Mar 15, 2023

Hi everyone, StreetLayer uses com.conveyal.r5.labeling.SpeedLabeler#getSpeedMS to determine the speeds on roads, looking first at tags like maxspeed:motorcar or maxspeed:forward and falling back on com.conveyal.r5.point_to_point.builder.SpeedConfig#defaultConfig as the defaults.

https://github.com/conveyal/r5/blob/995635a1750696d1666a6ac06fde827cf1944fd0/src/main/java/com/conveyal/r5/point_to_point/builder/SpeedConfig.java#L62-L78

A related issue is under discussion over at conveyal/r5#863 (comment) and I'm unable to reproduce the problem. I think we'd need specific data sets and exact request parameters sent (as well as exact R5 version number) to further diagnose.

@rafapereirabr
Copy link
Member

Thank you @abyrd for the info. @ichsan2895 , could you please provide the OSM .pbf file you are using and paste below the output of r5r::r5r_sitrep() ?

@ichsan2895
Copy link
Author

ichsan2895 commented Mar 23, 2023

Sorry for slow response..

Here is the PBF that downloaded from QuickOSM QGIS plugin. Why I don't download from Geofabrik?

AFAIK, QuickOSM data is same as geofabrik but I didn't download from Geofabrik since it was to big for my computer (~220 MB for entire Sumatera island), even for processing for crop it to a province.

Please route from B to A using r5r::detailed_itineraries
B = 0.4994880 N, 101.4547 E
A = 0.3610082 N, 101.9085 E

Here is it the pbf data:
Highway_Riau_20230123.osm.pbf.zip

> r5r::r5r_sitrep()
$r5r_package_version
[1] ‘1.0.0’

$r5_jar_version
[1] "6.8"

$java_version
[1] "11.0.16"

$set_memory
[1] "-Xmx2G"

$session_info
R version 4.2.3 (2023-03-15)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Debian GNU/Linux 10 (buster)

Matrix products: default
BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.8.0
LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.8.0

locale:
 [1] LC_CTYPE=en_US.UTF-8          LC_NUMERIC=C                  LC_TIME=en_US.UTF-8          
 [4] LC_COLLATE=en_US.UTF-8        LC_MONETARY=en_US.UTF-8       LC_MESSAGES=en_US.UTF-8      
 [7] LC_PAPER=en_US.UTF-8          LC_NAME=en_US.UTF-8           LC_ADDRESS=en_US.UTF-8       
[10] LC_TELEPHONE=en_US.UTF-8      LC_MEASUREMENT=en_US.UTF-8    LC_IDENTIFICATION=en_US.UTF-8

attached base packages:
[1] parallel  stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] tidygeocoder_1.0.5 mapview_2.11.0     r5r_1.0.0          httpuv_1.6.5       promises_1.2.0.1  
 [6] GGally_2.1.2       reticulate_1.28    yardstick_0.0.9    workflowsets_0.2.1 workflows_0.2.6   
[11] tune_0.2.0         rsample_0.1.1      recipes_0.2.0      modeldata_0.1.1    infer_1.0.0       
[16] dials_0.1.1        scales_1.1.1       broom_0.7.12       tidymodels_0.2.0   parsnip_0.2.1     
[21] modeltime_1.2.0    tibbletime_0.1.6   h2o_3.36.0.4       forecast_8.16      TSstudio_0.1.6    
[26] tseries_0.10-50    mFilter_0.1-5      vars_1.5-6         lmtest_0.9-40      urca_1.3-0        
[31] strucchange_1.5-2  sandwich_3.0-1     zoo_1.8-9          MASS_7.3-57        DataExplorer_0.8.2
[36] SmartEDA_0.3.8     timetk_2.8.0       anomalize_0.2.2    gridExtra_2.3      forcats_0.5.1     
[41] stringr_1.4.0      purrr_0.3.4        readr_2.1.2        tidyr_1.2.0        tibble_3.1.6      
[46] tidyverse_1.3.1    randomForest_4.7-1 plotly_4.10.0      nnet_7.3-17        snow_0.4-4        
[51] caret_6.0-91       lattice_0.20-45    ggplot2_3.3.5      sf_1.0-9           dplyr_1.0.8       
[56] raster_3.6-20      rgdal_1.6-5        PROJ_0.4.0         sp_1.6-0           gstat_2.1-0       

loaded via a namespace (and not attached):
  [1] rappdirs_0.3.3          spacetime_1.2-8         ModelMetrics_1.2.2.2    intervals_0.15.2       
  [5] bit64_4.0.5             knitr_1.38              data.table_1.14.2       rpart_4.1.16           
  [9] inline_0.3.19           hardhat_0.2.0           RCurl_1.98-1.6          generics_0.1.2         
 [13] leaflet_2.1.1           GPfit_1.0-8             callr_3.7.0             terra_1.7-18           
 [17] proxy_0.4-26            future_1.24.0           bit_4.0.4               tzdb_0.3.0             
 [21] webshot_0.5.3           xml2_1.3.3              lubridate_1.8.0         StanHeaders_2.21.0-7   
 [25] assertthat_0.2.1        gower_1.0.0             xfun_0.30               hms_1.1.1              
 [29] jquerylib_0.1.4         rJava_1.0-6             satellite_1.0.4         evaluate_0.15          
 [33] fansi_1.0.3             progress_1.2.2          dbplyr_2.1.1            readxl_1.4.0           
 [37] igraph_1.3.0            DBI_1.1.2               quantmod_0.4.20         htmlwidgets_1.5.4      
 [41] reshape_0.8.9           stats4_4.2.3            ellipsis_0.3.2          crosstalk_1.2.0        
 [45] backports_1.4.1         RcppParallel_5.1.5      vctrs_0.4.1             here_1.0.1             
 [49] TTR_0.24.3              withr_2.5.0             checkmate_2.0.0         vroom_1.5.7            
 [53] xts_0.12.1              prettyunits_1.1.1       svglite_2.1.0           lazyeval_0.2.2         
 [57] crayon_1.5.1            leaflet.providers_1.9.0 pkgconfig_2.0.3         labeling_0.4.2         
 [61] units_0.8-0             nlme_3.1-158            rlang_1.0.6             globals_0.14.0         
 [65] lifecycle_1.0.1         modelr_0.1.8            cellranger_1.1.0        rprojroot_2.0.3        
 [69] matrixStats_0.61.0      Matrix_1.4-1            loo_2.5.1               reprex_2.0.1           
 [73] base64enc_0.1-3         processx_3.5.3          png_0.1-7               viridisLite_0.4.0      
 [77] bitops_1.0-7            KernSmooth_2.23-20      pROC_1.18.0             classInt_0.4-3         
 [81] brew_1.0-7              parallelly_1.31.0       lpSolve_5.6.15          magrittr_2.0.3         
 [85] plyr_1.8.7              compiler_4.2.3          sweep_0.2.3             RColorBrewer_1.1-3     
 [89] snakecase_0.11.0        cli_3.2.0               DiceDesign_1.9          listenv_0.8.0          
 [93] ps_1.6.0                tidyselect_1.1.2        stringi_1.7.6           yaml_2.3.5             
 [97] grid_4.2.3              sass_0.4.1              tools_4.2.3             future.apply_1.8.1     
[101] uuid_1.0-4              rstudioapi_0.13         foreach_1.5.2           leafpop_0.1.0          
[105] janitor_2.1.0           prodlim_2019.11.13      farver_2.1.0            digest_0.6.29          
[109] FNN_1.1.3.1             lava_1.6.10             quadprog_1.5-8          networkD3_0.4          
[113] Rcpp_1.0.10             prophet_1.0             later_1.3.0             httr_1.4.5             
[117] colorspace_2.0-3        rvest_1.0.2             fs_1.5.2                splines_4.2.3          
[121] xgboost_1.5.2.1         systemfonts_1.0.4       jsonlite_1.8.0          leafem_0.2.0           
[125] timeDate_3043.102       rstan_2.21.5            ISLR_1.4                sfheaders_0.4.0        
[129] ipred_0.9-12            gt_0.4.0                R6_2.5.1                lhs_1.1.5              
[133] pillar_1.7.0            htmltools_0.5.4         glue_1.6.2              fastmap_1.1.0          
[137] class_7.3-20            codetools_0.2-18        pkgbuild_1.3.1          furrr_0.2.3            
[141] utf8_1.2.2              bslib_0.3.1             curl_4.3.2              survival_3.3-1         
[145] rmarkdown_2.13          sampling_2.9            munsell_0.5.0           e1071_1.7-9            
[149] iterators_1.0.14        haven_2.4.3             fracdiff_1.5-1          reshape2_1.4.4         
[153] gtable_0.3.0

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