TP2

I. 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).

Antes de hacer el ejercicio, cargamos las librerías necesarias para trabajar con los datos de nuestro interés.

library(tidyverse)
library(sf)
library(osmdata)
library(leaflet)
library(ggsflabel)

Traemos la caja de coordenadas de la ciudad elegida.

bbox <- getbb("Bilbao, España")

bbox
##         min       max
## x -2.985981 -2.880264
## y 43.213718 43.290132

Traemos el contorno de la ciudad de OSM.

bbox_poly <- getbb("Bilbao, España", format_out = "sf_polygon")
leaflet(bbox_poly) %>%
    addTiles() %>% 
    addPolygons()

Traemos las calles de la ciudad de OSM.

bilbao <- opq(bbox) %>% 
    add_osm_feature(key = "highway")
bilbao <- bilbao %>% 
    osmdata_sf()
calles <- bilbao$osm_lines

Visualizamos los resultados.

ggplot() +
    geom_sf(data = calles) + 
    theme_light()

Realizamos la intersección/recorte con el polígono de límites.

calles <- st_intersection(calles, bbox_poly)
ggplot() +
    geom_sf(data = calles) + 
    theme_light()

Con la información obtenida realizamos un mapa con las velocidades maximas de la ciudad.

calles <- calles %>% 
  mutate(maxspeed = as.numeric(maxspeed),
         lanes = ifelse(is.na(lanes), 1, as.numeric(lanes)))
ggplot(calles) +
    geom_sf(aes(color = maxspeed), alpha = 1) +
    scale_color_viridis_c() +
    theme_void() +
    labs(title = "Bilbao",
         subtitle = "Vías de circulación",
         caption = "fuente: OpenStreetMap",
         color = "velocidad máxima")

Con la información obtenida realizamos un mapa con las avenidas de la ciudad.

ggplot() +
    geom_sf(data = calles,color = "gray40", alpha = 1) +
    geom_sf(data = filter(calles, str_detect(name, "Avenida")),color = "red") +
    theme_void() +
    labs(title = "Bilbao",
         subtitle = "Avenidas",
         caption = "fuente: OpenStreetMap")

II. 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)

a. Proyectar los datos descargados en un mapa y comentar los resultados: ¿Cómo se distribuyen en la Ciudad?

Descargamos de OSM la ubicación de hostels en la ciudad de Bilbao.

Bilbao_hostels <- opq(bbox) %>% 
  add_osm_feature(key = "tourism", value = "hostel") %>% 
  osmdata_sf() 
Bilbao_hostels
Bilbao_hostels2 <- st_intersection(Bilbao_hostels$osm_points, bbox_poly)

Eliminamos con la función UNIQUE los registros duplicados en el total de las variables.

Bilbao_hostels_U <- unique(Bilbao_hostels2)
Bilbao_hostels_U$name
##  [1] NA                                            
##  [2] NA                                            
##  [3] NA                                            
##  [4] NA                                            
##  [5] NA                                            
##  [6] NA                                            
##  [7] NA                                            
##  [8] NA                                            
##  [9] NA                                            
## [10] NA                                            
## [11] "Akelarre"                                    
## [12] NA                                            
## [13] NA                                            
## [14] NA                                            
## [15] NA                                            
## [16] NA                                            
## [17] NA                                            
## [18] NA                                            
## [19] "Pension Mendez"                              
## [20] NA                                            
## [21] NA                                            
## [22] NA                                            
## [23] NA                                            
## [24] NA                                            
## [25] NA                                            
## [26] "Albergue Claret Enea Aterpea"                
## [27] NA                                            
## [28] "Poshtel Bilbao"                              
## [29] NA                                            
## [30] NA                                            
## [31] NA                                            
## [32] "Hostal Central"                              
## [33] NA                                            
## [34] NA                                            
## [35] NA                                            
## [36] NA                                            
## [37] "Albergue de Peregrinos Santa Cruz de Begoña"
## [38] "Hostel Optimi Rooms"                         
## [39] NA                                            
## [40] NA                                            
## [41] NA                                            
## [42] NA                                            
## [43] NA                                            
## [44] NA                                            
## [45] NA                                            
## [46] NA                                            
## [47] NA                                            
## [48] NA                                            
## [49] NA                                            
## [50] NA                                            
## [51] NA                                            
## [52] NA                                            
## [53] "All Iron"

