An open API service indexing awesome lists of open source software.

https://github.com/robinlovelace/od_to_routes_osrm

Repository demonstrating how to convert OD data to routes on a big scale
https://github.com/robinlovelace/od_to_routes_osrm

Last synced: about 1 month ago
JSON representation

Repository demonstrating how to convert OD data to routes on a big scale

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
library(tidyverse)
library(stplanr)
library(tmap)
tmap_mode("view")
```

# od_to_routes_osrm

The goal of od_to_routes_osrm is to demonstrate how to calculate journey times and geographic routes between OD pairs rapidly with a local instance of a routing engine (OSRM in the first instance).

Start by reading in the data.

```{r}
origins = read_csv("KY_Origins.csv")
destinations = read_csv("KY_Destinations.csv")
destinations_updated = destinations %>%
rename(tx = Lat, ty = Long)
# od::od_points_to_od_all # this function does not exist]
od_matrix = expand.grid(origins$OID_, destinations$OID_)
od_df = as_tibble(od_matrix)
names(od_df)[1] = names(origins)[1]
names(od_df)[2] = "OID_D"
od_df_o = left_join(od_df, origins)
names(destinations)[1] = "OID_D"
od_df_od = left_join(od_df_o, destinations_updated)
od_df_od_distinct = od_df_od %>%
distinct(Lat, Long, tx, ty, .keep_all = TRUE)
od_coords = od_df_od_distinct %>%
select(fx = Lat, fy = Long, tx = tx, ty = ty)
od_sf = od::odc_to_sf(odc = od_coords)
od_sf$origin_id = od_df_od_distinct$OID_
od_sf$destination_id = od_df_od_distinct$OID_D
# map with sample of 1000 od pairs
od_sf$length = as.numeric(sf::st_length(od_sf))
summary(od_sf$length)
od_sf_short = od_sf %>%
filter(length < 2000000)

od_sf_short %>%
qtm()
```

Do the routing with OSRM:

```{r, eval=FALSE}
od_routes_osrm = route(l = od_sf_short, route_fun = route_osrm, osrm.profile = "car")
sf::write_sf(od_routes_osrm, "od_routes_osrm.geojson")
```

```{r}
od_routes_osrm = sf::read_sf("od_routes_osrm.geojson")
qtm(od_routes_osrm)
```

```{r, eval=FALSE}
od_routes = route(l = od_sf_short, route_fun = route_google)
sf::write_sf(od_routes, "od_routes.geojson")
```

```{r}
od_routes = sf::read_sf("od_routes.geojson")
qtm(od_routes)
```