TRABAJO PRACTICO N°1: Geoprocesamiento

CIENCIA DE DATOS PARA CIUDADES II

Snyders, Federico / Vargas, Juan

library(tidyverse)
## -- Attaching packages ---------------------------------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.0     v purrr   0.3.3
## v tibble  2.1.3     v dplyr   0.8.5
## v tidyr   1.0.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.5.0
## -- Conflicts ------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3

#Cargamos el geojson de la ciudad de Boston y el dataset con las escuelas públicas que oferta la ciudad. (fuente= https://data.boston.gov/dataset)

BOSTON <- st_read ("~/JUAN/POSGRADOS/MAESTRIA EN ECONOMIA URBANA/2020/CIENCIA DE DATOS PARA CIUDADES II/CLASE 1/TP_1/datos BOSTON/Boston_Neighborhoods.geojson")
## Reading layer `Boston_Neighborhoods' from data source `C:\Users\usuario\Documents\JUAN\POSGRADOS\MAESTRIA EN ECONOMIA URBANA\2020\CIENCIA DE DATOS PARA CIUDADES II\CLASE 1\TP_1\datos BOSTON\Boston_Neighborhoods.geojson' using driver `GeoJSON'
## Simple feature collection with 26 features and 7 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 739715.3 ymin: 2908294 xmax: 812255.1 ymax: 2970086
## CRS:            4326
ESCUELAS_BOSTON <- read.csv("~/JUAN/POSGRADOS/MAESTRIA EN ECONOMIA URBANA/2020/CIENCIA DE DATOS PARA CIUDADES II/CLASE 1/TP_1/datos BOSTON/Public_Schools.csv", stringsAsFactors = FALSE)
names(ESCUELAS_BOSTON)
##  [1] "ï..X"       "Y"          "OBJECTID_1" "OBJECTID"   "BLDG_ID"   
##  [6] "BLDG_NAME"  "ADDRESS"    "CITY"       "ZIPCODE"    "CSP_SCH_ID"
## [11] "SCH_ID"     "SCH_NAME"   "SCH_LABEL"  "SCH_TYPE"   "SHARED"    
## [16] "COMPLEX"    "POINT_X"    "POINT_Y"

#Retiramos las coordenas repetidas para evitar erros en el desarrollo

ESCUELAS_BOSTON2 <- select(ESCUELAS_BOSTON, -(POINT_X:POINT_Y))

names(ESCUELAS_BOSTON2)
##  [1] "ï..X"       "Y"          "OBJECTID_1" "OBJECTID"   "BLDG_ID"   
##  [6] "BLDG_NAME"  "ADDRESS"    "CITY"       "ZIPCODE"    "CSP_SCH_ID"
## [11] "SCH_ID"     "SCH_NAME"   "SCH_LABEL"  "SCH_TYPE"   "SHARED"    
## [16] "COMPLEX"

#Hacemos un grafico de geometrias y puntos, motrando la geolocalizacion de las escuelas públicas en cada barrio de la ciudad.

ggplot()+
  geom_sf(data=BOSTON)+
  geom_point(data = ESCUELAS_BOSTON2, aes(x=ï..X, y= Y),
             alpha=0.8,
             color= "red")
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data

#Convertirmos el dataset de escuelas en un archivo con contenido espacial.

ESCUELAS_BOSTON2 <- ESCUELAS_BOSTON2 %>% 
    st_as_sf(coords = c("ï..X", "Y"), crs = 4326)

#Generamos un plot con los dos archivos espaciales

ggplot()+
  geom_sf(data = BOSTON)+
  geom_sf(data = ESCUELAS_BOSTON2, color= "red")
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data

#Realizamos una union espacial y agrupamos la cantidad de escuelas por barrio.

