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
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.5.1
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
linea20 <- st_read("datos/linea20.shp")
## Reading layer `linea20' from data source 
##   `C:\Users\PC\Desktop\PERFECCIONAMIENTO\MAESTRIA\TRANSPORTE PUBLICO URBANO\01_mapas\TPU\datos\linea20.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 2 features and 18 fields
## Geometry type: LINESTRING
## Dimension:     XYZ
## Bounding box:  xmin: 4383165 ymin: 6518636 xmax: 4390128 ymax: 6530944
## z_range:       zmin: 1 zmax: 135
## Projected CRS: POSGAR 94 / Argentina 4
linea20 %>%
  group_by(SENTIDO) %>%
  summarise(longitud_m = sum(st_length(geometry))) %>%
  mutate(longitud_km = as.numeric(longitud_m) / 1000)
## Simple feature collection with 2 features and 3 fields
## Geometry type: LINESTRING
## Dimension:     XYZ
## Bounding box:  xmin: 4383165 ymin: 6518636 xmax: 4390128 ymax: 6530944
## z_range:       zmin: 1 zmax: 135
## Projected CRS: POSGAR 94 / Argentina 4
## # A tibble: 2 × 4
##   SENTIDO longitud_m                                        geometry longitud_km
## * <chr>          [m]                                <LINESTRING [m]>       <dbl>
## 1 ida         21605. Z (4390125 6519387 1, 4390121 6519171 2, 43901…        21.6
## 2 vuelta      21253. Z (4383165 6530783 1, 4383179 6530827 2, 43831…        21.3

Distancia promedio entre paradas (por sentido)

paradas_completas <- st_read("datos/paradas_orden.shp")
## Reading layer `paradas_orden' from data source 
##   `C:\Users\PC\Desktop\PERFECCIONAMIENTO\MAESTRIA\TRANSPORTE PUBLICO URBANO\01_mapas\TPU\datos\paradas_orden.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 127 features and 10 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -7149817 ymin: -3693524 xmax: -7141814 ymax: -3679159
## Projected CRS: WGS 84 / Pseudo-Mercator
paradas_completas %>%
  group_by(sentido) %>%
  arrange(sentido, orden) %>%
  mutate(
    distancia_m = as.numeric(st_distance(geometry, lag(geometry), by_element = TRUE))
  ) %>%
  summarise(
    dist_promedio_m = mean(distancia_m, na.rm = TRUE)
  )
## Simple feature collection with 2 features and 2 fields
## Geometry type: MULTIPOINT
## Dimension:     XY
## Bounding box:  xmin: -7149817 ymin: -3693524 xmax: -7141814 ymax: -3679159
## Projected CRS: WGS 84 / Pseudo-Mercator
## # A tibble: 2 × 3
##   sentido dist_promedio_m                                               geometry
##   <chr>             <dbl>                                       <MULTIPOINT [m]>
## 1 ida                351. ((-7149814 -3679159), (-7149581 -3679369), (-7149394 …
## 2 vuelta             372. ((-7149817 -3679171), (-7149620 -3679294), (-7149311 …
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.5.1
ocupacion <- paradas_completas %>%
  group_by(sentido) %>%
  arrange(sentido, orden) %>%
  mutate(ocupacion = cumsum(suben - bajan))
ggplot(ocupacion, aes(x = orden, y = ocupacion, color = sentido)) +
  geom_line(size = 1.2) +
  labs(x = "Parada", y = "Pasajeros a bordo", title = "Perfil de ocupación por sentido") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

ocupacion %>%
  group_by(sentido) %>%
  slice_max(ocupacion, n = 1)
## Simple feature collection with 2 features and 11 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -7145857 ymin: -3689738 xmax: -7144387 ymax: -3687217
## Projected CRS: WGS 84 / Pseudo-Mercator
## # A tibble: 2 × 12
## # Groups:   sentido [2]
##   stop_id stop_name      stop_lat stop_lon sentido orden layer path  suben bajan
##   <chr>   <chr>             <dbl>    <dbl> <chr>   <dbl> <chr> <chr> <dbl> <dbl>
## 1 CU15    Av M Allende …    -31.4    -64.2 ida        26 Camb… "C:\…    31    25
## 2 C4105   Obispo Salgue…    -31.4    -64.2 vuelta     32 para… "C:\…    12     0
## # ℹ 2 more variables: geometry <POINT [m]>, ocupacion <dbl>
flujo_max <- 1388 
cap_max <- 70 * 0.9
frecuencia <- flujo_max / cap_max
frecuencia
## [1] 22.03175
flujo_max <- 1184 
cap_max <- 70 * 0.9
frecuencia <- flujo_max / cap_max
frecuencia
## [1] 18.79365
intervalo_min <- 60 / frecuencia
#install.packages("tidyverse")
library(tidyverse)
## Warning: package 'purrr' was built under R version 4.5.1
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ lubridate 1.9.4     ✔ tibble    3.2.1
## ✔ purrr     1.1.0     ✔ tidyr     1.3.1
## ✔ readr     2.1.5     
## ── 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
#install.packages("sf")
library(sf)
#install.packages("ggmap")
library(ggmap)
## ℹ 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.
#install.packages("osmdata")
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
bbox_cordoba <- getbb("Cordoba, Cordoba, Argentina")
mapa_cordoba <- get_stadiamap(bbox=bbox_cordoba,
                                 maptype="alidade_smooth",
                                 zoom=12)
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
ggmap(mapa_cordoba)

linea20 <- st_transform(linea20,4326)
paradas_completas <- st_transform(paradas_completas,4326)
linea20_ida <- linea20 %>% filter(SENTIDO == "ida")
linea20_vuelta <- linea20 %>% filter(SENTIDO == "vuelta")
paradas_ida <- paradas_completas %>% filter(sentido == "ida")
paradas_vuelta <- paradas_completas %>% filter(sentido == "vuelta")

MAPA

library(gganimate)
## Warning: package 'gganimate' was built under R version 4.5.1
paradas_df <- paradas_ida %>%
  st_coordinates() %>%
  as.data.frame() %>%
  cbind(st_drop_geometry(paradas_ida)) %>%
  rename(lon = X, lat = Y)
paradas2_df <- paradas_vuelta %>%
  st_coordinates() %>%
  as.data.frame() %>%
  cbind(st_drop_geometry(paradas_vuelta)) %>%
  rename(lon = X, lat = Y)
ggmap(mapa_cordoba) +
  geom_sf(data = linea20_ida, color = "#FF6464", size = 1, inherit.aes = FALSE) +
  geom_point(data = paradas_df, aes(x = lon, y = lat, frame = orden),
             color = "#1874CD", size = 3) +
  theme_void() +
  labs(title = "Recorrido línea 20 - Ida animada") +
  transition_reveal(along = orden)
## Coordinate system already present.
## ℹ Adding new coordinate system, which will replace the existing one.
## Warning in geom_point(data = paradas_df, aes(x = lon, y = lat, frame = orden),
## : Ignoring unknown aesthetics: frame

ggmap(mapa_cordoba) +
  geom_sf(data = linea20_vuelta, color = "#40E0D0", size = 1, inherit.aes = FALSE) +
  geom_point(data = paradas2_df, aes(x = lon, y = lat, frame = orden),
             color = "#1874CD", size = 3) +
  theme_void() +
  labs(title = "Recorrido línea 20 - Vuelta animada") +
  transition_reveal(along = orden)
## Coordinate system already present.
## ℹ Adding new coordinate system, which will replace the existing one.
## Warning in geom_point(data = paradas2_df, aes(x = lon, y = lat, frame = orden),
## : Ignoring unknown aesthetics: frame