Thursday, March 19, 2020

Creating an animated gif of public transport networks using GTFS data



Creating an animated gif of public transport networks using GTFS data and the gtfs2gps package

In this gist we show with a reproducible example how to create an animation of public transport networks using GTFS data in R. We use a few packages to do this. One of the core packages here is the new gtfs2gps. The gtfs2gps package converts public transport data in GTFS format to GPS-like records in a data.frame/data.table, which we will be using to create a .gif with the gganimate package.

Step 1 - Convert GTFS to GPS-like records

The first step is to process a GTFS.zip file. The function gtfs2gps{gtfs2gps} interpolates the space-time position of each vehicle in each trip considering the network distance and average speed between stops. The output is a data.table where each row represents the timestamp of each vehicle at a given spatial resolution. In this example, we use a sample of the public transport network of Sao Paulo (Brazil) mapped every 30 meters.


# load libraries
library(gtfs2gps)
library(geobr)
library(ggplot2)
library(gganimate)
library(data.table)
library(ggthemes)
library(sf)
library(viridis)
library(sfheaders)



###### 1. process public transport data  ------------------

# get local GTFS.zip file
gtfs_zip <- system.file("extdata/saopaulo.zip", package="gtfs2gps" )

# read gtfs
gtfs_dt <- gtfs2gps::read_gtfs(gtfs_zip)

# filter transport services on weekdays
gtfs_dt <- gtfs2gps::filter_week_days(gtfs_dt)

# get transport network as sf object
shapes_sf <- gtfs2gps::gtfs_shapes_as_sf(gtfs_dt)

# Convert GTFS data into a data.table with GPS-like records
gps_dt <- gtfs2gps::gtfs2gps(gtfs_dt, spatial_resolution = 30, progress = T, parallel = T)
head(gps_dt)

# subset time interval. Get only vehicle positions between 7am and 7:30am
gps_dt2 <- gps_dt[ between(departure_time, as.ITime("07:00:"), as.ITime("07:31"))]

# remove observations with 'too high speed' (in km/h)
gps_dt2 <- gps_dt2[speed < 20 , ]


# 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
head(gps_sf)

Step 2 - Create gif

Once you have created the GPS-like dataset of the public transport network, creating an animation gif is simple as this:


###### 2. gif with blank background [fully reproducible] ------------------

# download Sao Paulo border for spatial context
sao_paulo <- geobr::read_municipality(code_muni = 3550308)

anim <- ggplot() +
          geom_sf(data = sao_paulo, color='gray50', fill=NA) +
          geom_sf(data = shapes_sf, color='gray90', size=0.01) +
          geom_point(data = gps_dt2, aes(x = shape_pt_lon, y=shape_pt_lat, colour = trip_id), size=1.5, alpha = 0.5, show.legend = FALSE) +
          scale_colour_viridis( discrete = T ) +
          coord_sf(ylim = c(min(gps_dt2$shape_pt_lat), max(gps_dt2$shape_pt_lat)) ) +
          theme_map() +
  
          # gganimate specificatons
          labs(title = 'Time: {frame_time}') +
          transition_time(as.POSIXct(departure_time) + 10800) +  # need to fix issue with time zone
          shadow_wake(wake_length = 0.015, alpha = FALSE) +
          ease_aes('linear')


# save gif
anim_save(animation = anim, "./tests_rafa/spo_gtfs_sample_20fps__GIST.gif", fps = 20)

Extra

In case you'd like to add more context info to the gif, you can use rasters as spatial tiles. I've added here some background info using a mapbox tile created by Kaue Braga for the Access to Opportunities Project. Thanks Kaue.


###### 3. gif tile background ------------------

# read map tile for spatial context
map_tiles <- readr::read_rds("L:/Proj_acess_oport/data/maptiles_crop/2019/mapbox/maptile_crop_mapbox_spo_2019.rds")

# convert gps data to UTM
gps_sf_utm <- sf::st_transform(gps_sf, 3857)
gps_dt_utm <- sfheaders::sf_to_df( gps_sf_utm, fill = TRUE )

anim2 <- ggplot() +
          geom_raster(data = map_tiles, aes(x, y, fill = hex), alpha = 1) +
          scale_fill_identity() +
          geom_sf(data= st_transform(sao_paulo, 3857), color='gray50', fill=NA) +
          geom_point(data = gps_dt_utm, aes(x = x, y=y, colour = trip_id), size=1.5, alpha = 0.6, show.legend = FALSE) +
          scale_colour_viridis(discrete = T) +
          coord_sf(ylim = c(min(gps_dt_utm$y), max(gps_dt_utm$y)) ) +
          theme_map() + 
          
          # gganimate specificatons
          labs(title = 'Time: {frame_time}') +
          transition_time(as.POSIXct(departure_time) + 10800) + # need to fix issue with time zone
          shadow_wake(wake_length = 0.015, alpha = FALSE) +
          ease_aes('linear')
        

# save gif
anim_save(animation = anim2, "./tests_rafa/spo_gtfs_sample_20fps_tile__GIST.gif", fps = 20)

Wednesday, March 18, 2020

Sunday, March 8, 2020

Call for papers: Advances in Spatial and Transport Network Analysis

