Edited with additional details added
I have a shapefile of 2,061 Open Street Map (OSM) road segments. Each segment in my shapefile is is identified by its OSM Way ID.
Here is an example of five segments from my data:
data =
structure(list(osm_id = structure(1:5, .Label = c("17990110",
"17993246", "17994983", "17994985", "17995338"), class = "factor"),
name = structure(c(1L, 3L, 4L, 5L, 2L), .Label = c("109th Avenue Northeast",
"85th Avenue Northeast", "Bunker Lake Boulevard Northeast",
"Foley Boulevard", "Northdale Boulevard Northwest"), class = "factor")), row.names = c(NA,
5L), class = c("sf", "data.frame"))
For each of these 2061 road segments, I want to count the number of intersections with other roads, separately for each road type (residential, primary, tertiary...).
For example, this OSM way intersects 27 other ways, 11 of which are "residential," and 3 of which are "secondary" highways.
This is secondary to the analysis, but ultimately, for intersections where multiple types of roads join, I will select the "largest" type of road. For example, this node joins a service road and residential road; I would like to select the residential road road. I think I can create my own hierarchy list for this and deal with it later.
I am trying to use R Open Sci package osmdata
So far I can get at #2 (signalized intersections) using osmdata:
node_dat <- opq_osm_id(type = "node", id = '17990110')%>%
opq_string()%>%
osmdata_sf
node_dat_pts <- node_dat$osm_points
nrow(node_dat_pts[node_dat_pts$traffic_signals %in% "signal",])
This reveals that there are three traffic signals along this OSM segment. (Although, in reality, there are only two signalized intersections -- two signals are associated with a divided highway...but that might be a problem for another time).
I'm an R native, which is why the osmdata package is so appealing to me, but I'm open to exploring querying in Overpass API. TBH I found the example on how to get intersection nodes on the website not quite applicable -- and I'm unclear of how to scale up this process to the long list of 2000+ way IDs that I have (but if the docs or an example exist, point me to it).
I'm also open to exploring other tool libraries in Python; the Python osmnx package has what seem like excellent tools to tool to calculate "intersection density," but for specified polygons, like city boundaries, and does not seem to have functionality to create calls within Python by way or node ID.
I also know that I could probably do this in ArcGIS or QGIS, but because I have these OSM IDs handy in my database already, it just seems like a waste to load a whole dang shapefile of intersections for a large metropolitan region and do some kind of buffering process to get the information I need. Plus if I had a script handy to extract some pieces of information by way or node ID, I could more easily broaden the kinds of data I extract to get other tidbits of great information recorded for OSM elements.
Thank you spatial data community!