I'm trying to build a graph from a osm file (large ones, continent sized). I can do this with R by using osmextract which gives me sf lines, and then doing some data wrangling. However, this is extremely slow, and perhaps there is software that does this faster?

Basically I need data that I can then use with a graph query package like cppRouting

from to weight
1    2  123
2    3  34
3    4  100

This is what I'm doing with R:


berlin_data <- oe_read("./data/berlin-latest.osm.pbf")
berlin_data_small <- filter(berlin_data
                         , highway %in%
                           c("primary", "secondary", "bridleway"))

edges <- dplyr::select(berlin_data_small, geometry) %>%
    mutate(line_id = 1:n()) %>% 

nodes <- dplyr::select(edges, geometry) %>%
    distinct() %>%
    mutate(id = 1:n())

edges_nodes <- st_join(edges, nodes)

reshape <- function(df) {
    l <- nrow(df)
    cbind(rename(df[1:l-1, ], from = id, g_from = geometry)
        , rename(df[2:l, ], to = id, g_to = geometry))

edges_nodes_d <- reshape(edges_nodes) %>%

edges_nodes_d <- mutate(edges_nodes_d
                     , weight = as.numeric(st_distance(g_from, g_to
                                                     , by_element = TRUE
                                                     , which = "Great Circle")))

I tried osm2po but it seems to only produce gph and 2po files which I don't see how to read from R.

1 Answer 1


I had the exact same problem, i.e. extract massive road networks from osm.pbf files. Just use the magical osm4routing2 application. It's written in Rust but very easy to use even for non rust user like me. It takes an osm.pbf file as input and outputs two csv files, one for the nodes and one for the edges. Very powerful with no RAM problem even for massive data.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.