Mapeamos los hostels en la ciudad de Bilbao.

ggplot() +
  geom_sf(data = calles, color = "grey") +
  geom_sf(data = Bilbao_hostels_U, aes(color = internet_access),  size = 4) +
  geom_sf_label_repel(data = Bilbao_hostels_U, aes(label = name), size = 4) +
  theme_void() +
  labs(title = "Bilbao",
       subtitle = "Hostels",
       caption = "Fuente: OpenStreetMap",
       color = "Acceso internet")

b. Hacer un conteo de los ítems de la capa descargada por barrio, mapearlo y compararlo con el conteo de los ítems descargados en el ejercicio anterior: ¿La distribución es similar o hay diferencias? ¿A qué se puede deber?

Importamos el geojson con los barrios de Bilbao.

barrios <- st_read('distritos-bilbao_wgs84.geojson') %>% rename(barrios_bilbao=BAR_DS_O) %>% mutate(barrios_bilbao=str_replace_na(barrios_bilbao, "Deusto"))
## Reading layer `distritos-bilbao_wgs84' from data source `C:\Users\jabib\Desktop\TP\distritos-bilbao_wgs84.geojson' using driver `GeoJSON'
## Simple feature collection with 8 features and 10 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -2.985978 ymin: 43.21371 xmax: -2.880261 ymax: 43.29013
## CRS:            4326
barrios
## Simple feature collection with 8 features and 10 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -2.985978 ymin: 43.21371 xmax: -2.880261 ymax: 43.29013
## CRS:            4326
##           BAR_CL BAR_BARR              barrios_bilbao BAR_MUNI BAR_MUNI_D
## 1 44802000000200      702                  Iralabarri      020     Bilbao
## 2 44802000000200      301                  Otxarkoaga      020     Bilbao
## 3 44802000000400      204                    Uribarri      020     Bilbao
## 4 44802000000100      403                     Bolueta      020     Bilbao
## 5 44802000000100      506             Bilbao la Vieja      020     Bilbao
## 6 44802000000000      601                      Abando      020     Bilbao
## 7 44802000000500      806 Diseminado Basurtu-Zorrotza      020     Bilbao
## 8             NA     <NA>                      Deusto     <NA>       <NA>
##   BAR_PROV BAR_PROV_D Shape_Leng Shape_Area distrito
## 1       48    Bizkaia   3013.924   382538.0        7
## 2       48    Bizkaia   4073.511   584765.0        3
## 3       48    Bizkaia   2944.946   323226.1        2
## 4       48    Bizkaia   7268.457   768816.2        4
## 5       48    Bizkaia   2206.221   134503.2        5
## 6       48    Bizkaia   4972.360  1174614.1        6
## 7       48    Bizkaia  13613.681  4162459.1        8
## 8     <NA>       <NA>   6911.348   847718.6        1
##                         geometry
## 1 MULTIPOLYGON (((-2.934927 4...
## 2 MULTIPOLYGON (((-2.896959 4...
## 3 MULTIPOLYGON (((-2.922761 4...
## 4 MULTIPOLYGON (((-2.905864 4...
## 5 MULTIPOLYGON (((-2.921609 4...
## 6 MULTIPOLYGON (((-2.940626 4...
## 7 MULTIPOLYGON (((-2.950495 4...
## 8 MULTIPOLYGON (((-2.967939 4...
summary(barrios)
##      BAR_CL                  BAR_BARR barrios_bilbao     BAR_MUNI  BAR_MUNI_D
##  Min.   :44802000000000   204    :1   Length:8           020 :7   Bilbao:7   
##  1st Qu.:44802000000100   301    :1   Class :character   NA's:1   NA's  :1   
##  Median :44802000000200   403    :1   Mode  :character                       
##  Mean   :44802000000200   506    :1                                          
##  3rd Qu.:44802000000300   601    :1                                          
##  Max.   :44802000000500   (Other):2                                          
##  NA's   :1                NA's   :1                                          
##  BAR_PROV   BAR_PROV_D   Shape_Leng      Shape_Area         distrito   
##  48  :7   Bizkaia:7    Min.   : 2206   Min.   : 134503   Min.   :1.00  
##  NA's:1   NA's   :1    1st Qu.: 2997   1st Qu.: 367710   1st Qu.:2.75  
##                        Median : 4523   Median : 676791   Median :4.50  
##                        Mean   : 5626   Mean   :1047330   Mean   :4.50  
##                        3rd Qu.: 7001   3rd Qu.: 929442   3rd Qu.:6.25  
##                        Max.   :13614   Max.   :4162459   Max.   :8.00  
##                                                                        
##           geometry
##  MULTIPOLYGON :8  
##  epsg:4326    :0  
##  +proj=long...:0  
##                   
##                   
##                   
## 

