Closed Robinlovelace closed 5 years ago
No time like the present!
See https://github.com/ITSLeeds/pct/issues/27
Heads-up @mpadge and @layik.
A "proper" implementation needs a few efficiency tweaks from that code, mostly through using match_pts_to_graph()
. Point me to where in the code this should live, and I'll happily implement it.
Many thanks Mark. The code would live here:
https://github.com/ropensci/stplanr/blob/master/R/route.R#L85
Cool! I've got a day off tomorrow, and a weekend of fun times, but will get on to it first thing Monday.
Great. Here's where my code gets to (downloading the rnet if it's not already there, which will be really useful IMO). I await your PR, many thanks!
devtools::install_github("ropensci/stplanr")
#> Skipping install of 'stplanr' from a github remote, the SHA1 (4b1c758c) has not changed since last install.
#> Use `force = TRUE` to force installation
library(stplanr)
from <- sf::st_coordinates(cents_sf[1, ])
to <- sf::st_coordinates(cents_sf[2, ])
r <- route_dodgr(from, to)
#> Warning: plotting the first 9 out of 30 attributes; use max.plot = 30 to
#> plot all
Created on 2019-03-14 by the reprex package (v0.2.1)
Heads-up @mpadge I've had a first bash at this. Look OK to you? I've used nabor
rather than dodgr
to match the points, which is probably not optimal. Very happy to see it working in any case. Next step, I guess, is adding route (optional) route statistics, such as distance and perhaps other summary stats.
yo @Robinlovelace, I've done this, but just need to iron out a couple of issues before I push. Didn't modify your code that much, but just implemented the ability to input multiple from
and to
points, and to return a full multi-row sf
data.frame
with details of start and end points, and geometry containing the routes. I'll send PR tomorrow morning
Sounds great to me. Many thanks, looking forward to the PR - being able to pass route_dodgr()
many OD pairs will be awesome.
Hi, I'm trying to use the function and I can not get past this error:
_Error in is_numeric_matrix(x) : is.numeric(x) && is.matrix(x) is not TRUE
_
I suppose that as.matrix () is not converting the list properly to pass it to st_linestring (). I am not very expert in the subject and I would like to know if it is only with my data or it is a problem of the function.
Greetings,
That's likely because your installed package versions are a bit out of date. Please try remotes::install_github("atfutures/dodgr")
and let us know if that fixes. Maybe also same for stplanr
° If that doesn't work, then please try to include some reproducible code
Thank you very much, I did what you said and cleaned the workspace but I still get the same error:
library(dplyr)
#> Warning: package 'dplyr' was built under R version 3.6.1
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(sf)
#> Warning: package 'sf' was built under R version 3.6.1
#> Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
library(dodgr)
library(stplanr)
#> Registered S3 method overwritten by 'R.oo':
#> method from
#> throw.default R.methodsS3
bb99 <- c(-8.5728, 43.2368,-8.1657,43.4034)
#Descargar las calles del area a un osmdata_sf
area99<- opq(bbox = bb99) %>%
add_osm_feature(key = 'highway')%>% osmdata_sf()
#Objeto sf
area99_sf <- area99$osm_lines %>%
st_transform(crs ="+proj=longlat +ellps=WGS84")
from <- structure(list(X = c(-8.42006027698517, -8.20952297835075, -8.42643857002258,
-8.41481924057007, -8.41923952102661, -8.41465830802918, -8.41951957008356,
-8.38328182697296, -8.35565439254155, -8.43316555023194, -8.4194424547759,
-8.42190563678742, -8.40007245540619, -8.44299852848053, -8.41782331466675
), Y = c(43.3650984295013, 43.2767695265241, 43.3608434951152,
43.3577388817328, 43.3713262417024, 43.3647396381231, 43.3586539122512,
43.3067747933252, 43.3028463919537, 43.3757325102483, 43.3605667983998,
43.3584838474195, 43.3795731114911, 43.3236166758399, 43.3605899836471
)), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15"), class = "data.frame")
to <- structure(list(X = -8.413942, Y = 43.36652), class = "data.frame", row.names = "1")
r <- route_dodgr(from, to, net = area99_sf)
#> Error in is_numeric_matrix(x): is.numeric(x) && is.matrix(x) is not TRUE
Created on 2019-07-13 by the reprex package (v0.3.0)
Yep, I can reproduce that - thanks @gert08 for the code. I'll dig deeper, and report back
@Robinlovelace It's a bug in the route_dodgr
code - one of those paths comes back empty, and these lines then fail. Do you want me to PR a fix? There's also another bit of a bug, in that the from/to
pairs are submitted as matched columns of the od_coordinate_matrix
object, and so in the case given of a single to
point simply repeats those coordinates to create a to
column the same length as from. This is, however, not how
dodgris set up, and it ends up doing
nrow` times as many calculations as necessary. I should fix that too ...
Hi @gert08 thanks for the reproducible example, and massive thanks to @mpadge for the quick fix.
Full reprex with current version of stplanr
gives:
library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.1.2, PROJ 4.9.3
library(stplanr)
#> Registered S3 method overwritten by 'R.oo':
#> method from
#> throw.default R.methodsS3
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
packageVersion("stplanr")
#> [1] '0.2.10.9000'
bb99 <- c(-8.5728, 43.2368,-8.1657,43.4034)
area99<- opq(bbox = bb99) %>%
add_osm_feature(key = 'highway')%>% osmdata_sf()
#Objeto sf
area99_sf <- area99$osm_lines %>%
st_transform(crs ="+proj=longlat +ellps=WGS84")
from <- structure(list(X = c(-8.42006027698517, -8.20952297835075, -8.42643857002258,
-8.41481924057007, -8.41923952102661, -8.41465830802918, -8.41951957008356,
-8.38328182697296, -8.35565439254155, -8.43316555023194, -8.4194424547759,
-8.42190563678742, -8.40007245540619, -8.44299852848053, -8.41782331466675
), Y = c(43.3650984295013, 43.2767695265241, 43.3608434951152,
43.3577388817328, 43.3713262417024, 43.3647396381231, 43.3586539122512,
43.3067747933252, 43.3028463919537, 43.3757325102483, 43.3605667983998,
43.3584838474195, 43.3795731114911, 43.3236166758399, 43.3605899836471
)), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15"), class = "data.frame")
to <- structure(list(X = -8.413942, Y = 43.36652), class = "data.frame", row.names = "1")
r <- route_dodgr(from, to, net = area99_sf)
#> Error in is_numeric_matrix(x): is.numeric(x) && is.matrix(x) is not TRUE
plot(r)
#> Error in plot(r): object 'r' not found
Created on 2019-07-15 by the reprex package (v0.3.0)
Thank you and @mpadge for your work, know it's working, but in the original file I have 288 points of which I only found paths for 222 points towards the destination. Do you think I could be missing something?
Update showing working code below. @gert08 could you provide clarity on what you expect the output to be based on this reprex?
remotes::install_github("atfutures/dodgr")
#> Skipping install of 'dodgr' from a github remote, the SHA1 (d8bf3fb1) has not changed since last install.
#> Use `force = TRUE` to force installation
remotes::install_github("mpadge/stplanr")
#> Skipping install of 'stplanr' from a github remote, the SHA1 (a342489f) has not changed since last install.
#> Use `force = TRUE` to force installation
library(sf)
#> Linking to GEOS 3.5.1, GDAL 2.1.2, PROJ 4.9.3
library(stplanr)
#> Registered S3 method overwritten by 'R.oo':
#> method from
#> throw.default R.methodsS3
library(osmdata)
#> Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
packageVersion("stplanr")
#> [1] '0.2.10.9000'
bb99 <- c(-8.5728, 43.2368,-8.1657,43.4034)
area99<- opq(bbox = bb99) %>%
add_osm_feature(key = 'highway')%>% osmdata_sf()
#Objeto sf
area99_sf <- area99$osm_lines %>%
st_transform(crs ="+proj=longlat +ellps=WGS84")
from <- structure(list(X = c(-8.42006027698517, -8.20952297835075, -8.42643857002258,
-8.41481924057007, -8.41923952102661, -8.41465830802918, -8.41951957008356,
-8.38328182697296, -8.35565439254155, -8.43316555023194, -8.4194424547759,
-8.42190563678742, -8.40007245540619, -8.44299852848053, -8.41782331466675
), Y = c(43.3650984295013, 43.2767695265241, 43.3608434951152,
43.3577388817328, 43.3713262417024, 43.3647396381231, 43.3586539122512,
43.3067747933252, 43.3028463919537, 43.3757325102483, 43.3605667983998,
43.3584838474195, 43.3795731114911, 43.3236166758399, 43.3605899836471
)), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15"), class = "data.frame")
to <- structure(list(X = -8.413942, Y = 43.36652), class = "data.frame", row.names = "1")
r <- route_dodgr(from, to, net = area99_sf)
#> unable to trace path number 10
plot(r)
Created on 2019-07-15 by the reprex package (v0.3.0)
In the reprex code I could not upload the whole file for a privacy issue, so I used a sample of 10 points to build from
but in the complete file I have 288 points of which the function was only able to direct 222 to the point to
, losing 66 paths. I don't know if I explained well now.
@gert08 I've just done another tweak via #326 that should increase the number of successfully connecting paths - let us know, and/or do remotes::install_github("mpadge/stplanr")
in case you want to try it before it gets merged here.
@mpadge I keep getting the same results.
r=stplanr::route_dodgr(from,to, net = area99_sf)
#> unable to trace path number 103032335669798995108114115129131138150151159161166170179187189203238252253_
sessionInfo()
#> R version 3.6.0 (2019-04-26)
#> Platform: x86_64-w64-mingw32/x64 (64-bit)
#> Running under: Windows 10 x64 (build 17763)
#>
#> Matrix products: default
#>
#> Random number generation:
#> RNG: Mersenne-Twister
#> Normal: Inversion
#> Sample: Rounding
#>
#> locale:
#> [1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252
#> [3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
#> [5] LC_TIME=English_United States.1252
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] reprex_0.3.0 dodgr_0.2.0.004 dplyr_0.8.3 osmdata_0.1.1
#> [5] sf_0.7-6 stplanr_0.2.10.9000
#>
#> loaded via a namespace (and not attached):
#> [1] httr_1.4.0 jsonlite_1.6 splines_3.6.0 R.utils_2.9.0
#> [5] RcppParallel_4.4.3 assertthat_0.2.1 sp_1.3-1 remotes_2.0.2
#> [9] pillar_1.4.2 backports_1.1.4 lattice_0.20-38 glue_1.3.1
#> [13] digest_0.6.20 rvest_0.3.4 htmltools_0.3.6 Matrix_1.2-17
#> [17] R.oo_1.22.0 clipr_0.6.0 pkgconfig_2.0.2 raster_2.9-23
#> [21] fda_2.4.8 purrr_0.3.2 processx_3.3.1 whisker_0.3-2
#> [25] openxlsx_4.1.0.1 fda.usc_1.4.0 tibble_2.1.3 mgcv_1.8-28
#> [29] withr_2.1.2 cli_1.1.0 magrittr_1.5 crayon_1.3.4
#> [33] maptools_0.9-5 evaluate_0.12 ps_1.3.0 R.methodsS3_1.7.1
#> [37] fs_1.2.6 plogr_0.2.0 nlme_3.1-139 MASS_7.3-51.4
#> [41] xml2_1.2.0 foreign_0.8-71 class_7.3-15 pkgbuild_1.0.2
#> [45] tools_3.6.0 prettyunits_1.0.2 geosphere_1.5-10 stringr_1.4.0
#> [49] zip_2.0.3 callr_3.2.0 compiler_3.6.0 e1071_1.7-2
#> [53] rlang_0.4.0 classInt_0.3-3 units_0.6-3 grid_3.6.0
#> [57] rstudioapi_0.8 igraph_1.2.4.1 rmarkdown_1.10 codetools_0.2-16
#> [61] DBI_1.0.0 curl_3.3 R6_2.4.0 lubridate_1.7.4
#> [65] knitr_1.21 rgeos_0.4-3 rprojroot_1.3-2 KernSmooth_2.23-15
#> [69] stringi_1.4.3 Rcpp_1.0.1 rpart_4.1-15 tidyselect_0.2.5
#> [73] xfun_0.4
Created on 2019-07-16 by the reprex package (v0.3.0)
@gert08 Not sure what you mean by same results? There may always be some paths that do connect if your submitted network itself does not fully connect (and even then untraceable paths may be theoretically possible because of such things as one-way restrictions). What you should see is a reduction in the number of untraceable paths; or (equivalently) an increase in the number of paths actually returned.
Can you also please include more reprex
code for the above, and I'll use that to provide a more informative message that the one you've got above. (With your code as given above, both @Robinlovelace and I got only #> unable to trace path number 10
, but it looks like you've got a bigger network there, or more from/to
points, or both?) Thanks!
@mpadge it's the same code, just like I said before, in the first I just used a sample of 10 points to build from
but in the complete network I have 288 points of which the function was only able to direct 222 to the point to
, losing 66 paths.
It's right, with the tweak #326 has increased to 226 the number of successfully connecting paths.
@gert08 thanks for the follow-up. Just to clarify: do you think that the function is still not working as it should?
Yes, I think it's working. I will try to add more features to the over pass query object to see if I get more connected routes. Thank a lot for your work and help.
No problem @gert08, thank you for taking the time to test it and ask questions, which help hugely when developing a package.
As discussed with @mpadge. Related to #246. Links to the need for a
route_local()
function that wrapsSpatialLinesNetwork()
.