library(ggmap)
## Warning: package 'ggmap' was built under R version 4.5.1
## Cargando paquete requerido: ggplot2
## ℹ Google's Terms of Service: <https://mapsplatform.google.com>
## Stadia Maps' Terms of Service: <https://stadiamaps.com/terms-of-service>
## OpenStreetMap's Tile Usage Policy: <https://operations.osmfoundation.org/policies/tiles>
## ℹ Please cite ggmap if you use it! Use `citation("ggmap")` for details.
library(leaflet)
## Warning: package 'leaflet' was built under R version 4.5.1
library(leaflet.extras)
## Warning: package 'leaflet.extras' was built under R version 4.5.1
library(osmdata)
## Warning: package 'osmdata' was built under R version 4.5.1
## Data (c) OpenStreetMap contributors, ODbL 1.0. https://www.openstreetmap.org/copyright
library(tidyverse)
## Warning: package 'lubridate' was built under R version 4.5.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.3.0
## ✔ purrr 1.0.4 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(sf)
## Warning: package 'sf' was built under R version 4.5.1
## Linking to GEOS 3.13.1, GDAL 3.11.0, PROJ 9.6.0; sf_use_s2() is TRUE
Probar informacion en openstreetmap - si ahi essta en R lo deberia encontrar.
bbox_comuna1 <- getbb("Comuna 1, Ciudad Autónoma de Buenos Aires, Argentina")
register_stadiamaps("04584325-d3a4-462c-9344-c47c03e9f957", write = TRUE)
## ℹ Replacing old key (04584325) with new key in C:\Users\Facundo\Documents/.Renviron
mapa_comuna1 <- get_stadiamap(bbox=bbox_comuna1,
maptype="alidade_smooth",
zoom=14)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
ggmap(mapa_comuna1)
poligono_comuna1 <- getbb("Comuna 1, Ciudad Autónoma de Buenos Aires, Argentina", format_out = "sf_polygon")
ggplot()+
geom_sf(data=poligono_comuna1)
ggmap(mapa_comuna1)+
geom_sf(data=poligono_comuna1, inherit.aes = FALSE,
fill=NA, color="red")
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
Usando https://wiki.openstreetmap.org/wiki/ES:Objetos_del_mapa
buscamos los objetos que nos interesa descargar desde openstreetmap.
Paso 1 Cargar BBOX
parques_comuna1 <- opq(bbox_comuna1)
Paso 2
Elegir que quiero descargar
parques_comuna1 <- add_osm_feature(parques_comuna1,
key="leisure",
value=c("park", "nature_reserve"))
Paso 3 : Descarga de info - Conexion a la base de datos
parques_comuna1 <- osmdata_sf(parques_comuna1)
parques_comuna1
## Object of class 'osmdata' with:
## $bbox : -34.634108,-58.3929401,-34.5774715,-58.3397467
## $overpass_call : The call submitted to the overpass API
## $meta : metadata including timestamp and version numbers
## $osm_points : 'sf' Simple Features Collection with 3600 points
## $osm_lines : 'sf' Simple Features Collection with 43 linestrings
## $osm_polygons : 'sf' Simple Features Collection with 271 polygons
## $osm_multilines : NULL
## $osm_multipolygons : 'sf' Simple Features Collection with 14 multipolygons
parques_polygons <- parques_comuna1$osm_polygons
parques_multipolygons <- parques_comuna1$osm_multipolygons
Aca le agrego la info al ggmap las capas que genere
ggmap(mapa_comuna1)+
geom_sf(data=poligono_comuna1, inherit.aes = FALSE,
fill=NA, color="red")+
geom_sf(data=parques_polygons, fill="green", inherit.aes = FALSE)+
geom_sf(data=parques_multipolygons, fill="green", inherit.aes = FALSE)
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
st_write(parques_polygons, "data/parques_polygons.geojson",delete_dsn = TRUE)
## Deleting source `data/parques_polygons.geojson' using driver `GeoJSON'
## Writing layer `parques_polygons' to data source
## `data/parques_polygons.geojson' using driver `GeoJSON'
## Writing 271 features with 47 fields and geometry type Polygon.
Ahora hacemos para otros rubros - bares y restaurantes
PASO 1: CARGAR BBOX
gastronomia_comuna1 <- opq(bbox_comuna1)
PASO 2: ELEGIR QUE QUIERO DESCARGAR
gastronomia_comuna1 <- add_osm_feature(gastronomia_comuna1,
key="amenity",
value=c("bar", "restaurant"))
PASO 3: DESCARGA DE INFO
gastronomia_comuna1 <- osmdata_sf(gastronomia_comuna1)
PASO 4: REVISAR LO DESCARGADO
gastronomia_comuna1
## Object of class 'osmdata' with:
## $bbox : -34.634108,-58.3929401,-34.5774715,-58.3397467
## $overpass_call : The call submitted to the overpass API
## $meta : metadata including timestamp and version numbers
## $osm_points : 'sf' Simple Features Collection with 835 points
## $osm_lines : NULL
## $osm_polygons : 'sf' Simple Features Collection with 32 polygons
## $osm_multilines : NULL
## $osm_multipolygons : NULL
Al ser puntos - debo poner osm_points
gastronomia_comuna1 <- gastronomia_comuna1$osm_points
ggmap(mapa_comuna1)+
geom_sf(data=poligono_comuna1, inherit.aes = FALSE,
fill=NA, color="red")+
geom_sf(data=gastronomia_comuna1, inherit.aes = FALSE)
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
Como tengo puntos por fuera de los limites. uso la funcion intersection
para recortar el mapa y dejar los que quedan dentro del poliogono
comuna. solo los que estan en ambas capas.
gastronomia_comuna1 <- st_intersection(gastronomia_comuna1, poligono_comuna1)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
ggmap(mapa_comuna1)+
geom_sf(data=poligono_comuna1, inherit.aes = FALSE,
fill=NA, color="red")+
geom_sf(data=gastronomia_comuna1, inherit.aes = FALSE, aes(color=amenity))
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
Saco los nulos!!
gastronomia_comuna1 <- gastronomia_comuna1 %>%
filter(!is.na(amenity))
gastronomia_comuna1 %>%
group_by(amenity) %>%
summarise(cantidad=n())
## Simple feature collection with 2 features and 2 fields
## Geometry type: MULTIPOINT
## Dimension: XY
## Bounding box: xmin: -58.39271 ymin: -34.63391 xmax: -58.3611 ymax: -34.58136
## Geodetic CRS: WGS 84
## # A tibble: 2 × 3
## amenity cantidad geometry
## <chr> <int> <MULTIPOINT [°]>
## 1 bar 98 ((-58.37009 -34.61835), (-58.37086 -34.61868), (-58.37151…
## 2 restaurant 511 ((-58.37059 -34.62525), (-58.37075 -34.62525), (-58.37123…
ggmap(mapa_comuna1)+
geom_sf(data=poligono_comuna1, inherit.aes = FALSE,
fill=NA, color="red")+
geom_sf(data=gastronomia_comuna1, inherit.aes = FALSE, aes(color=amenity))+
facet_wrap(~amenity)
## Coordinate system already present. Adding new coordinate system, which will
## replace the existing one.
factpal <- colorFactor(palette = c("seagreen4", "deeppink4"),
levels = unique(gastronomia_comuna1$amenity))
leaflet(gastronomia_comuna1) %>%
addTiles() %>%
addCircleMarkers(
popup = paste("Tipo:", gastronomia_comuna1$amenity, "<br>",
"Nombre:", gastronomia_comuna1$name),
radius = 5,
color = ~factpal(amenity),
opacity = 0.5,
fillColor = ~factpal(amenity),
fillOpacity = 0.5) %>%
addLegend(
"bottomright",
pal = factpal,
values = ~amenity,
title = "Tipo",
opacity = 1)
gastronomia_comuna1 <- gastronomia_comuna1 %>%
filter(!is.na(name))
leaflet() %>%
addTiles() %>%
addHeatmap(data=gastronomia_comuna1,
radius = 15,
max = 0.3)