BOSTON_con_ESCUELAS <- st_join (ESCUELAS_BOSTON2, BOSTON)
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
head(BOSTON_con_ESCUELAS)
## Simple feature collection with 6 features and 21 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 780367 ymin: 2962122 xmax: 790128.2 ymax: 2967094
## CRS:            EPSG:4326
##   OBJECTID_1 OBJECTID.x BLDG_ID             BLDG_NAME             ADDRESS
## 1          1          1       1            Guild Bldg   195 Leyden Street
## 2          2          2       3       Kennedy, P Bldg 343 Saratoga Street
## 3          3          3       4             Otis Bldg   218 Marion Street
## 4          4          4       6         Odonnell Bldg   33 Trenton Street
## 5          5          5       7 East Boston High Bldg     86 White Street
## 6          6          6       8   Umana / Barnes Bldg   312 Border Street
##          CITY ZIPCODE CSP_SCH_ID SCH_ID             SCH_NAME      SCH_LABEL
## 1 East Boston    2128       4061   4061     Guild Elementary          Guild
## 2 East Boston    2128       4541   4541 Kennedy Patrick Elem     PJ Kennedy
## 3 East Boston    2128       4322   4322      Otis Elementary           Otis
## 4 East Boston    2128       4543   4543 O'Donnell Elementary      O'Donnell
## 5 East Boston    2128       1070   1070     East Boston High East Boston HS
## 6 East Boston    2128       4323   4323        Umana Academy  Umana Academy
##   SCH_TYPE SHARED COMPLEX OBJECTID.y        Name   Acres Neighborhood_ID
## 1       ES                        38 East Boston 3012.06               8
## 2       ES                        38 East Boston 3012.06               8
## 3       ES                        38 East Boston 3012.06               8
## 4       ES                        38 East Boston 3012.06               8
## 5       HS                        38 East Boston 3012.06               8
## 6      K-8                        38 East Boston 3012.06               8
##   SqMiles ShapeSTArea ShapeSTLength                 geometry
## 1    4.71   131384487      121089.1 POINT (790128.2 2967094)
## 2    4.71   131384487      121089.1 POINT (783027.7 2963318)
## 3    4.71   131384487      121089.1 POINT (782112.8 2962122)
## 4    4.71   131384487      121089.1   POINT (780994 2963140)
## 5    4.71   131384487      121089.1   POINT (781823 2964190)
## 6    4.71   131384487      121089.1   POINT (780367 2963210)
GRUPO_BOSTON_con_ESCUELAS <- BOSTON_con_ESCUELAS %>% 
    group_by(Name) %>% 
    summarise(cantidad = n())
head(GRUPO_BOSTON_con_ESCUELAS)
## Simple feature collection with 6 features and 2 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: 747666.9 ymin: 2949869 xmax: 775733 ymax: 2963847
## CRS:            EPSG:4326
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data
## # A tibble: 6 x 3
##   Name      cantidad                                                    geometry
##   <fct>        <int>                                              <GEOMETRY [°]>
## 1 Allston          4 MULTIPOINT ((751868.4 2952925), (754098.8 2953524), (75505~
## 2 Back Bay         1                                    POINT (770246.7 2953059)
## 3 Bay Vill~        2           MULTIPOINT ((772662 2952280), (772904.8 2952413))
## 4 Brighton         6 MULTIPOINT ((747666.9 2953140), (747865.4 2953524), (74802~
## 5 Charlest~        4 MULTIPOINT ((773097 2963460), (773855.7 2963070), (774759.~
## 6 Chinatown        1                                    POINT (773779.1 2952129)

#Creamos nuevamente un mapa con puntos mostrando en distintos colores las escuelas por barrio.

ggplot()+
  geom_sf(data = BOSTON)+
  geom_sf(data = GRUPO_BOSTON_con_ESCUELAS, aes(color = Name))
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data

#Grafico de Barras mostrando la cantidad de escuelas por barrio

ggplot(GRUPO_BOSTON_con_ESCUELAS)+
  geom_col(aes(x= Name, y= cantidad))

#En un mapa de coropletas se muestra la oferta de escuelas públicas que ofrece la ciudad de Boston.

GRUPO_BOSTON_con_ESCUELAS2 <- GRUPO_BOSTON_con_ESCUELAS %>%
  st_set_geometry(NULL)
BOSTON <- BOSTON %>%
  left_join(GRUPO_BOSTON_con_ESCUELAS2, by="Name")
names(BOSTON)
## [1] "OBJECTID"        "Name"            "Acres"           "Neighborhood_ID"
## [5] "SqMiles"         "ShapeSTArea"     "ShapeSTLength"   "cantidad"       
## [9] "geometry"
ggplot() +
  geom_sf(data = BOSTON, aes(fill = cantidad)) +
  geom_sf_text(data=BOSTON, aes(label = Name), size=2, colour = "black") +
    labs(title = "Escuelas en BOSTON",
         subtitle = "Cantidad de escuelas por barrio",
         fill = "Cantidad",
         caption= "Fuente: https://data.boston.gov/dataset",
         y="",
         x="") +
  scale_fill_gradient(low="gray", high="light blue")
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data

