Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerize ridership map computations #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Dockerize ridership map computations
  • Loading branch information
thcrock committed Apr 9, 2016
commit 96b9ae89bee283d67ab459971d9e6d78a4cd1f4c
34 changes: 34 additions & 0 deletions ridership_map/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
FROM debian:sid

MAINTAINER Tristan Crockett <tristan.h.crockett@gmail.com>

RUN groupadd -r transit && useradd -r -g transit transit

RUN \
apt-get update && \
apt-get install -y wget unzip git
RUN mkdir /app
RUN mkdir /app/data && chown transit:transit /app/data
VOLUME /app/data
WORKDIR /app
RUN wget http://gtfs.s3.amazonaws.com/chicago-transit-authority_20121108_0426.zip
RUN unzip chicago-transit-authority_20121108_0426.zip

RUN apt-get install -y postgresql-client
RUN apt-get install -y python2.7
RUN apt-get install -y python-psycopg2
RUN apt-get install -y python-geojson

RUN git clone https://github.com/thcrock/gtfs_SQL_IMPORTER.git

COPY import.sh ./
COPY create_foia_table.sql ./
COPY create_segment_ridership.sql ./
COPY join_riders_using_gtfs.py ./
COPY create_geojson.py ./
COPY run.sh ./

RUN chmod +x import.sh
RUN chmod +x run.sh
RUN wget https://raw.githubusercontent.com/chihacknight/BetterTransit/master/ridership-data/CTA%20original%20bus%20ridership%20data%20for%20October%202012.csv
RUN sed -i -e "1d" 'CTA original bus ridership data for October 2012.csv'
11 changes: 11 additions & 0 deletions ridership_map/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
The ridership map's input data computation process is now automated using Docker. The steps to run are:

- Install Docker ( https://docs.docker.com/engine/installation/ )
- Install Docker-Compose ( https://docs.docker.com/compose/install/ )
- `cd ridership_map`
- `mkdir data`
- `chmod 777 data`
- `docker-compose build` (the path to the docker-compose binary may be different than this, depending on how you installed)
- `docker-compose up`

Both of the last two steps will take a while, as many dependencies will be downloaded and installed during the 'build' command, and a lot of computation will happen during the 'up' command. You should see progress scroll by in your terminal window. Eventually, it should finish and you will have a new .geojson file in your data directory that the map will use.
17 changes: 17 additions & 0 deletions ridership_map/create_foia_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
create table if not exists foia_data (
stop_id varchar,
on_street varchar,
cross_street varchar,
route varchar,
direction varchar,
boardings float,
alightings float,
month_beginning varchar,
daytype varchar,
lat varchar,
lon varchar
);
create index foia_direction on foia_data(direction);
create index foia_route on foia_data(route);
create index foia_stop on foia_data(stop_id);
COPY foia_data from '/app/CTA original bus ridership data for October 2012.csv';
2 changes: 1 addition & 1 deletion ridership_map/create_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def get_data(cursor):
return list(reversed(cursor.fetchall()))

if __name__ == '__main__':
conn = psycopg2.connect(database='transit', user='transit')
conn = psycopg2.connect(database='transit', user='postgres', host='db')
cursor = conn.cursor()
results = get_data(cursor)
features = []
Expand Down
10 changes: 10 additions & 0 deletions ridership_map/create_segment_ridership.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
create table if not exists segment_ridership(
begin_lon varchar,
begin_lat varchar,
end_lon varchar,
end_lat varchar,
route varchar,
direction varchar,
riders integer,
primary key (begin_lon, begin_lat, end_lon, end_lat, route, direction)
);
2 changes: 1 addition & 1 deletion ridership_map/data/citywide.geojson

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions ridership_map/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
db:
image: postgres:9.4
ports:
- "5432"
compute:
build: .
stdin_open: true
tty: true
entrypoint: /app/run.sh
volumes:
- ./data:/app/data
links:
- db
8 changes: 8 additions & 0 deletions ridership_map/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
cd gtfs_SQL_IMPORTER/src
echo 'CREATE DATABASE transit' | psql -h db -U postgres
cat gtfs_tables.sql \
<(python2.7 import_gtfs_to_sql.py /app) \
gtfs_tables_makeindexes.sql \
vacuumer.sql \
| psql -h db -U postgres transit
2 changes: 1 addition & 1 deletion ridership_map/join_riders_using_gtfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import geojson
import argparse

conn = psycopg2.connect(database='transit', user='transit')
conn = psycopg2.connect(database='transit', user='postgres', host='db')
cursor = conn.cursor()


Expand Down
8 changes: 8 additions & 0 deletions ridership_map/run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

./import.sh
cat create_foia_table.sql | psql -h db -U postgres transit
cat CTA\ original\ bus\ ridership\ data\ for\ October\ 2012.csv | psql -h db -U postgres -c 'COPY foia_data from stdin csv' transit
cat create_segment_ridership.sql | psql -h db -U postgres transit
python2.7 join_riders_using_gtfs.py
python2.7 create_geojson.py