Created
March 19, 2020 00:31
-
-
Save Joaobazzo/05f92053d7f9f57d48c9d1e1a4de8ad8 to your computer and use it in GitHub Desktop.
Visualization of 20 routes of public transportation of São Paulo using gtfs2gps R package
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(gtfs2gps) | |
| library(magrittr) | |
| library(ggplot2) | |
| library(gganimate) | |
| library(data.table) | |
| library(ggthemes) | |
| library(sf) | |
| library(viridis) | |
| library(sfheaders) | |
| library(mapview) | |
| library(gapminder) | |
| library(gifski) | |
| library(ggmap) | |
| library(googleway) | |
| library(leaflet) | |
| library(leaftime) | |
| library(sf) | |
| library(geojsonio) | |
| library(geojsonlint) | |
| gc(reset = TRUE) | |
| rm(list=ls()) | |
| setwd("L:/# DIRUR #/ASMEQ/bosistas/joaobazzo/") | |
| Sys.setenv(TZ= Sys.timezone(location = T) ) | |
| ###### Prepare data ------------------ | |
| # local GTFS.zip file | |
| sp <- read_gtfs(system.file("extdata/saopaulo.zip", package="gtfs2gps")) | |
| muni <- geobr::read_municipality(3550308,tp = "original") %>% sf::st_transform(4326) | |
| # # # # # subset time interval | |
| #gtfs_dt <- gtfs2gps::filter_day_period(sp, period_start = "07:00:", period_end = "08:01") | |
| # | |
| # get transport network as sf object | |
| shapes_sf <- gtfs_shapes_as_sf(gtfs_dt) | |
| # Convert GTFS data into a data.table with GPS-like records | |
| gps_dt <- gtfs2gps(sp, spatial_resolution = 15, progress = T, parallel = T ) | |
| # subset time interval | |
| gps_dt2 <- gps_dt[ between(departure_time, as.ITime("07:00:"), as.ITime("07:15")),] | |
| # Convert "GPS" points into sf | |
| gps_sf <- sfheaders::sf_point(gps_dt2, x = "shape_pt_lon" , y = "shape_pt_lat", keep = T) | |
| sf::st_crs(gps_sf) <- 4326 | |
| # convert gps to points | |
| sf_gps_points <- sf::st_cast(gps_sf, "POINT") | |
| # add departure time | |
| sf_gps_points$start <- paste0(Sys.Date()," ",gps_sf$departure_time) | |
| sf_gps_points$end <- c(sf_gps_points$start[-1], NA) | |
| bbox <- as.vector(st_bbox(sf_gps_points)) | |
| sf_gps_points1 <- sf_gps_points[seq(1,nrow(sf_gps_points),by=1),] | |
| sf_gps_points1 <- sf_gps_points1[sf_gps_points1$shape_id %in% unique(sf_gps_points1$shape_id)[1:60],] | |
| sf_gps_points2 <- sf_gps_points1[,c(13,14)] | |
| leaflet(geojsonio::geojson_json(sf_gps_points2)) %>% | |
| fitBounds(bbox[1],bbox[2],bbox[3],bbox[4]) %>% | |
| addPolygons(data=muni$geom,weight=2,color = "black",fill = FALSE, opacity = 0.8) %>% | |
| addProviderTiles(providers$CartoDB.Positron) %>% | |
| leaftime::addTimeline( | |
| sliderOpts = sliderOptions(duration = 250,waitToUpdateMap = TRUE, | |
| enableKeyboardControls =TRUE), | |
| timelineOpts = timelineOptions( | |
| styleOptions = styleOptions(color = c("blue"), radius = 3) | |
| ) | |
| ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment