knitr::opts_chunk$set(collapse = TRUE, comment = "#>") has_sfn <- requireNamespace("sfnetworks", quietly = TRUE) has_tg <- requireNamespace("tidygraph", quietly = TRUE)
library(osmnxr) g <- ox_example("olinda") # a small real network bundled with the package
An osm_graph is deliberately thin: tidy sf nodes and edges. That makes it
easy to hand off to the rest of the R spatial-network ecosystem, or to export
for other tools.
The nodes and edges are always available as sf:
parts <- ox_as_sf(g) parts$edges
Convert to an sfnetwork for the tidygraph verb workflow, or to a bare
tbl_graph:
net <- ox_as_sfnetwork(g) net
tg <- ox_as_tidygraph(g) tg
ox_as_dodgr() returns the column layout dodgr expects, so you can use its
highly optimised routing directly:
library(dodgr) graph <- ox_as_dodgr(g) dodgr_dists(graph, from = graph$from_id[1], to = graph$to_id[10])
Export edges to GeoJSON, or build a MapLibre GL style fragment that points at the written data:
gj <- tempfile(fileext = ".geojson") ox_to_geojson(g, gj) style <- ox_to_maplibre(g, tempfile(fileext = ".geojson")) str(style, max.level = 2)
ox_save_graphml() / ox_load_graphml() persist the graph in a format
compatible with OSMnx, NetworkX and Gephi. Edge geometry is stored as WKT, so
the round-trip is lossless:
f <- tempfile(fileext = ".graphml") ox_save_graphml(g, f) g2 <- ox_load_graphml(f) ox_basic_stats(g2)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.