ropensci / stplanr

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

Forcing route() to stay within the country #472

Closed joshpersi closed 2 years ago

joshpersi commented 2 years ago

Hi everyone,

I'm trying to map a car route from Toronto to Vancouver and unsurprisingly, the shortest route often cuts through the United States. Is there any way I could have the route() function find the shortest route within Canada?

Here is a reproducible example:

library(stplanr)
library(osrm)

trip <- route(
  from = "Toronto",
  to = "Vancouver",
  route_fun = osrmRoute,
  returnclass = "sf"
)

mapview::mapview(trip) 

Any advice would be much appreciated!

Thanks,

Josh

Robinlovelace commented 2 years ago

Not sure, that's a question for the routing engines, cannot think of a solution based on {stplanr} code other than either some code to check if a route strays into unwanted places, calculate the route to an intermediate point inside the wanted polygon, and save the entire route or repeate if it does not work.

Did you figure out a solution @josh-persi ? Apologies for the very slow reply, closing for now but keen to hear how you got on.