#Cantidad de escuelas por superficie

ggplot() +
  geom_sf(data = BOSTON, aes(fill=(cantidad/SqMiles)*100)) +
  geom_sf_text(data=BOSTON, aes(label = Name), size=2.5, colour = "black")+
    labs(title = "Escuelas en BOSTON",
         subtitle = "Cantidad de escuelas por barrio",
         fill = "Cantidad por ha",
         caption= "Fuente: https://data.boston.gov/dataset",
         y="",
         x="") +
  scale_fill_gradient(low="white", high="blue")
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data


03/05/2020

TRABAJO PRACTICO N°2

cIENCIA DE DATOS PARA CIUDADES II: OpenStreetMap desde R

Snyders, Federico / Vargas, Juan

#Explorando y mapeando información georreferenciada de OpenStreetMap

library(leaflet)
library(osmdata)
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright

##Continuamos trabajando con la Ciudad de Boston, Massachusetts. Descargamos la informacion para la ciudad y la visualizamos.

boston <- getbb("boston")
boston
##         min       max
## x -71.19125 -70.80449
## y  42.22791  42.39698
boston_poly <- getbb("Boston, Massachusetts", format_out = "sf_polygon")
leaflet(boston_poly) %>%
    addTiles() %>% 
    addPolygons()

#Descargamos la información de las vias de circulación de la ciudad Boston.

Boston <- opq(boston) %>% 
    add_osm_feature(key = "highway")
Boston
## $bbox
## [1] "42.2279112,-71.1912491,42.3969775,-70.8044881"
## 
## $prefix
## [1] "[out:xml][timeout:25];\n(\n"
## 
## $suffix
## [1] ");\n(._;>;);\nout body;"
## 
## $features
## [1] " [\"highway\"]"
## 
## attr(,"class")
## [1] "list"           "overpass_query"
Boston <- Boston %>% 
    osmdata_sf()
