# Transport routing with stplanr

    library(stplanr)

## This vignette is work in progress - watch this space!

    roads_iow = sf::read_sf("https://github.com/ropensci/stplanr/releases/download/0.3.1/roads_iow.geojson")

# osrm

Then in bash run the following commands to make the OSRM docker image work for you.

    docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-extract -p /opt/car.lua /data/iow.pbf
    docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-partition /data/iow.osrm
    docker run -t -v "${PWD}:/data" osrm/osrm-backend osrm-customize /data/iow.osrm
    docker run -t -i -p 5000:5000 -v "${PWD}:/data" osrm/osrm-backend osrm-routed --algorithm mld /data/iow.osrm
    curl "http://127.0.0.1:5000/route/v1/driving/13.388860,52.517037;13.385983,52.496891?steps=true"

Now we can do routing in R!

    l = pct::wight_lines_30
    p = line2points(l)
    r = osrm::osrmRoute(src = p[1, ], dst = p[2, ], returnclass = "sf")
    plot(r)

    route_osrm2 = function(l) {
      p = line2points(l)
      s = (1:nrow(l)) * 2 - 1
      list_out = lapply(s, function(i) osrm::osrmRoute(p[i, ], dst = p[i + 1, ], returnclass = "sf"))
      do.call(rbind, list_out)
    }
    routes_osrm = route_osrm2(l)
    plot(routes_osrm)
    # tidy up
    file.remove("iow.pbf")