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

Tried creating a route from desirelines using osrm function #553

Closed
subha-nair opened this issue Nov 21, 2023 · 9 comments
Closed

Tried creating a route from desirelines using osrm function #553

subha-nair opened this issue Nov 21, 2023 · 9 comments

Comments

@subha-nair
Copy link

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
Copy link
Member

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

@subha-nair
Copy link
Author

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
Copy link
Member

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
Copy link
Member

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
Copy link
Member

Only the first route is being returned for some reason.

@subha-nair
Copy link
Author

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
Copy link
Author

But more importantly have 3715 lines to be converted to routes

@subha-nair
Copy link
Author

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
Copy link
Member

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..

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

2 participants