calles_Boston <- Boston$osm_lines
head(calles_Boston[1:3,])
## Simple feature collection with 3 features and 251 fields
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: -71.08944 ymin: 42.36657 xmax: -71.02179 ymax: 42.39411
## CRS:            EPSG:4326
##          osm_id                name FIXME FIXME.hgv  NHS abutters access
## 4790718 4790718 Skybridge to Hilton  <NA>      <NA> <NA>     <NA>   <NA>
## 4790735 4790735                <NA>  <NA>      <NA> <NA>     <NA>   <NA>
## 8603359 8603359    Wheatland Street  <NA>      <NA> <NA>     <NA>   <NA>
##         access.conditional addr.city addr.housename addr.housenumber
## 4790718               <NA>      <NA>           <NA>             <NA>
## 4790735               <NA>      <NA>           <NA>             <NA>
## 8603359               <NA>      <NA>           <NA>             <NA>
##         addr.postcode addr.state addr.street alt_name amenity architect area
## 4790718          <NA>       <NA>        <NA>     <NA>    <NA>      <NA> <NA>
## 4790735          <NA>       <NA>        <NA>     <NA>    <NA>      <NA> <NA>
## 8603359          <NA>       <NA>        <NA>     <NA>    <NA>      <NA> <NA>
##         artwork_type
## 4790718         <NA>
## 4790735         <NA>
## 8603359         <NA>
##                                                          attribution barrier
## 4790718                                                         <NA>    <NA>
## 4790735                                                         <NA>    <NA>
## 8603359 Office of Geographic and Environmental Information (MassGIS)    <NA>
##          bic bicycle bicycle.lanes.backward bridge bridge.material
## 4790718 <NA>    <NA>                   <NA>    yes            <NA>
## 4790735 <NA>    <NA>                   <NA>   <NA>            <NA>
## 8603359 <NA>    <NA>                   <NA>   <NA>            <NA>
##         bridge.movable bridge.name bridge.structure building
## 4790718           <NA>        <NA>             <NA>     <NA>
## 4790735           <NA>        <NA>             <NA>     <NA>
## 8603359           <NA>        <NA>             <NA>     <NA>
##         building.architecture building.colour building.height building.levels
## 4790718                  <NA>            <NA>            <NA>            <NA>
## 4790735                  <NA>            <NA>            <NA>            <NA>
## 8603359                  <NA>            <NA>            <NA>            <NA>
##         building.material building.part  bus busway busway.right campusbuilding
## 4790718              <NA>          <NA> <NA>   <NA>         <NA>           <NA>
## 4790735              <NA>          <NA> <NA>   <NA>         <NA>           <NA>
## 8603359              <NA>          <NA> <NA>   <NA>         <NA>           <NA>
##         change.lanes.forward check_date class.bicycle comment condition
## 4790718                 <NA>       <NA>          <NA>    <NA>      <NA>
## 4790735                 <NA>       <NA>          <NA>    <NA>      <NA>
## 8603359                 <NA>       <NA>          <NA>    <NA>      fair
##         construction conveying covered created_by crossing crossing.island
## 4790718         <NA>      <NA>    <NA>       <NA>     <NA>            <NA>
## 4790735         <NA>      <NA>    <NA>       <NA>     <NA>            <NA>
## 8603359         <NA>      <NA>    <NA>       <NA>     <NA>            <NA>
##         crossing_1 crossing_ref cutting cycleway cycleway.both
## 4790718       <NA>         <NA>    <NA>     <NA>          <NA>
## 4790735       <NA>         <NA>    <NA>     <NA>          <NA>
## 8603359       <NA>         <NA>    <NA>     <NA>          <NA>
##         cycleway.both.lane cycleway.lanes.backward cycleway.left
## 4790718               <NA>                    <NA>          <NA>
## 4790735               <NA>                    <NA>          <NA>
## 8603359               <NA>                    <NA>          <NA>
##         cycleway.left.buffer cycleway.left.lane cycleway.right
## 4790718                 <NA>               <NA>           <NA>
## 4790735                 <NA>               <NA>           <NA>
## 8603359                 <NA>               <NA>           <NA>
##         cycleway.right.buffer cycleway.right.lane cycleway.right_1 denomination
## 4790718                  <NA>                <NA>             <NA>         <NA>
## 4790735                  <NA>                <NA>             <NA>         <NA>
## 8603359                  <NA>                <NA>             <NA>         <NA>
##         description designation destination destination.backward
## 4790718        <NA>        <NA>        <NA>                 <NA>
## 4790735        <NA>        <NA>        <NA>                 <NA>
## 8603359        <NA>        <NA>        <NA>                 <NA>
##         destination.forward destination.lanes destination.ref
## 4790718                <NA>              <NA>            <NA>
## 4790735                <NA>              <NA>            <NA>
## 8603359                <NA>              <NA>            <NA>
##         destination.ref.lanes destination.ref.to destination.street
## 4790718                  <NA>               <NA>               <NA>
## 4790735                  <NA>               <NA>               <NA>
## 8603359                  <NA>               <NA>               <NA>
##         destination.symbol direction driveway  ele embankment emergency fixme
## 4790718               <NA>      <NA>     <NA> <NA>       <NA>      <NA>  <NA>
## 4790735               <NA>      <NA>     <NA> <NA>       <NA>      <NA>  <NA>
## 8603359               <NA>      <NA>     <NA> <NA>       <NA>      <NA>  <NA>
##         floating foot footway ford former.name gnis.county_id gnis.county_name
## 4790718     <NA>  yes    <NA> <NA>        <NA>           <NA>             <NA>
## 4790735     <NA>  yes    <NA> <NA>        <NA>           <NA>             <NA>
## 8603359     <NA> <NA>    <NA> <NA>        <NA>           <NA>             <NA>
##         gnis.created gnis.feature_id gnis.import_uuid gnis.reviewed
## 4790718         <NA>            <NA>             <NA>          <NA>
## 4790735         <NA>            <NA>             <NA>          <NA>
## 8603359         <NA>            <NA>             <NA>          <NA>
##         gnis.state_id golf golf_cart goods handrail handrail.center hazmat
## 4790718          <NA> <NA>      <NA>  <NA>     <NA>            <NA>   <NA>
## 4790735          <NA> <NA>      <NA>  <NA>     <NA>            <NA>   <NA>
## 8603359          <NA> <NA>      <NA>  <NA>     <NA>            <NA>   <NA>
##         height  hgv hgv.conditional     highway historic           history
## 4790718   <NA> <NA>            <NA>     footway     <NA>              <NA>
## 4790735   <NA> <NA>            <NA>     footway     <NA>              <NA>
## 8603359   <NA> <NA>            <NA> residential     <NA> Retrieved from v1
##         horse hour_off hour_on  hov image incline indoor informal junction
## 4790718  <NA>     <NA>    <NA> <NA>  <NA>    <NA>   <NA>     <NA>     <NA>
## 4790735  <NA>     <NA>    <NA> <NA>  <NA>    <NA>   <NA>     <NA>     <NA>
## 8603359  <NA>     <NA>    <NA> <NA>  <NA>    <NA>   <NA>     <NA>     <NA>
##         junction.ref kerb landing landuse lanes lanes.backward lanes.forward
## 4790718         <NA> <NA>    <NA>    <NA>  <NA>           <NA>          <NA>
## 4790735         <NA> <NA>    <NA>    <NA>  <NA>           <NA>          <NA>
## 8603359         <NA> <NA>    <NA>    <NA>     1           <NA>          <NA>
##         layer leisure level light_rail  lit loc_name man_made massgis.ref
## 4790718     2    <NA>  <NA>       <NA> <NA>     <NA>     <NA>        <NA>
## 4790735  <NA>    <NA>  <NA>       <NA> <NA>     <NA>     <NA>        <NA>
## 8603359  <NA>    <NA>  <NA>       <NA> <NA>     <NA>     <NA>        <NA>
##         massgis.way_id maxheight maxspeed maxspeed.advisory maxspeed.type
## 4790718           <NA>      <NA>     <NA>              <NA>          <NA>
## 4790735           <NA>      <NA>     <NA>              <NA>          <NA>
## 8603359         194103      <NA>   25 mph              <NA>          <NA>
##         maxspeed.variable maxweight min_height motor_vehicle motorcar  mtb
## 4790718              <NA>      <NA>       <NA>          <NA>     <NA> <NA>
## 4790735              <NA>      <NA>       <NA>          <NA>     <NA> <NA>
## 8603359              <NA>      <NA>       <NA>          <NA>     <NA> <NA>
##         name.en name.etymology.wikidata name.mk name_orig natural noexit noname
## 4790718    <NA>                    <NA>    <NA>      <NA>    <NA>   <NA>   <NA>
## 4790735    <NA>                    <NA>    <NA>      <NA>    <NA>   <NA>   <NA>
## 8603359    <NA>                    <NA>    <NA>      <NA>    <NA>   <NA>   <NA>
##         note note.en note.lanes note.old_railway_operator notes official_name
## 4790718 <NA>    <NA>       <NA>                      <NA>  <NA>          <NA>
## 4790735 <NA>    <NA>       <NA>                      <NA>  <NA>          <NA>
## 8603359 <NA>    <NA>       <NA>                      <NA>  <NA>          <NA>
##         old_name old_railway_operator old_ref oneway oneway.bicycle oneway.bus
## 4790718     <NA>                 <NA>    <NA>   <NA>           <NA>       <NA>
## 4790735     <NA>                 <NA>    <NA>   <NA>           <NA>       <NA>
## 8603359     <NA>                 <NA>    <NA>    yes           <NA>       <NA>
##         oneway.conditional operator orig_name ownership parking
## 4790718               <NA>     <NA>      <NA>      <NA>    <NA>
## 4790735               <NA>     <NA>      <NA>      <NA>    <NA>
## 8603359               <NA>     <NA>      <NA>      <NA>    <NA>
##         parking.condition.both parking.condition.both.customers
## 4790718                   <NA>                             <NA>
## 4790735                   <NA>                             <NA>
## 8603359                   <NA>                             <NA>
##         parking.condition.both.maxstay parking.condition.both.time_interval
## 4790718                           <NA>                                 <NA>
## 4790735                           <NA>                                 <NA>
## 8603359                           <NA>                                 <NA>
##         parking.condition.left parking.condition.left.maxstay
## 4790718                   <NA>                           <NA>
## 4790735                   <NA>                           <NA>
## 8603359                   <NA>                           <NA>
##         parking.condition.left.time_interval parking.condition.right
## 4790718                                 <NA>                    <NA>
## 4790735                                 <NA>                    <NA>
## 8603359                                 <NA>                    <NA>
##         parking.condition.right.maxstay parking.condition.right.time_interval
## 4790718                            <NA>                                  <NA>
## 4790735                            <NA>                                  <NA>
## 8603359                            <NA>                                  <NA>
##         parking.lane parking.lane.both parking.lane.both_1 parking.lane.left
## 4790718         <NA>              <NA>                <NA>              <NA>
## 4790735         <NA>              <NA>                <NA>              <NA>
## 8603359         <NA>              <NA>                <NA>              <NA>
##         parking.lane.right parking.right.both postal_code private proposed
## 4790718               <NA>               <NA>        <NA>    <NA>     <NA>
## 4790735               <NA>               <NA>        <NA>    <NA>     <NA>
## 8603359               <NA>               <NA>        <NA>    <NA>     <NA>
##         proposed_name  psv public_transport railway ramp ramp.wheelchair
## 4790718          <NA> <NA>             <NA>    <NA> <NA>            <NA>
## 4790735          <NA> <NA>             <NA>    <NA> <NA>            <NA>
## 8603359          <NA> <NA>             <NA>    <NA> <NA>            <NA>
##         rcn_ref  ref religion roof.material route sac_scale segregated service
## 4790718    <NA> <NA>     <NA>          <NA>  <NA>      <NA>       <NA>    <NA>
## 4790735    <NA> <NA>     <NA>          <NA>  <NA>      <NA>       <NA>    <NA>
## 8603359    <NA> <NA>     <NA>          <NA>  <NA>      <NA>       <NA>    <NA>
##         shelter_type short_name shoulder sidewalk sidewalk.both
## 4790718         <NA>       <NA>     <NA>     <NA>          <NA>
## 4790735         <NA>       <NA>     <NA>     <NA>          <NA>
## 8603359         <NA>       <NA>     <NA>     <NA>          <NA>
##         sidewalk.both.kerb sidewalk.both.surface sidewalk.left sidewalk.right
## 4790718               <NA>                  <NA>          <NA>           <NA>
## 4790735               <NA>                  <NA>          <NA>           <NA>
## 8603359               <NA>                  <NA>          <NA>           <NA>
##         smoothness                             source source.addr
## 4790718       <NA>                               <NA>        <NA>
## 4790735       <NA>                               <NA>        <NA>
## 8603359       <NA> massgis_import_v0.1_20071008124914        <NA>
##         source.geometry source.hgv source.maxspeed source.name source.noname
## 4790718            <NA>       <NA>            <NA>        <NA>          <NA>
## 4790735            <NA>       <NA>            <NA>        <NA>          <NA>
## 8603359            <NA>       <NA>            <NA>        <NA>          <NA>
##         source.oneway source.surface source_ref sport start_date step_count
## 4790718          <NA>           <NA>       <NA>  <NA>       <NA>       <NA>
## 4790735          <NA>           <NA>       <NA>  <NA>       <NA>       <NA>
## 8603359          <NA>           <NA>       <NA>  <NA>       <NA>       <NA>
##         stroller surface tactile_paving taxi toll tourism tracktype
## 4790718     <NA>    <NA>           <NA> <NA> <NA>    <NA>      <NA>
## 4790735     <NA>    <NA>           <NA> <NA> <NA>    <NA>      <NA>
## 8603359     <NA>    <NA>           <NA> <NA> <NA>    <NA>      <NA>
##         traffic_calming trail_visibility train tram tree_lined trolley_wire
## 4790718            <NA>             <NA>  <NA> <NA>       <NA>         <NA>
## 4790735            <NA>             <NA>  <NA> <NA>       <NA>         <NA>
## 8603359            <NA>             <NA>  <NA> <NA>       <NA>         <NA>
##         tunnel turn turn.lanes turn.lanes.backward turn.lanes.forward  url
## 4790718   <NA> <NA>       <NA>                <NA>               <NA> <NA>
## 4790735   <NA> <NA>       <NA>                <NA>               <NA> <NA>
## 8603359   <NA> <NA>       <NA>                <NA>               <NA> <NA>
##         vehicle vehicle.lanes.backward website website.searchstring wheelchair
## 4790718    <NA>                   <NA>    <NA>                 <NA>       <NA>
## 4790735    <NA>                   <NA>    <NA>                 <NA>       <NA>
## 8603359    <NA>                   <NA>    <NA>                 <NA>       <NA>
##         width width.feet wikidata wikimedia_commons wikipedia
## 4790718  <NA>       <NA>     <NA>              <NA>      <NA>
## 4790735  <NA>       <NA>     <NA>              <NA>      <NA>
## 8603359  12.2       <NA>     <NA>              <NA>      <NA>
##                               geometry
## 4790718 LINESTRING (-71.02179 42.36...
## 4790735 LINESTRING (-71.03662 42.37...
## 8603359 LINESTRING (-71.08944 42.39...
ggplot() +
    geom_sf(data = calles_Boston)

