bicis <- read.csv("C:/Users/Public/colectivo/bici.csv" ,fileEncoding="UTF-8",sep=",", stringsAsFactors =FALSE)
estaciones <- read.csv("C:/Users/Public/colectivo/estaciones.csv" ,fileEncoding="UTF-8",sep=",", stringsAsFactors =FALSE)
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.1
## Registered S3 method overwritten by 'rvest':
##   method            from
##   read_xml.response xml2
## -- Attaching packages ----------------------------------------------------------------------- tidyverse 1.2.1 --
## v ggplot2 3.2.0       v purrr   0.3.2  
## v tibble  2.1.1       v dplyr   0.8.0.1
## v tidyr   0.8.3       v stringr 1.4.0  
## v readr   1.3.1       v forcats 0.4.0
## -- Conflicts -------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggmap)
## Warning: package 'ggmap' was built under R version 3.6.1
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, PROJ 4.9.3
library(osrm)
## Warning: package 'osrm' was built under R version 3.6.1
## Data: (c) OpenStreetMap contributors, ODbL 1.0 - http://www.openstreetmap.org/copyright
## Routing: OSRM - http://project-osrm.org/
library(dplyr)
library(purrr)
library(osmdata)
## Warning: package 'osmdata' was built under R version 3.6.1
## Data (c) OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright
bicis <- bicis %>%
  rename(origen="bici_nombre_estacion_origen") %>% 
  rename(destino="bici_nombre_estacion_destino")
estaciones_viejas <- st_read("C:/Users/Public/colectivo/zip/estaciones_de_bicicletas.shp")
## Reading layer `estaciones_de_bicicletas' from data source `C:\Users\Public\colectivo\zip\estaciones_de_bicicletas.shp' using driver `ESRI Shapefile'
## Simple feature collection with 199 features and 8 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 100755.7 ymin: 98616.85 xmax: 109815.6 ymax: 106787.5
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=-34.6297166 +lon_0=-58.4627 +k=0.9999980000000001 +x_0=100000 +y_0=100000 +ellps=intl +units=m +no_defs
espacios_verdes <- st_read("C:/Users/Public/colectivo/verde/espacio-verde-publico.shp")
## Reading layer `espacio-verde-publico' from data source `C:\Users\Public\colectivo\verde\espacio-verde-publico.shp' using driver `ESRI Shapefile'
## Warning in CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
## GDAL Message 1: One or several characters couldn't be converted correctly
## from CP1252 to UTF-8. This warning will not be emitted anymore
## Simple feature collection with 1708 features and 31 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 93723.44 ymin: 91536.04 xmax: 111323.5 ymax: 111392.8
## epsg (SRID):    NA
## proj4string:    +proj=tmerc +lat_0=-34.6297166 +lon_0=-58.4627 +k=0.9999980000000001 +x_0=100000 +y_0=100000 +ellps=intl +units=m +no_defs
conteo <- bicis %>% 
    group_by(origen, bici_estacion_origen, destino,bici_estacion_destino ) %>% 
    summarise(total=n())%>%
  arrange(total)%>%
rename(id_estacion="bici_estacion_origen")%>%
  rename(NRO_EST="bici_estacion_destino")
top10 <- conteo %>% 
    ungroup() %>% 
    filter(origen != destino) %>% 
    top_n(10)
## Selecting by total
top10_NO <- top10 %>% 
  mutate(id_estacion=ifelse(id_estacion==175,147,id_estacion))
ggplot() + 
    geom_tile(data = conteo, aes(x = origen, y = destino, fill = total)) +
    scale_fill_distiller(palette = "Spectral")

ggplot() + 
    geom_tile(data = top10, 
              aes(y = as.factor(origen),
                  x = as.factor(destino),
                  fill = total)) +
    scale_fill_distiller(palette = "Spectral")

estaciones_viejas <- estaciones_viejas %>%
mutate(lat = unlist(map(estaciones_viejas$geometry,1)),
long = unlist(map(estaciones_viejas$geometry,2)))
top10 <- top10 %>% 
  mutate(id_estacion=as.numeric(id_estacion)) %>% 
  mutate(NRO_EST=as.numeric(NRO_EST))
Viajes <- left_join(top10,estaciones)%>%
mutate(lat=as.numeric(lat)) %>% 
  mutate(long=as.numeric(long)) %>% 
rename(origen_lat = lat,
           origen_long = long)
## Joining, by = "id_estacion"
Viajes <- Viajes %>% 
  left_join(estaciones, 
              by = c("NRO_EST"= "id_estacion")) 
recorridos <- Viajes %>% 
    mutate(ID = paste(origen, "-", destino))
recorrido_simple <- osrmRoute(src = c(Viajes$origen, Viajes$origen_lat, Viajes$origen_long), 
                                 dst = c(Viajes$destino, Viajes$lat, Viajes$long))
## The OSRM server returned an error:
## Error: InvalidInput
## Coordinate is invalid: 025 - Plaza Guemes,009 - Parque Las Heras
bbox <- make_bbox(Viajes$origen_long, Viajes$origen_lat)
mapa_base <- get_stamenmap(bbox, color = "bw", zoom = 12)
## Source : http://tile.stamen.com/terrain/12/1383/2467.png
## Source : http://tile.stamen.com/terrain/12/1383/2468.png
ggmap(mapa_base) +
    geom_point(data = Viajes, aes(x = origen_long, y = origen_lat), color = "limegreen")

ggplot()+
  geom_sf(data=espacios_verdes, fill="green")

Consti <- c(name="Constitucion",long =-58.38093,lat=-34.62674)

Guemes <- c(name="Plaza Guemes", long= -58.41612,lat=-34.58943)

LasHeras <- c(name="Parque Las Heras", long= -58.40774,lat=-34.58544)

Ecuador<- c(name="Ecuador", long= -58.40461,lat=-34.59610)

EcoParque<- c(name="EcoParque", long= -58.41388,lat=-34.57551)

Italia<- c(name="Plaza Italia", long= -58.42095,lat=-34.58055)

Pena<- c(name="Pena", long= -58.39737,lat=-34.59082)

Congreso <- c(name="Plaza Congreso", long= -58.38934,lat=-34.6094218)

Lezama<- c(name="Parque Lezama", long= -58.36976,lat=-34.628526)
Uno <- osrmRoute(src = Pena, 
                                 dst = LasHeras, 
                                 sp = TRUE, 
                                 overview = "full")
## Warning: sp is deprecated; use returnclass instead.
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.6.1
leaflet(Uno) %>% 
    addTiles() %>% 
    addPolylines(color = "red")
EspaciosVerdes <- opq(bbox) %>% 
  add_osm_feature(key = "landuse", value = "greenfield") %>% 
  osmdata_sf()