library(osmdata)
## Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
library(tidyverse) 
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.2.1 ──
## ✔ ggplot2 3.2.0     ✔ purrr   0.3.2
## ✔ tibble  2.1.3     ✔ dplyr   0.8.3
## ✔ tidyr   0.8.3     ✔ stringr 1.4.0
## ✔ readr   1.3.1     ✔ forcats 0.4.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(sf) 
## Linking to GEOS 3.6.1, GDAL 2.1.3, PROJ 4.9.3
library(leaflet) 
bbox <- getbb("Partido Escobar, Buenos Aires")
bbox
##         min       max
## x -58.90090 -58.64936
## y -34.45644 -34.22916
bbox_poly <- getbb("Partido Escobar, Buenos Aires", format_out = "sf_polygon")
leaflet(bbox_poly) %>%
  addTiles() %>% 
  addPolygons()
escobar <- opq(bbox) %>% 
  add_osm_feature(key = "highway")
escobar <- escobar %>% 
  osmdata_sf()
highway <- escobar$osm_lines
ggplot() +
  geom_sf(data = highway)

calles <- st_intersection(highway, bbox_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
calles <- calles %>% 
  mutate(maxspeed = as.numeric(maxspeed),
         lanes = ifelse(is.na(lanes), 1, as.numeric(lanes)))
ggplot(calles) +
  geom_sf(aes(color = maxspeed), alpha = 0.5) +
  scale_color_viridis_c() +
  theme_void() +
  labs(title = "Partido de Escobar",
       subtitle = "Vías de circulación",
       caption = "fuente: OpenStreetMap",
       color = "Velocidad máxima")

restaurant <- opq(bbox) %>% 
  add_osm_feature(key = "amenity", value = "restaurant") %>% 
  osmdata_sf()
restaurant <- st_intersection(restaurant$osm_points, bbox_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
ggplot() +
  geom_sf(data = calles, 
          color = "gray") +
  geom_sf(data = restaurant) +
  geom_sf_label(data = restaurant, 
                aes(label = name), size = 2) +
  theme_void() +
  labs(title = "Partido de Escobar",
       subtitle = "Restaurantes",
       caption = "fuente: OpenStreetMap")
## 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 21 rows containing missing values (geom_label).

library(ggsflabel)
## 
## Attaching package: 'ggsflabel'
## The following objects are masked from 'package:ggplot2':
## 
##     geom_sf_label, geom_sf_text, StatSfCoordinates
ggplot() +
  geom_sf(data = calles,
            color = "darkslateblue") +
  geom_sf(data = restaurant,
            aes(color = "turquoise3")) +
  geom_sf_label_repel(data = restaurant, 
                  aes(label = name), size = 2) +
  theme_void() +
  labs(title = "Partidos de Escobar",
       subtitle = "restaurant",
       caption = "fuente: OpenStreetMap",
       color = "take away")
## Warning in st_point_on_surface.sfc(data$geometry): st_point_on_surface may
## not give correct results for longitude/latitude data
## Warning: Removed 21 rows containing missing values (geom_label_repel).

library(tidyverse)
library(sf)
escobar_limites <- st_read("~/Desktop/EJERCICIO2/escobar_localidades.geojson")
## Reading layer `OGRGeoJSON' from data source `/Users/florenciabaudino/Desktop/EJERCICIO2/escobar_localidades.geojson' using driver `GeoJSON'
## Simple feature collection with 7 features and 2 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -58.90108 ymin: -34.45646 xmax: -58.69029 ymax: -34.26188
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
restaurant_escobar <- st_join(restaurant, escobar_limites)
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
restaurant_escobar_conteo <- restaurant_escobar %>%
  group_by(NOM_ENTI) %>%
  summarise(cantidad=n())
## Warning: Factor `NOM_ENTI` contains implicit NA, consider using
## `forcats::fct_explicit_na`
ggplot() +
    geom_sf(data = escobar_limites) +
    geom_sf(data = restaurant_escobar_conteo, aes(color = NOM_ENTI)) 

restaurant_escobar_conteo <- st_join(escobar_limites, restaurant_escobar_conteo)
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
ggplot() +
    geom_sf(data = restaurant_escobar_conteo, aes(fill = cantidad))