Cargamos también el Ría de Bilbao en proyecciones EPSG:3042 y las transformamos a EPSG:4326

ria <- st_read('ria.geojson')
## Reading layer `ria' from data source `C:\Users\jabib\Desktop\TP\ria.geojson' using driver `GeoJSON'
## replacing null geometries with empty geometries
## Simple feature collection with 4 features and 1 field (with 2 geometries empty)
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 501820.3 ymin: 4788047 xmax: 506458.3 ymax: 4793100
## CRS:            3042
ria_mercator <- st_transform(ria, crs = 4326)

ria_mercator
## Simple feature collection with 4 features and 1 field (with 2 geometries empty)
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: -2.977567 ymin: 43.2452 xmax: -2.920445 ymax: 43.29072
## CRS:            EPSG:4326
##     id                       geometry
## 1 <NA>                  POLYGON EMPTY
## 2 <NA>                  POLYGON EMPTY
## 3 <NA> POLYGON ((-2.965035 43.2615...
## 4 <NA> POLYGON ((-2.969032 43.2880...

Mapeamos todos los shapes que tenemos hasta el momento.

ggplot() +
    geom_sf(data = barrios) +
    geom_sf(data = calles, color = "grey") +
    geom_sf(data = ria_mercator , color='blue') +
    geom_sf(data = Bilbao_hostels_U, shape = 21, fill = "red", size = 5) +
    labs(title = "Mapa de hostels en la ciudad de Bilbao", caption = "Fuente: OpenStreetMap", y="Latitud", x="Longitud") + 
    theme_light()

hostels_por_barrios <- st_join(Bilbao_hostels_U, barrios) 
names(hostels_por_barrios)
##  [1] "osm_id"              "name"                "addr.city"          
##  [4] "addr.district"       "addr.housenumber"    "addr.postcode"      
##  [7] "addr.street"         "addr.suburb"         "air_conditioning"   
## [10] "beds"                "designation"         "email"              
## [13] "guest_house"         "internet_access"     "internet_access.fee"
## [16] "opening_hours"       "operator"            "phone"              
## [19] "rooms"               "source"              "source.url"         
## [22] "tourism"             "website"             "wheelchair"         
## [25] "BAR_CL"              "BAR_BARR"            "barrios_bilbao"     
## [28] "BAR_MUNI"            "BAR_MUNI_D"          "BAR_PROV"           
## [31] "BAR_PROV_D"          "Shape_Leng"          "Shape_Area"         
## [34] "distrito"            "geometry"

Sacamos la cantidad de hostels que tenemos por barrios.

barrios_bilbao_cantidad <- hostels_por_barrios %>% 
    group_by(barrios_bilbao) %>% 
    summarise(cantidad = n())

