Road distances and trip duration matrix for Brazilian municipalities

Introduction

Distance information between places is useful to evaluate the proximity and interconnection of regions. The Euclidean distance between two places, although simple and easy to compute, is not realistic in terms of dislocation costs. This dataset present the road distance and trip duration metrics between all Brazilian municipalities.

Methods

As reference to starting and ending point of the routes, I opted to use the municipality seat localization (“sede dos municípios”), which is usually the city hall or historic downtown localization. This data is originally generated by the Brazilian Institute of Geography and Statistics (IBGE) and available on the {geobr} package. The last available data is for the year of 20101 and present 5,565 features.

With the list of municipalities and respective seat geographic coordinates, a list of pairs of municipalities is computed using simple combinatorial analysis. Example:

x <- c("a", "b", "c", "d")

combn(x, m = 2)
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,] "a"  "a"  "a"  "b"  "b"  "c" 
[2,] "b"  "c"  "d"  "c"  "d"  "d" 
Note

With this, I assume that the distance between two municipalities is the same, independently of the direction.

Thus, for the 5,565 municipalities, we will have a total number of routes to be computed:

choose(n = 5565, k = 2)
[1] 15481830

To compute the road distance between pairs of municipalities, the OSRM API service was used, with the {osrm} package (Giraud 2022). More specifically, the table service was used, considering the “car” profile, returning as result the distance in meters and estimated trip duration in minutes for the fastest route found.

Route example

For example, lets compute the road distance between Rio de Janeiro, RJ and São Paulo, SP with the {osrm} package.

library(geobr)
library(osrm)
library(leaflet)
library(sf)

seats <- read_municipal_seat(showProgress = FALSE)

rj <- subset(seats, code_muni == 3304557)
sp <- subset(seats, code_muni == 3550308)

route <- osrmRoute(
  src = rj, dst = sp, 
  overview = "full",
  osrm.profile = "car"
)
# Route distance, in meters
route$distance
[1] 422.436
# Route duration, in minutes
route$duration
[1] 320.7633
route |>
  st_transform(4326) |>
  leaflet() |>
  addTiles() |>
  addPolylines()
Important

For some pairs of municipalities, the OSRM service is not able to determine a possible road route. This is expected, as some municipalities are reachable only by plane or boat.

The scripts used to prepare the dataset are available here.

Dataset download

The dataset with the road distances and trip duration are available on Zenodo, on RDS format, parquet format, and CSV (zipped) format.

Click the link below to access and download the data.

You can also download the dataset directly from R, using the {zendown} package.

# install.packages("zendown")
library(zendown)

dist_brasil_file <- zen_file(11400243, "dist_brasil.rds")

dist_brasil <- readRDS(dist_brasil_file)

head(dist_brasil)
     orig    dest   dist   dur
1 1100015 1100023 330534 313.6
2 1100015 1100031 397600 380.7
3 1100015 1100049 112805 113.9
4 1100015 1100056 390938 373.2
5 1100015 1100064 353606 333.9
6 1100015 1100072 321636 348.5

Graphs

library(ggplot2)

ggplot(data = dist_brasil, aes(x = dist/1000)) +
  geom_histogram(bins = 100) +
  labs(
    title = "Fastest route road distance between Brazilian municipalities", 
    x = "Distance (km)", y = "count"
  ) +
  theme_bw()

ggplot(data = dist_brasil, aes(x = dur/60)) +
  geom_histogram(bins = 100) +
  labs(
    title = "Fastest route estimated trip duration between Brazilian municipalities", 
    x = "Trip duration (hours)", y = "count"
  ) +
  theme_bw()

Future plans

  • Compute routes using other available routing services.

  • Yearly updates of the dataset, as the road infrastructure may change.

Back to top

References

Giraud, Timothée. 2022. osrm: Interface Between R and the OpenStreetMap-Based Routing Service OSRM.” Journal of Open Source Software 7 (78): 4574. https://doi.org/10.21105/joss.04574.

Footnotes

  1. As new data for the 2022 Brazilian Census be available, the dataset will be likely updated.↩︎