#Exploramos y visualizamos el ejido municipal de interés.

calles_Boston <- st_intersection(calles_Boston, boston_poly)
## although coordinates are longitude/latitude, st_intersection assumes that they are planar
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries

#Visualizamos la geometría.

ggplot() +
    geom_sf(data = calles_Boston)

calles_Boston <- calles_Boston%>%
mutate(maxspeed = as.numeric(str_remove(maxspeed, "mph")),
lanes = ifelse(is.na(lanes), 1, as.numeric(lanes)))
## Warning: NAs introducidos por coerción

#Mapa donde se muestra las velocidades maximas en las calles de Boston.

ggplot(calles_Boston) +
    geom_sf(aes(color = maxspeed), alpha = 0.8) +
    scale_color_viridis_c() +
      theme_void() +
    labs(title = "Boston, Massachusetts",
         subtitle = "Calles",
         caption = "fuente: OpenStreetMap",
         color = "velocidad máxima")

#Buscamos contrastar la información proveniente de https://data.boston.gov con la de OpenStreetMaps, para ello desde la biblioeta de https://wiki.openstreetmap.org/wiki/Map_Features usando el key AMENITY descargarmos la informacion de las escuelas primarias y secundarias de la ciudad.

escuelas_Boston <- opq(boston) %>% 
  add_osm_feature(key = "amenity", value = "school") %>% 
  osmdata_sf() 
