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
barrios <- st_read("https://gobiernoabierto.cordoba.gob.ar/media/datos/barrios2.kml")
## Reading layer `Barrios de Cordoba' from data source `https://gobiernoabierto.cordoba.gob.ar/media/datos/barrios2.kml' using driver `KML'
## Simple feature collection with 502 features and 2 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -64.30993 ymin: -31.52498 xmax: -64.05738 ymax: -31.30852
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
barrios<-mutate(barrios,barrio=row_number())
head(barrios)
## Simple feature collection with 6 features and 3 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: -64.24949 ymin: -31.48442 xmax: -64.17119 ymax: -31.33909
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##   Name Description                       geometry barrio
## 1                  MULTIPOLYGON (((-64.24561 -...      1
## 2                  MULTIPOLYGON (((-64.21201 -...      2
## 3                  MULTIPOLYGON (((-64.20441 -...      3
## 4                  MULTIPOLYGON (((-64.20148 -...      4
## 5                  MULTIPOLYGON (((-64.17123 -...      5
## 6                  MULTIPOLYGON (((-64.24341 -...      6
escuelas <- read.csv("~/Desktop/Ejercicio II/escuelas.csv")
escuelas <- separate(data = escuelas,
col = COORDENADAS, #indica columna a separar
into = c("LAT", "LON"), #indica nombre de las nuevas columnas
sep = ",") #indica separador
escuelas<- escuelas %>% 
    filter(!is.na(LAT), !is.na(LON)) %>% 
    st_as_sf(coords = c("LON", "LAT"), crs = 4326)
ggplot() +
    geom_sf(data = barrios) +
    geom_sf(data = escuelas)

ggplot() +
    geom_sf(data = barrios) +
    geom_sf(data = escuelas, color = "orange")

escuelas_barrios <- st_join(escuelas, barrios)
## 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
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
escuelas_barrios2<-escuelas_barrios%>%
  group_by(barrio)%>%
  summarise(cantidad=n())
escuelas_barrios2 <- st_join(barrios, escuelas_barrios2)
## although coordinates are longitude/latitude, st_intersects assumes that they are planar
ggplot()+
geom_sf(data=escuelas_barrios2, aes(fill = cantidad), color = NA)