The Int Journal of Geo-information has opened a call for papers for special issue on "Advances in Spatial and Transport Network Analysis". This issue is edited by Henrikki Tenkanen, Elsa Arcaute, Marta C. Gonzalez and myself. This is a great opportunity to create a dialogue between network and social scientists, transport geographers, engineers etc working on transport and mobility networks. This dialogue raises new challenges, though, as discussed in this thoughtful recent paper by Tim Schwanen.

Here is a short snippet of the cfp:
"This Special Issue is dedicated to papers focusing on recent advances in the development of new measures and methodologies to evaluate and analyze the performance of transportation networks. These measures might include, but are not limited to, environmental costs or exposures (e.g., CO2 , noise, pollution); monetary costs (the price of access), complexity, and resilience of multimodal transportation networks; or focus on qualitative aspects of travel, where travel might be seen as a gain instead of cost (such as exposure to aesthetic or green environments). Methodologically, we welcome works using novel ways to measure transport network connectivity, performance, and accessibility, including recent advances in machine learning and AI. Special attention will be paid to papers studying transport-related questions with interdisciplinary approaches."

Thursday, March 5, 2020

New paper out: Disparities in travel times between cars and public transport

I am very glad to share our new paper looking at the travel time gap between private and public transport at high spatial and temporal resolutions. The study combines real-time traffic data, transit data, and travel demand estimated using Twitter data to compare this travel time gap in four cities (São Paulo, Stockholm, Sydney and Amsterdam).

Despite remarkable differences between these cities in terms of transportation networks, area, and population size, we found travel times of transit and vs. driving are surprisingly similar across cities: R < 1 for trips shorter than 3km, then increases rapidly but quickly stabilizes at 2 (figure below). Moreover, using public transport generally takes on average 1.4–2.6 times longer than driving a car. The share of area where travel time favors public transport over car use is also very small in all cities. As Giulio Mattioli noted on Twitter, these results 'would confirm that car dependence is much more than just a question of culture & attitudes'.

The paper is open access and it was written in collaboration with a great team led by Yuan Liao and Sonia Yeh at University of Chalmers, Sweden.

Liao, Y., Gil, J., Pereira, R.H.M. et al. Disparities in travel times between car and transit: Spatiotemporal patterns in cities. Scientific Reports 10, 4056 (2020). https://doi.org/10.1038/s41598-020-61077-0

Abstract:
Cities worldwide are pursuing policies to reduce car use and prioritise public transit (PT) as a means to tackle congestion, air pollution, and greenhouse gas emissions. The increase of PT ridership is constrained by many aspects; among them, travel time and the built environment are considered the most critical factors in the choice of travel mode. We propose a data fusion framework including real-time traffic data, transit data, and travel demand estimated using Twitter data to compare the travel time by car and PT in four cities (São Paulo, Brazil; Stockholm, Sweden; Sydney, Australia; and Amsterdam, the Netherlands) at high spatial and temporal resolutions. We use real-world data to make realistic estimates of travel time by car and by PT and compare their performance by time of day and by travel distance across cities. Our results suggest that using PT takes on average 1.4–2.6 times longer than driving a car. The share of area where travel time favours PT over car use is very small: 0.62% (0.65%), 0.44% (0.48%), 1.10% (1.22%) and 1.16% (1.19%) for the daily average (and during peak hours) for São Paulo, Sydney, Stockholm, and Amsterdam, respectively. The travel time disparity, as quantified by the travel time ratio R (PT travel time divided by the car travel time), varies widely during an average weekday, by location and time of day. A systematic comparison between these two modes shows that the average travel time disparity is surprisingly similar across cities: R < 1 for travel distances less than 3 km, then increases rapidly but quickly stabilises at around 2. This study contributes to providing a more realistic performance evaluation that helps future studies further explore what city characteristics as well as urban and transport policies make public transport more attractive, and to create a more sustainable future for cities

The relationship between travel distance and travel time ratio R (PT travel time divided by the car travel time)

Wednesday, March 4, 2020

Six open positions in the Access to Opportunities Project



We are hiring six research assistants to work with me on the Access to Opportunities Project, at the Institute for Applied Economic Research (Ipea). Applications will be open until the 12th of March 2020. More details can be found here (info in Portuguese only).



We are looking for driven candidates with good data analysis skills in R and interested in evaluating policy impacts on issues of transport accessibility and urban inequalities.

Please, help spread us the word!

Tuesday, March 3, 2020

Perspectives and challenges of Smart (and inclusive) Cities in Brazil

I will be on a Facebook Live today discussing some of the challenges of developing Smart and inclusive (!) Cities in Brazil. The Live is being organized by the National Industry Confederation (CNI) and it starts at 5:30pm GMT on this link. The event will be conducted in Portuguese, but please feel free to drop in any comments and questions in any language you prefer.

You can find the recorded video below.






Monday, March 2, 2020

geobr v1.2 is on CRAN




geobr is an R package to download official spatial data sets of Brazil. It includes more than 20 data sets available at various geographic scales and for various years with harmonized attributes, projection and topology





News in this release: 
  • the package is much faster 
  • new data on metropolitan areas : read_metro_areas
  • new data on municipal seats (sede dos municipios) : read_municipal_seat
  • new fun to look up code of municipalities : lookup_muni
  • new fun to list all data sets available in geobr : list_geobr