tzema
tzema

Reputation: 461

R: find overlapping geometries: link routes and roads

I have a dataset of transport routes (sf objects) and I would like to link them to streets (ie to know which streets are contained in each route). What could be a potential approach?

Some (very few) dummy data:


my_route=data.frame(
  stringsAsFactors = FALSE,
                 origin = c("E02001186", "E02001260"),
                 destination = c("E02001186", "E02001260"),
                 geometry = c("c(-2.438813, -2.438813, 53.429707, 53.429707)",
                       "c(-2.350659, -2.350659, 53.457229, 53.457229)")
)

roads=data.frame(
  stringsAsFactors = FALSE,
            osm_id = c(779434L, 779437L),
              name = c("Ruskin Avenue", "Barnes Avenue"),
          geometry = c("c(-1.5178758, -1.5171935, -1.5170291, -1.5168588, -1.5163581, -1.5158187, -1.5156387, -1.5154735, -1.5153933, -1.5146483, -1.5132778, -1.5126556, 53.7011323, 53.7013923, 53.7014655, 53.701575, 53.7020413, 53.7025049, 53.7026595, 53.7027965, 53.7028383, 53.7032271, 53.7039413, 53.7042654)",
                       "c(-1.5132778, -1.5113646, 53.7039413, 53.7031114)")
)


The full data on roads and routes (above datasets include only few rows and cols) can be obtained using:

library(stplanr)

od_data <- stplanr::flow[1:20, ]
desire_lines_ <- od2line(flow = od_data, zones = cents_sf)


routes= route(l = desire_lines_, route_fun = route_osrm)


library(osmextract)
library(osmdata)


chorlton_point = tmaptools::geocode_OSM("bristol")
c_m_coordiantes = rbind(chorlton_point$coords, chorlton_point$coords)
c_m_od = od::points_to_od(p = c_m_coordiantes, interzone_only = TRUE)
c_m_desire_line = od::odc_to_sf(c_m_od[-(1:2)])[1, ]
chorlton_buffer = stplanr::geo_buffer(c_m_desire_line, dist = 1000)
qtm(chorlton_buffer)


iow_sf_polygon <- getbb("bristol", format_out = "sf_polygon")

roads = oe_get(chorlton_buffer, stringsAsFactors = FALSE, quiet = TRUE)


Upvotes: 0

Views: 63

Answers (1)

ktiu
ktiu

Reputation: 2626

This seems to work well with the "full data" you provided:

st_join(routes, roads)

Upvotes: 1

Related Questions