barrios_bilbao_cantidad
## Simple feature collection with 6 features and 2 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: -2.963484 ymin: 43.24647 xmax: -2.909634 ymax: 43.26877
## CRS:            EPSG:4326
## # A tibble: 6 x 3
##   barrios_bilbao       cantidad                                         geometry
## * <chr>                   <int>                                   <GEOMETRY [°]>
## 1 Abando                      3 MULTIPOINT ((-2.943954 43.25818), (-2.93537 43.~
## 2 Bilbao la Vieja            22 MULTIPOINT ((-2.928026 43.25579), (-2.925355 43~
## 3 Bolueta                    14 MULTIPOINT ((-2.910163 43.24669), (-2.910155 43~
## 4 Deusto                      1                       POINT (-2.954546 43.26877)
## 5 Diseminado Basurtu-~       12 MULTIPOINT ((-2.963484 43.26073), (-2.963405 43~
## 6 Uribarri                    1                       POINT (-2.912549 43.25955)

Importamos los hitos turísticos del TP anterior y transformamos sus proyecciones a mercator.

turistico <- st_read('turistico_con_barrios.geojson')
## Reading layer `turistico_con_barrios' from data source `C:\Users\jabib\Desktop\TP\turistico_con_barrios.geojson' using driver `GeoJSON'
## Simple feature collection with 266 features and 21 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 503856.3 ymin: 4788591 xmax: 507093.7 ymax: 4791059
## CRS:            3042
turistico_mercator <- st_transform(turistico, crs = 4326)

turistico_mercator
## Simple feature collection with 266 features and 21 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: -2.952485 ymin: 43.25009 xmax: -2.912599 ymax: 43.27233
## CRS:            EPSG:4326
## First 10 features:
##            NOMBRE_FAMILIA              NOMBRE_LUGAR_CAS
## 1  Edificios emblemáticos                 LA EQUITATIVA
## 2  Edificios emblemáticos              PALACIO IBAIGANE
## 3  Edificios emblemáticos             INSTITUTO UNAMUNO
## 4  Edificios emblemáticos              EDIFICIO CORREOS
## 5      Nueva arquitectura      GRAN HOTEL DOMINE BILBAO
## 6      Nueva arquitectura DEPARTAMENTO VASCO DE SANIDAD
## 7           Museos Bilbao       MUSEO GUGGENHEIM BILBAO
## 8      Arte al aire libre                   DODECLATHOS
## 9      Arte al aire libre                    TERPSÍCORE
## 10     Arte al aire libre                         PUPPY
##                               NOMBRE_LUGAR_EUS COD_CALLE NOMBRE_CALLE
## 1                                LA EQUITATIVA      6090    MAZARREDO
## 2                            IBAIGANE JAUREGIA      6090    MAZARREDO
## 3  UNAMUNO INSTITUTUA ETA UNIBERTSITATE ESKOLA      9340      URQUIJO
## 4                               POSTA ERAIKINA      9340      URQUIJO
## 5                         BILBAO DOMINE HOTELA      6090    MAZARREDO
## 6                       OSASUN SAILEKO EGOITZA      7770      RECALDE
## 7                     GUGGENHEIM BILBAO MUSEOA        15 ABANDOIBARRA
## 8                                  DODECLATHOS        15 ABANDOIBARRA
## 9                                   TERPSÍCORE        15 ABANDOIBARRA
## 10                                       PUPPY        15 ABANDOIBARRA
##    COD_TIPO_VIA NOMBRE_TIPO_VIA BLOQUE NUMERO BIS    X         BAR_CL BAR_BARR
## 1            AL         ALAMEDA   <NA>      7     <NA> 44802000000000      601
## 2            AL         ALAMEDA   <NA>     23     <NA> 44802000000400      204
## 3            AL         ALAMEDA   <NA>     10     <NA> 44802000000000      601
## 4            AL         ALAMEDA   <NA>     19     <NA> 44802000000000      601
## 5            AL         ALAMEDA   <NA>     61     <NA> 44802000000000      601
## 6            AL         ALAMEDA   <NA>     39     <NA> 44802000000000      601
## 7            AV         AVENIDA   <NA>      2     <NA> 44802000000400      204
## 8            AV         AVENIDA   <NA>      4     <NA>             NA     <NA>
## 9            AV         AVENIDA   <NA>      4     <NA>             NA     <NA>
## 10           AV         AVENIDA   <NA>      2     <NA> 44802000000400      204
##    barrios_bilbao BAR_MUNI BAR_MUNI_D BAR_PROV BAR_PROV_D Shape_Leng Shape_Area
## 1          Abando      020     Bilbao       48    Bizkaia   4972.360  1174614.1
## 2        Uribarri      020     Bilbao       48    Bizkaia   2944.946   323226.1
## 3          Abando      020     Bilbao       48    Bizkaia   4972.360  1174614.1
## 4          Abando      020     Bilbao       48    Bizkaia   4972.360  1174614.1
## 5          Abando      020     Bilbao       48    Bizkaia   4972.360  1174614.1
## 6          Abando      020     Bilbao       48    Bizkaia   4972.360  1174614.1
## 7        Uribarri      020     Bilbao       48    Bizkaia   2944.946   323226.1
## 8          Deusto     <NA>       <NA>     <NA>       <NA>   6911.348   847718.6
## 9          Deusto     <NA>       <NA>     <NA>       <NA>   6911.348   847718.6
## 10       Uribarri      020     Bilbao       48    Bizkaia   2944.946   323226.1
##    distrito                   geometry
## 1         6 POINT (-2.927917 43.26493)
## 2         2  POINT (-2.928838 43.2678)
## 3         6 POINT (-2.929697 43.26236)
## 4         6 POINT (-2.930489 43.26173)
## 5         6 POINT (-2.932582 43.26935)
## 6         6 POINT (-2.934481 43.26251)
## 7         2 POINT (-2.932982 43.27045)
## 8         1  POINT (-2.94359 43.26864)
## 9         1  POINT (-2.94359 43.26864)
## 10        2 POINT (-2.932982 43.27045)

