Osm

x <- c("osmdata","ggplot2","osrm","sf")
lapply(x, library, character.only = TRUE) # carga los paquetes requeridos

Región

bbx <- getbb("Torreón Coahuila")

#ylim=c(25.47, 25.67)
#xlim=c(-103.30, -103.52)


min_lon <- -103.52; max_lon <- -103.30
min_lat <- 25.47; max_lat <- 25.67
bbx <- rbind(x=c(min_lon,max_lon),y=c(min_lat,max_lat))
colnames(bbx) <- c("min","max")

highways <- bbx %>%
  opq()%>%
  add_osm_feature(key = "highway", 
                  value=c("motorway", "trunk",
                          "primary","secondary", 
                          "tertiary","motorway_link",
                          "trunk_link","primary_link",
                          "secondary_link",
                          "tertiary_link")) %>%
  osmdata_sf()

require(sf)
ggplot() +
  geom_sf(data = highways$osm_lines,
          aes(color=highway),
          size = .4,
          alpha = .65)+
  theme_void()

streets <- bbx %>%
  opq()%>%
  add_osm_feature(key = "highway", 
                  value = c("residential", "living_street",
                            "service","unclassified",
                            "pedestrian", "footway",
                            "track","path")) %>%
  osmdata_sf()

ggplot() +
  geom_sf(data = streets$osm_lines,
          aes(color=highway),
          size = .4,
          alpha = .65)+
  theme_void()

p1 <- c(lon=-103.4170318,lat=25.5248350)
    p2 <- c(on=-103.4001839,lat=25.6104486)
    oo <- osrmRoute(src=p1,dst=p2,returnclass = "sf",
                    overview="full")
    
 
color_roads <- rgb(0.42,0.449,0.488)
ggplot() +
  geom_sf(data = streets$osm_lines,
          col = color_roads,
          size = .4,
          alpha =.65) +
  geom_sf(data = highways$osm_lines,
          col = color_roads,
          size = .6,
          alpha = .8)+
  geom_sf(data=st_geometry(oo),
          inherit.aes = FALSE,col="red",alpha=0.5)+
    theme(legend.position = F) + theme_void()

#ggsave("osm_map.png", width = 1300, height = 1300, units = "mm", dpi = "retina",limitsize = FALSE)
#punto 1 -103.4170318, 25.5248350
#punto 2 -103.4001839, 25.6104486

ggsave("miruta.svg", scale=1, width = 10, height = 10, units = "in", dpi = 200,limitsize = FALSE)