OPEN STREET MAP

CONSIGNA:

  1. Descargar de OpenStreetMap la grilla de calles para la Ciudad elegida en el ejercicio 1 y mapearla por uno de sus atributos (velocidad mínima, velocidad máxima, cantidad de carriles, etc).

  2. Descargar de OpenStreetMap una (o más) capas de datos de tipo puntos o polígonos. (Ver catálogo de categorías en https://wiki.openstreetmap.org/wiki/Map_Features)

    1. Proyectar los datos descargados en un mapa y comentar los resultados: ¿Cómo se distribuyen en la Ciudad?
library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.4     v dplyr   1.0.7
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   2.0.1     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(osmdata)
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright

PARA EMPEZAR: traemos el MAPA del lugar que queremos:

bbox_caba <- getbb("Ciudad Autónoma de Buenos Aires, Argentina")
bbox_caba
##         min       max
## x -58.53145 -58.33514
## y -34.70564 -34.52655
mapa_caba <- get_stamenmap(bbox=bbox_caba,
                          zoom=13)
## Source : http://tile.stamen.com/terrain/13/2764/4934.png
## Source : http://tile.stamen.com/terrain/13/2765/4934.png
## Source : http://tile.stamen.com/terrain/13/2766/4934.png
## Source : http://tile.stamen.com/terrain/13/2767/4934.png
## Source : http://tile.stamen.com/terrain/13/2768/4934.png
## Source : http://tile.stamen.com/terrain/13/2764/4935.png
## Source : http://tile.stamen.com/terrain/13/2765/4935.png
## Source : http://tile.stamen.com/terrain/13/2766/4935.png
## Source : http://tile.stamen.com/terrain/13/2767/4935.png
## Source : http://tile.stamen.com/terrain/13/2768/4935.png
## Source : http://tile.stamen.com/terrain/13/2764/4936.png
## Source : http://tile.stamen.com/terrain/13/2765/4936.png
## Source : http://tile.stamen.com/terrain/13/2766/4936.png
## Source : http://tile.stamen.com/terrain/13/2767/4936.png
## Source : http://tile.stamen.com/terrain/13/2768/4936.png
## Source : http://tile.stamen.com/terrain/13/2764/4937.png
## Source : http://tile.stamen.com/terrain/13/2765/4937.png
## Source : http://tile.stamen.com/terrain/13/2766/4937.png
## Source : http://tile.stamen.com/terrain/13/2767/4937.png
## Source : http://tile.stamen.com/terrain/13/2768/4937.png
## Source : http://tile.stamen.com/terrain/13/2764/4938.png
## Source : http://tile.stamen.com/terrain/13/2765/4938.png
## Source : http://tile.stamen.com/terrain/13/2766/4938.png
## Source : http://tile.stamen.com/terrain/13/2767/4938.png
## Source : http://tile.stamen.com/terrain/13/2768/4938.png
## Source : http://tile.stamen.com/terrain/13/2764/4939.png
## Source : http://tile.stamen.com/terrain/13/2765/4939.png
## Source : http://tile.stamen.com/terrain/13/2766/4939.png
## Source : http://tile.stamen.com/terrain/13/2767/4939.png
## Source : http://tile.stamen.com/terrain/13/2768/4939.png
ggmap(mapa_caba)

para obtener el poligono de la CABA:

caba_polygon <- getbb("Ciudad Autónoma de Buenos Aires, Argentina", format_out = "sf_polygon")
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()
## old-style crs object detected; please recreate object with a recent sf::st_crs()

Ahora para verlo en el mapa:

class(caba_polygon)
## [1] "list"
geometria_caba <- caba_polygon[[2]]
class(geometria_caba)
## [1] "sf"         "data.frame"

Dejamos el mapa de fondo con un poligono pegado arriba:

ggmap(mapa_caba)+
  geom_sf(data=geometria_caba, fill=NA, size=1, color="firebrick3", inherit.aes = FALSE)+
  labs(title="CIUDAD AUTÓNOMA DE BUENOS AIRES",
       caption="Fuente: Open Street Map")+
  theme_void()
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

Descargando GRILLA DE CALLES (roads):

PASO 1: Hacer la consulta

caba_calles <- opq(bbox_caba) %>%
  add_osm_feature(key="highway", value = "primary")

PASO 2: efectivamente traerme los datos

caba_calles <- osmdata_sf(caba_calles)

PASO 3: para mirar la lista de elementos que tiene

caba_calles
## Object of class 'osmdata' with:
##                  $bbox : -34.705637,-58.5314494,-34.5265535,-58.3351423
##         $overpass_call : The call submitted to the overpass API
##                  $meta : metadata including timestamp and version numbers
##            $osm_points : 'sf' Simple Features Collection with 3568 points
##             $osm_lines : 'sf' Simple Features Collection with 1074 linestrings
##          $osm_polygons : 'sf' Simple Features Collection with 0 polygons
##        $osm_multilines : NULL
##     $osm_multipolygons : NULL

En estos datos encontramos: 3568 puntos, 1074 lineas y 0 polygons.

PASO 4: ahora quiero quedarme solo con las líneas que demarcan la traza de las calles primarias.

caba_calles <- caba_calles$osm_lines
head(caba_calles)
## Simple feature collection with 6 features and 73 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: -58.51848 ymin: -34.64659 xmax: -58.37345 ymax: -34.57415
## Geodetic CRS:  WGS 84
##            osm_id                   name access access.lanes alt_name
## 10055451 10055451    Avenida San Martín   <NA>         <NA>     <NA>
## 10616672 10616672       Avenida Córdoba   <NA>         <NA>     <NA>
## 17441481 17441481 Avenida Del Libertador   <NA>         <NA>     <NA>
## 17442200 17442200        Avenida de Mayo   <NA>         <NA>     <NA>
## 17448289 17448289 Avenida Del Libertador   <NA>         <NA>     <NA>
## 18793046 18793046     Avenida Directorio   <NA>         <NA>     <NA>
##             bicycle bridge bus.lanes busway.right covered cutting cycleway.left
## 10055451       <NA>   <NA>      <NA>         <NA>    <NA>    <NA>          <NA>
## 10616672 designated   <NA>      <NA>         <NA>    <NA>    <NA>         track
## 17441481       <NA>   <NA>      <NA>         <NA>    <NA>    <NA>          <NA>
## 17442200       <NA>   <NA>      <NA>         <NA>    <NA>    <NA>          <NA>
## 17448289       <NA>   <NA>      <NA>         <NA>    <NA>    <NA>          <NA>
## 18793046       <NA>   <NA>      <NA>         <NA>    <NA>    <NA>          <NA>
##          cycleway.right destination destination.ref embankment embedded_rails
## 10055451           <NA>        <NA>            <NA>       <NA>           <NA>
## 10616672           <NA>        <NA>            <NA>       <NA>           <NA>
## 17441481           <NA>        <NA>            <NA>       <NA>           <NA>
## 17442200           <NA>        <NA>            <NA>       <NA>           <NA>
## 17448289           <NA>        <NA>            <NA>       <NA>           <NA>
## 18793046           <NA>        <NA>            <NA>       <NA>           <NA>
##          foot hazard  hgv hgv.lanes highway horse junction lane_markings lanes
## 10055451 <NA>   <NA> <NA>      <NA> primary  <NA>     <NA>          <NA>     3
## 10616672 <NA>   <NA> <NA>      <NA> primary  <NA>     <NA>          <NA>     4
## 17441481 <NA>   <NA> <NA>      <NA> primary  <NA>     <NA>          <NA>    12
## 17442200 <NA>   <NA> <NA>      <NA> primary  <NA>     <NA>          <NA>  <NA>
## 17448289 <NA>   <NA> <NA>      <NA> primary  <NA>     <NA>          <NA>     4
## 18793046 <NA>   <NA> <NA>      <NA> primary  <NA>     <NA>            no  <NA>
##          lanes.backward lanes.both_ways lanes.bus lanes.forward lanes.psv layer
## 10055451           <NA>            <NA>      <NA>          <NA>      <NA>  <NA>
## 10616672           <NA>            <NA>      <NA>          <NA>      <NA>  <NA>
## 17441481           <NA>               3      <NA>             9      <NA>  <NA>
## 17442200           <NA>            <NA>      <NA>          <NA>      <NA>  <NA>
## 17448289           <NA>            <NA>      <NA>          <NA>      <NA>  <NA>
## 18793046           <NA>            <NA>      <NA>          <NA>      <NA>  <NA>
##           lit maxheight maxspeed maxspeed.conditional maxspeed.type maxweight
## 10055451 <NA>      <NA>       60                 <NA>          <NA>      <NA>
## 10616672 <NA>      <NA>       60                 <NA>          <NA>      <NA>
## 17441481  yes      <NA>       70                 <NA>          <NA>      <NA>
## 17442200 <NA>      <NA>       60                 <NA>          <NA>      <NA>
## 17448289 <NA>      <NA>       60                 <NA>          <NA>      <NA>
## 18793046 <NA>      <NA>       60                 <NA>          <NA>      <NA>
##          name.etymology noname old_name old_ref oneway oneway.bicycle
## 10055451           <NA>   <NA>     <NA>    <NA>    yes           <NA>
## 10616672           <NA>   <NA>     <NA>    <NA>    yes            yes
## 17441481           <NA>   <NA>     <NA>    <NA>   <NA>           <NA>
## 17442200           <NA>   <NA>     <NA>    <NA>    yes           <NA>
## 17448289           <NA>   <NA>     <NA>    <NA>    yes           <NA>
## 18793046           <NA>   <NA>     <NA>    <NA>    yes           <NA>
##                                     oneway.conditional overtaking
## 10055451                                          <NA>       <NA>
## 10616672                                          <NA>       <NA>
## 17441481 no @ (Mo-Fr 15:00-22:00;Sa-Su,PH 08:00-13:00)       <NA>
## 17442200                                          <NA>       <NA>
## 17448289                                          <NA>       <NA>
## 18793046                                          <NA>       <NA>
##          parking.condition.left parking.condition.left.time_interval
## 10055451                   <NA>                                 <NA>
## 10616672                   <NA>                                 <NA>
## 17441481                   <NA>                                 <NA>
## 17442200                   <NA>                                 <NA>
## 17448289                   <NA>                                 <NA>
## 18793046                   <NA>                                 <NA>
##          parking.condition.right parking.condition.right.time_interval
## 10055451                    <NA>                                  <NA>
## 10616672                    <NA>                                  <NA>
## 17441481                    <NA>                                  <NA>
## 17442200                    <NA>                                  <NA>
## 17448289                    <NA>                                  <NA>
## 18793046                    <NA>                                  <NA>
##          parking.lane.left parking.lane.left.parallel parking.lane.right
## 10055451              <NA>                       <NA>               <NA>
## 10616672              <NA>                       <NA>               <NA>
## 17441481              <NA>                       <NA>               <NA>
## 17442200              <NA>                       <NA>               <NA>
## 17448289              <NA>                       <NA>               <NA>
## 18793046              <NA>                       <NA>               <NA>
##          parking.lane.right.parallel psv.lanes  ref short_name sidewalk
## 10055451                        <NA>      <NA> <NA>       <NA>     <NA>
## 10616672                        <NA>      <NA> <NA>       <NA>     <NA>
## 17441481                        <NA>      <NA> <NA>       <NA>     both
## 17442200                        <NA>      <NA> <NA>       <NA>     <NA>
## 17448289                        <NA>      <NA> <NA>       <NA>     <NA>
## 18793046                        <NA>      <NA> <NA>       <NA>     <NA>
##          smoothness            sorting_name source source.maxspeed surface
## 10055451       <NA>                    <NA>   <NA>            <NA> asphalt
## 10616672       <NA>                    <NA>   <NA>            <NA> asphalt
## 17441481       <NA> Del Libertador, Avenida   <NA>            <NA> asphalt
## 17442200       <NA>                    <NA>   <NA>            <NA> asphalt
## 17448289       <NA> Del Libertador, Avenida   <NA>            <NA> asphalt
## 18793046       <NA>                    <NA>   <NA>            <NA> asphalt
##          surface.lanes traffic_calming tunnel turn.lanes turn.lanes.backward
## 10055451          <NA>            <NA>   <NA>       <NA>                <NA>
## 10616672          <NA>            <NA>   <NA>       <NA>                <NA>
## 17441481          <NA>            <NA>   <NA>       <NA>                <NA>
## 17442200          <NA>            <NA>   <NA>       <NA>                <NA>
## 17448289          <NA>            <NA>   <NA>       <NA>                <NA>
## 18793046          <NA>            <NA>   <NA>       <NA>                <NA>
##          turn.lanes.forward vehicle.lanes width wikidata wikipedia
## 10055451               <NA>          <NA>  <NA>     <NA>      <NA>
## 10616672               <NA>          <NA>  <NA>     <NA>      <NA>
## 17441481               <NA>          <NA>  <NA>     <NA>      <NA>
## 17442200               <NA>          <NA>  <NA>     <NA>      <NA>
## 17448289               <NA>          <NA>  <NA>     <NA>      <NA>
## 18793046               <NA>          <NA>  <NA>     <NA>      <NA>
##                                geometry
## 10055451 LINESTRING (-58.51848 -34.5...
## 10616672 LINESTRING (-58.44479 -34.5...
## 17441481 LINESTRING (-58.41093 -34.5...
## 17442200 LINESTRING (-58.37345 -34.6...
## 17448289 LINESTRING (-58.4168 -34.57...
## 18793046 LINESTRING (-58.48607 -34.6...
class(caba_calles)
## [1] "sf"         "data.frame"
dim(caba_calles)
## [1] 1074   74

Vemos que tiene 1074 observaciones y 74 variables.

PAso 5: mapear

ggmap(mapa_caba)+
  geom_sf(data=geometria_caba, fill= NA, size=1, color="blue", inherit.aes = FALSE)+
  geom_sf(data=caba_calles, color="red", alpha=2, inherit.aes = FALSE)+
  labs(title= "Calles Principales de CABA", caption= "Fuente: Open Street Map")
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

Las calles se descargando segun los límites del bbox, pero no de los limites geográficos de CABA. Hacemos una intersección entre ambos datos geográficos:

caba_calles <- st_intersection(caba_calles, geometria_caba)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
ggmap(mapa_caba)+
  geom_sf(data=geometria_caba, fill= NA, size=1, color="blue", inherit.aes = FALSE)+
  geom_sf(data=caba_calles, color="red", alpha=2, inherit.aes = FALSE)+
  labs(title= "Calles Principales de CABA", caption= "Fuente: Open Street Map")+
  theme_void()
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

FILTRAMOS A LA AVENIDA DEL LIBERTADOR:

ggmap(mapa_caba)+
  geom_sf(data=geometria_caba, fill= NA, size=1, color="blue", inherit.aes = FALSE)+
  geom_sf(data=filter(caba_calles, str_detect(name, "Avenida Del Libertador")), color="darkgreen", size=1.8, inherit.aes = FALSE)+
  labs(title= "Avenida del Libertador", subtitle= "CABA", caption= "Fuente: Open Street Map")+
  theme_void()
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

Pasamos del formato texto a SF el atributo de MAXIMA VELOCIDAD y LANES (carriles), para poder usarlos como numéricos:

caba_calles <- caba_calles %>% 
  mutate(maxspeed = as.numeric(maxspeed),
         lanes = ifelse(is.na(lanes), 1, as.numeric(lanes)))
ggplot(caba_calles) +
    geom_sf(aes(color = maxspeed), alpha = 0.8) +
    scale_color_viridis_c() +
    labs(title = "Avenidas de CABA", subtitle = "máximas velocidades",
         caption = "fuente: Open Stree tMap",
         color = "velocidad máxima")

Y lo vemos en el MAPA, todo junto:

ggmap(mapa_caba)+
    geom_sf(data = caba_calles, aes(color=maxspeed), size=2, inherit.aes = FALSE)+
    scale_color_viridis_c()+
  labs(title="Avenidas de CABA", subtitle = "Máximas Velocidades",
       caption="Fuente: Open Street Map", color="Máxima velicidad")+
  theme_classic()
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

A partir de este mapa, podemos observar no solo dónde se ubican las avenidas(calles principales) de CABA, si no cúal es su velocidad máxima correspondiente. Se observa que en promedio la mayoría de las avenidas tiene una velocidad máxima que ronda los 60 km/h (de un color verde claro, segundo en la escala de referencia). Llama la arención tambien el tramo de la Avenida Del Libertadir que presenta una máxima velocidad de 70 km/h, segun donde está ubicada en la ciudad. Al mismo tiempo es interesante ver, que existen avenidas con velocidades mde 50 o 40 km/h.

Por ultimo vamos a descargar puntos asociados a estos data sets:

DESCARGA DE PUNTOS

dentro de “Highway” –> bus_stop (paradas de colectivos)

PASO 1:

caba_paradas <- opq(bbox = bbox_caba) %>%
  add_osm_feature(key = "highway", value = "bus_stop")

PASO2:

caba_paradas <- osmdata_sf(caba_paradas)

PASO 3: MIRO

caba_paradas
## Object of class 'osmdata' with:
##                  $bbox : -34.705637,-58.5314494,-34.5265535,-58.3351423
##         $overpass_call : The call submitted to the overpass API
##                  $meta : metadata including timestamp and version numbers
##            $osm_points : 'sf' Simple Features Collection with 5954 points
##             $osm_lines : NULL
##          $osm_polygons : 'sf' Simple Features Collection with 0 polygons
##        $osm_multilines : NULL
##     $osm_multipolygons : NULL

Vemos que tiene 5954 puntos, asociados a las paradas de colectivos, con 42 variables.

PASO 4:

caba_paradas <- caba_paradas$osm_points

Paso 5: MAPEAR

ggmap(mapa_caba)+
  geom_sf(data=geometria_caba, fill= NA, size=1, color="blue", inherit.aes = FALSE)+
  geom_sf(data=caba_paradas, aes(color=highway), color="purple", inherit.aes=FALSE)+
  labs(title = "Paradas de Colectivos", subtitle = "CABA", caption = "FUENTE: Open Street Map")
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

Hacemos interseccion para observar solo las paradas dentro de los limites de CABA:

caba_paradas <- st_intersection(caba_paradas, geometria_caba)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
ggmap(mapa_caba)+
  geom_sf(data=geometria_caba, fill= NA, size=1.5, color="blue", inherit.aes = FALSE)+
  geom_sf(data=caba_paradas, aes(color=highway), color="purple", size=0.5, inherit.aes=FALSE)+
  labs(title = "Paradas de Colectivos", subtitle = "CABA", caption = "FUENTE: Open Street Map")+
  theme_void()
## Coordinate system already present. Adding new coordinate system, which will replace the existing one.

A partir de este mapa podemos observar dónde se encuentra la mayor cantidad de paradas de colecivos dentro de los limites de CABA, y cómo estan ubicadas.

FIN!