escuelas_Boston <- st_intersection(escuelas_Boston$osm_points, boston_poly)
## although coordinates are longitude/latitude, st_intersection assumes that they are planar
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries

#El mapa muestra la distribución de las escuelas en la ciudad.

ggplot() +
  geom_sf(data = calles_Boston, 
            color = "darkslateblue") +

  geom_sf_label(data = escuelas_Boston, 
                  aes(label = name), size = 3) +
  theme_void() +
  labs(title = "Boston, Massachusetts",
       subtitle = "Escuelas",
       caption = "Fuente: OpenStreetMap",
       color = "Escuelas primarias y secundarias")
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## Warning: Removed 1428 rows containing missing values (geom_label).

#Agrupamos la cantidad de escuelas por barrio y filtramos las filas con “NA” para visualizar la información de la geometría correctamente.

 escuelas_en_Boston2 <-  escuelas_Boston%>%
       filter(!is.na(addr.city)) %>% 
       group_by(addr.city) %>% 
    summarise(cantidad=n())

#Información de OpenStreetMaps donde muestra el resultado del relevamiento realizado por usuarios para la cantidad de escuelas por barrio.

head(escuelas_en_Boston2)
## Simple feature collection with 6 features and 2 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: -71.1615 ymin: 42.26485 xmax: -71.0044 ymax: 42.38846
## CRS:            EPSG:4326
## # A tibble: 6 x 3
##   addr.city   cantidad                                                  geometry
##   <chr>          <int>                                            <GEOMETRY [°]>
## 1 Allston            3 MULTIPOINT ((-71.13776 42.35216), (-71.13775 42.35209), ~
## 2 Boston            25 MULTIPOINT ((-71.13074 42.3556), (-71.12102 42.35199), (~
## 3 Brighton           4 MULTIPOINT ((-71.1615 42.35113), (-71.15299 42.35243), (~
## 4 Charlestown        1                                POINT (-71.06088 42.38126)
## 5 Dorchester        25 MULTIPOINT ((-71.08545 42.30134), (-71.08459 42.31616), ~
## 6 East Boston        4 MULTIPOINT ((-71.15892 42.26485), (-71.04006 42.37153), ~

