ropensci / stplanr

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

Tried creating a route from desirelines using osrm function #553

Closed subha-nair closed 3 months ago

subha-nair commented 8 months ago

Able to read geojson file (which has desirelines from origin and destination points) but the route function is generating error.

Followed documentation - https://docs.ropensci.org/stplanr/

code error

outputs

Looks like Routes function is generating a list instread of spatial file

Robinlovelace commented 8 months ago

That does seem to be from your screenshots @subha-nair. Can you share a reproducible example?

subha-nair commented 8 months ago

desirelines <-sf::st_read("Desirelines.geojson")

mapview::mapview(desirelines) [

routes <- route( l = desirelines, route_fun = route_osrm, osrm.profile ="car", returnclass = "sf" )

plot(sf::st_geometry(routes))

Desirelines.zip

Robinlovelace commented 8 months ago

Function works OK:

library(stplanr)
library(sf)
#> Linking to GEOS 3.11.1, GDAL 3.6.4, PROJ 9.1.1; sf_use_s2() is TRUE
# ?route_osrm
l1 = od_data_lines[49, ]
l1m = od_coords(l1)
from = l1m[, 1:2]
to = l1m[, 3:4]
r_foot = route_osrm(from, to)
r_bike = route_osrm(from, to, osrm.profile = "bike")
r_car = route_osrm(from, to, osrm.profile = "car")
plot(r_foot$geometry, lwd = 9, col = "grey")
plot(r_bike, col = "blue", add = TRUE)
#> Warning in plot.sf(r_bike, col = "blue", add = TRUE): ignoring all but the
#> first attribute
plot(r_car, col = "red", add = TRUE)
#> Warning in plot.sf(r_car, col = "red", add = TRUE): ignoring all but the first
#> attribute

Created on 2023-11-21 with reprex v2.0.2

Robinlovelace commented 8 months ago

But not within a route() call:

library(stplanr)
library(sf)
#> Linking to GEOS 3.11.1, GDAL 3.6.4, PROJ 9.1.1; sf_use_s2() is TRUE

u = "https://github.com/ropensci/stplanr/files/13431574/Desirelines.zip"
f = basename(u)
if (!file.exists(f)) {
  download.file(u, f)
}
unzip(f)
l = sf::read_sf("Desirelines.geojson")
l$length = as.numeric(sf::st_length(l))
summary(l$length)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>       0    4314   14740   53992   54380  491260
l_2km = l[l$length > 1900 & l$length < 2100, ]
l_minimal = l_2km[1:5, ]
# mapview::mapview(l_minimal)
r = route(l = l_minimal, route_fun = route_osrm, wait = 1)
#> Most common output is subscriptOutOfBoundsError
#> These routes failed: 2, 3, 4, 5
#> The first of which was:
#> <subscriptOutOfBoundsError in ldf[i, 1:2]: subscript out of bounds>
plot(r)
#> Warning: plotting the first 9 out of 10 attributes; use max.plot = 10 to plot
#> all

Created on 2023-11-21 with reprex v2.0.2

Robinlovelace commented 8 months ago

Only the first route is being returned for some reason.

subha-nair commented 8 months ago

desirelines <-sf::read_sf("Desirelines.geojson")

r_car <- desirelines[1:10, ]

mapview::mapview(r_car)

routes <- route(l = r_car, route_fun = route_osrm, wait = 1)

plot(sf::st_geometry(routes))

Oh yes, tried the first 10 lines - only 1 was generated. Wanted to understand why we calculate the st_length of the desire lines, Would prefer calculating length of the routes.

subha-nair commented 8 months ago

But more importantly have 3715 lines to be converted to routes

subha-nair commented 3 months ago

Update:

Finally got the code to work with a slight change (issue was the desireline was a multiline string instead of a linestring)

Apply stplanr route function with route_osrm as route function

routes = stplanr::route(l = sf, route_fun = osrmRoute, returnclass = "sf")

Plot the identified routes on a map by distance

plot(sf::st_geometry(routes))

But some of the lines are straight lines instead of routes, not sure why

routes

routes.zip

Robinlovelace commented 3 months ago

Hi @subha-nair that's great to hear, good work! I guess some of the route failed, for whatever reason, leading to a few straight lines. Closing this issue for now, keep us posted..