Ploteamos los hostels por barrios y los hitos turísticos de la ciudad de Bilbao.

ggplot() +
    geom_sf(data = barrios) +
    geom_sf(data = ria_mercator , color='blue') +
    geom_sf(data = barrios_bilbao_cantidad, aes(color = barrios_bilbao),size = 5) +
    geom_sf(data = turistico_mercator, color = 'black',  shape = 4 ,size = 2) + 
    labs(title = "Mapa hostels e hitos turísticos por barrios ciudad de Bilbao", caption = "Fuente: OpenStreetMap", y="Latitud", x="Longitud") + 
    theme_light()

barrios_bilbao_cantidad <- barrios_bilbao_cantidad %>%
  st_set_geometry(NULL)
barrios <- barrios %>%
  left_join(barrios_bilbao_cantidad, by="barrios_bilbao")

Mapa coroplético de hostels por barrios.

ggplot() +
  geom_sf(data = barrios, aes(fill=(cantidad/Shape_Area)*10000)) +
  geom_sf(data = ria_mercator , color='blue') +
  geom_sf_text(data=barrios, aes(label = barrios_bilbao), size=3 , colour = "black")+
  labs(title = "Mapa hostels por barrios",
         subtitle = "Ciudad de Bilbao - España",
         fill = "Cantidad",
         caption= "Fuente: OpenStreetMap",
         y="Latitud",
         x="Longitud") +
  scale_fill_viridis_c() + 
  theme_light()

Traemos el mapa de oferta turística por barrios y lo comparamos con la oferta de hostels por barrios.

knitr::include_graphics("oferta_turistica_barrio_vir.png")

Como se puede observar, la mayor oferta de hostel tiene correlato con la oferta de hitos turístico. Los hostels se encuentran distribuidos de manera equitativa con los hitos turisticos. En el barrio de Bilbao la Vieja, Bolueta y Diseminado Basurtu - Zorrotza encontramos que el OSM tiene cargados, lo que entendemos, son mismos hostels pero con una leve diferencia en las coordenadas. Esta observación impacta en el mapa coroplético ya que muestra a los barrios con gran cantidad de hostels, cuando en la realidad no es así.

Con este ultimo Chunk damos por finalizado el TP2.-