#En el mapa se visualiza la distribucón del relevamiento.

#Podemos observar que la mayor distribución se encuentra al norte, que es también donde confluyen las principales calles y densidad de vías de circulación. Es decir, que asumimos será la zona céntrica, y entonces, la que contiene mayor densidad de población.

ggplot()+
  geom_sf(data = calles_Boston, alpha=0.1)+
  geom_sf(data = escuelas_en_Boston2, aes(color = addr.city))+
    labs(title = "Boston",
       subtitle = "Cantidad de escuelas por Barrio",
       caption = "Fuente: OpenStreetMap",
       color = "Escuelas primarias y secundarias")

#Información de https://data.boston.gov donde muestra el resultado del relevamiento realizado por el municipio para la cantidad de escuelas por barrio.

head(GRUPO_BOSTON_con_ESCUELAS)
## Simple feature collection with 6 features and 2 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: 747666.9 ymin: 2949869 xmax: 775733 ymax: 2963847
## CRS:            EPSG:4326
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data
## # A tibble: 6 x 3
##   Name      cantidad                                                    geometry
##   <fct>        <int>                                              <GEOMETRY [°]>
## 1 Allston          4 MULTIPOINT ((751868.4 2952925), (754098.8 2953524), (75505~
## 2 Back Bay         1                                    POINT (770246.7 2953059)
## 3 Bay Vill~        2           MULTIPOINT ((772662 2952280), (772904.8 2952413))
## 4 Brighton         6 MULTIPOINT ((747666.9 2953140), (747865.4 2953524), (74802~
## 5 Charlest~        4 MULTIPOINT ((773097 2963460), (773855.7 2963070), (774759.~
## 6 Chinatown        1                                    POINT (773779.1 2952129)

