<file_sep># Portland Transit
Animating GTFS data in Portland, Oregon

This repository contains the code and data used to create the below animation:

![img](https://media.giphy.com/media/3o7bu6LaJBftC8TOuI/giphy.gif)

(It runs more smoothly in video format).

1) The `Data Wrangling` python notebook is used to download and manipulate the GTFS data into a form necessary for animation

2) The `gtfs` folder contains the raw gtfs that was downloaded using the python notebook

3) The `data.zip` file contains the clean csv that was generated by the python notebook

4) The `sketch` folder contains the Processing code used to create the animation

To run this locally:

* Download the repo
* Unzip the data.zip file
* Open the processing sketch and click play. 

You will need to have [Processing](https://processing.org/) and [Unfolding Maps](http://unfoldingmaps.org/) installed to play sketch. 

For the steps taken to download and manipulate the data, see `Data_Wrangling.ipynb`.
<file_sep>

###### Load libraries ------------

library(data.table)
library(magrittr)
library(dplyr)
library(sf)
library(sp)
library(rgdal)
library(ggplot2)
library(ggthemes)
library(gganimate)



###### Load GTFS data ------------

# Indicate GTFS file
  gtfs_file <- "./data.zip"

# read GTFS text files
  routes <- fread(unzip (gtfs_file, "routes.txt"))         # get route_id
  trips <- fread(unzip (gtfs_file, "trips.txt"))           # get trip_id
  stop_times <- fread(unzip (gtfs_file, "stop_times.txt")) # get stop_id
  stops <- fread(unzip (gtfs_file, "stops.txt"))           # get stop_id + lat long
  shapes <- fread(unzip (gtfs_file, "shapes.txt"))         # get stop_id + lat long

# convert coordinates to numeric
  shapes[, shape_pt_lat := as.numeric(shape_pt_lat) ][, shape_pt_lon := as.numeric(shape_pt_lon) ]
  stops[, stop_lon := as.numeric(stop_lon) ][, stop_lat := as.numeric(stop_lat) ]


###### Find locations of stop_id along route shapes ------------

# Convert stops and shapes into sf format

 # stops
   coordinates(stops) = ~stop_lon+stop_lat
   stops_sf = st_as_sf(stops)
 # shapes
   coordinates(shapes) = ~shape_pt_lon+shape_pt_lat
   shapes_sf = st_as_sf(shapes)


# assign CRS / spatial projection
   st_crs(stops_sf) <- st_crs("+proj=longlat +ellps=WGS84")
   st_crs(shapes_sf) <- st_crs("+proj=longlat +ellps=WGS84")

  # change projection to utm meters
    stops_sf <- st_transform(stops_sf, "+proj=utm +ellps=WGS84 +units=m")
    shapes_sf <- st_transform(shapes_sf, "+proj=utm +ellps=WGS84 +units=m")


# Creater a 10 meter buffer around each stop
   stops_buffer <- st_buffer(stops_sf, dist=10 )

# Overlay stop buffers with lat/long points of route shapes



###### Bind all datasets together ------------

total_df <- left_join(stop_times, stops, by="stop_id")
total_df <- left_join(total_df, trips, by="trip_id")
total_df <- left_join(total_df, routes, by="route_id") %>% setDT()


###### Interpolate time between arrival times for each route/trip/direction ------------

data.table