#En el siguiente gráfico de puntos visualizamos el contraste de la información relevada por el municipio y los usuarios de OpenStreetMap. El color azul muestra la distribución de la información de los usuarios y el color rojo muestra la distribución de la información del municipio.

ggplot() + 
  geom_point(data= escuelas_en_Boston2, aes(y = cantidad, x = addr.city, color="blue"))+
  geom_point(data= GRUPO_BOSTON_con_ESCUELAS, aes(x= Name, y= cantidad, color = "red"))+
    labs(title = "Escuelas primarias y secundarias en Boston",
         subtitle = "Cantidad de escuelas por barrio",
         caption = "Fuente: https://data.boston.gov/dataset, OpenStreetMap",
         x = "BARRIO",
         y = "CANTIDAD",
         color= "Contraste de relevamiento") 

#Cuando graficamos y observamos respecto al análisis anterior utilizando como fuente de datos la página oficial de datos abiertos de Boston, notamos una enrome diferencia entre una y otra. En el caso de la información cargada desde OSM, al filtrar aquellas escuelas con ciudad NA, perdemos información. A la vez, existe información dispar sobre los registros por barrio. Para verlo con mejor claridad utilizamos un gráfico de puntos cargando la cantidad de escuelas por barrio de cada dataset que muestra la diferencia existe entre una fuente y la siguiente.

#Para hacer un nuevo mapa de coropletas agregamos la información de OpenStreetMap al dataset del tp1.

escuelas_en_Boston3 <- escuelas_en_Boston2 %>% 
  rename(Name = addr.city) %>% 
  st_set_geometry(NULL)
BOSTON2 <- BOSTON %>% 
  left_join(escuelas_en_Boston3, by="Name")
## Warning: Column `Name` joining factor and character vector, coercing into
## character vector
names(BOSTON2)
##  [1] "OBJECTID"        "Name"            "Acres"           "Neighborhood_ID"
##  [5] "SqMiles"         "ShapeSTArea"     "ShapeSTLength"   "cantidad.x"     
##  [9] "cantidad.y"      "geometry"

#En el Tp1 el barrio con mayor cantidad de escuelas es el barrio de Dorchester al igual que en el relevamiento realizado por OpenStreetMap, como muestra el siguiente mapa.

ggplot() +
  geom_sf(data = BOSTON2, aes(fill = cantidad.y)) +
  geom_sf_text(data=BOSTON2, aes(label = Name), size=2, colour = "black") +
    labs(title = "Escuelas en BOSTON",
         subtitle = "Cantidad de escuelas por barrio",
         fill = "Cantidad",
         caption= "Fuente: OpenStreetMap",
         y="",
         x="") +
  scale_fill_gradient(low="gray", high="light blue")
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data

#En el tp1 el barrio con con mayor densidad de escuelas según el municipio de Boston es Charlestown, mientras que en el relevamiento realizado en OpenStreetMaps el barrio con mayor densidad de escuelas es Roxbury como muestra el siguiente mapa.

ggplot() +
  geom_sf(data = BOSTON2, aes(fill=(cantidad.y/SqMiles)*100)) +
  geom_sf_text(data=BOSTON2, aes(label = Name), size=2.5, colour = "black")+
    labs(title = "Escuelas en BOSTON",
         subtitle = "Barrio con mayor densidad de escuelas",
         fill = "Cantidad por ha",
         caption= "Fuente: OpenStreetMap",
         y="",
         x="") +
  scale_fill_gradient(low="white", high="blue")
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data
## Warning in st_point_on_surface.sfc(sf::st_zm(x)): st_point_on_surface may not
## give correct results for longitude/latitude data
## Warning in st_is_longlat(x): bounding box has potentially an invalid value range
## for longlat data