Skip to content

Instantly share code, notes, and snippets.

@rolandcrosby
Last active August 19, 2019 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rolandcrosby/902348ee645d0a65a3f66bfc3ec5de78 to your computer and use it in GitHub Desktop.
Save rolandcrosby/902348ee645d0a65a3f66bfc3ec5de78 to your computer and use it in GitHub Desktop.
CockroachDB 19.2 IMPORT INTO with GTFS data
-- IMPORT INTO for GTFS data (e.g. http://transitfeeds.com/p/mta/87)
create table agencies (
agency_id int primary key,
agency_name string,
agency_url string,
agency_timezone string,
agency_phone string,
agency_lang string
);
create table routes (
route_id int primary key,
agency_id int references agencies (agency_id),
route_short_name string,
route_long_name string,
route_desc string,
route_type int,
route_url string,
route_color string,
route_text_color string
);
create table stops (
stop_id int primary key,
stop_code string,
stop_name string,
stop_desc string,
stop_lat float,
stop_lon float,
zone_id string,
stop_url string,
location_type int,
parent_station int,
wheelchair_boarding boolean
);
create table notes (
note_id string primary key,
note_mark string,
note_title string,
note_description string
);
create table shapes (
shape_id int,
shape_pt_lat float,
shape_pt_lon float,
shape_pt_sequence int,
shape_dist_traveled float,
primary key (shape_id, shape_pt_sequence)
);
create table trips (
route_id int references routes (route_id),
service_id int,
trip_id int primary key,
trip_headsign string,
trip_short_name string,
direction_id int,
block_id string,
shape_id int,
wheelchair_accessible boolean,
peak_offpeak int
);
create table stop_times (
trip_id int references trips (trip_id),
arrival_time interval,
departure_time interval,
stop_id int,
stop_sequence int,
pickup_type int,
drop_off_type int,
track string,
note_id string references notes (note_id),
primary key (trip_id, stop_sequence)
);
import into agencies (
agency_id,
agency_name,
agency_url,
agency_timezone,
agency_phone,
agency_lang
) csv data (
'nodelocal:///agency.txt'
) with skip='1', nullif='';
import into routes (
route_id,
agency_id,
route_short_name,
route_long_name,
route_desc,
route_type,
route_url,
route_color,
route_text_color
) csv data (
'nodelocal:///routes.txt'
) with skip='1', nullif='';
import into stops (
stop_id,
stop_code,
stop_name,
stop_desc,
stop_lat,
stop_lon,
zone_id,
stop_url,
location_type,
parent_station,
wheelchair_boarding
) csv data (
'nodelocal:///stops.txt'
) with skip='1', nullif='';
import into notes (
note_id,
note_mark,
note_title,
note_description
) csv data (
'nodelocal:///notes.txt'
) with skip='1', nullif='';
import into shapes (
shape_id,
shape_pt_lat,
shape_pt_lon,
shape_pt_sequence,
shape_dist_traveled
) csv data (
'nodelocal:///shapes.txt'
) with skip='1', nullif='';
import into trips (
route_id,
service_id,
trip_id,
trip_headsign,
trip_short_name,
direction_id,
block_id,
shape_id,
wheelchair_accessible,
peak_offpeak
) csv data (
'nodelocal:///trips.txt'
) with skip='1', nullif='';
import into stop_times (
trip_id,
arrival_time,
departure_time,
stop_id,
stop_sequence,
pickup_type,
drop_off_type,
track,
note_id
) csv data (
'nodelocal:///stop_times.txt'
) with skip='1', nullif='';
-- All except stop_times to which access is denied
import into agencies (
agency_id,
agency_name,
agency_url,
agency_timezone,
agency_phone,
agency_lang
) csv data (
'https://openmobilitydata-data.s3-us-west-1.amazonaws.com/public/feeds/mta/87/20190818/original/agency.txt'
) with skip='1', nullif='';
import into routes (
route_id,
agency_id,
route_short_name,
route_long_name,
route_desc,
route_type,
route_url,
route_color,
route_text_color
) csv data (
'https://openmobilitydata-data.s3-us-west-1.amazonaws.com/public/feeds/mta/87/20190818/original/routes.txt'
) with skip='1', nullif='';
import into stops (
stop_id,
stop_code,
stop_name,
stop_desc,
stop_lat,
stop_lon,
zone_id,
stop_url,
location_type,
parent_station,
wheelchair_boarding
) csv data (
'https://openmobilitydata-data.s3-us-west-1.amazonaws.com/public/feeds/mta/87/20190818/original/stops.txt'
) with skip='1', nullif='';
import into notes (
note_id,
note_mark,
note_title,
note_description
) csv data (
'https://openmobilitydata-data.s3-us-west-1.amazonaws.com/public/feeds/mta/87/20190818/original/notes.txt'
) with skip='1', nullif='';
import into shapes (
shape_id,
shape_pt_lat,
shape_pt_lon,
shape_pt_sequence,
shape_dist_traveled
) csv data (
'https://openmobilitydata-data.s3-us-west-1.amazonaws.com/public/feeds/mta/87/20190818/original/shapes.txt'
) with skip='1', nullif='';
import into trips (
route_id,
service_id,
trip_id,
trip_headsign,
trip_short_name,
direction_id,
block_id,
shape_id,
wheelchair_accessible,
peak_offpeak
) csv data (
'https://openmobilitydata-data.s3-us-west-1.amazonaws.com/public/feeds/mta/87/20190818/original/trips.txt'
) with skip='1', nullif='';
rank stop_id stop_name stop_lat stop_lon trips min avg max
1 1 Grand Central 40.752998 -73.977056 5808 00:00:00 00:00:00 00:00:00
2 4 Harlem-125th St. 40.805157 -73.939149 5424 00:09:00 00:10:10 00:11:00
3 54 Melrose 40.825761 -73.915231 204 00:14:00 00:14:08 00:15:00
4 622 Yankees-E153 St. 40.8253 -73.9299 789 00:13:00 00:15:23 00:19:00
5 55 Tremont 40.847301 -73.89955 204 00:16:00 00:17:04 00:18:00
6 9 Morris Heights 40.854252 -73.919583 601 00:17:00 00:18:16 00:19:00
7 56 Fordham 40.8615 -73.89058 1921 00:18:00 00:18:28 00:21:00
8 10 University Heights 40.862248 -73.91312 601 00:19:00 00:20:14 00:21:00
9 57 Botanical Garden 40.866555 -73.883109 675 00:20:00 00:21:44 00:24:00
10 11 Marble Hill 40.874333 -73.910941 917 00:18:00 00:21:49 00:23:00
11 58 Williams Bridge 40.878569 -73.871064 698 00:21:00 00:24:11 00:27:00
12 14 Spuyten Duyvil 40.878245 -73.921455 845 00:22:00 00:24:21 00:26:00
13 105 Mt Vernon East 40.912161 -73.832185 922 00:24:00 00:26:48 00:28:00
14 59 Woodlawn 40.895361 -73.862916 710 00:21:00 00:27:04 00:29:00
15 16 Riverdale 40.903981 -73.914126 845 00:26:00 00:28:04 00:29:00
16 61 Wakefield 40.905936 -73.85568 678 00:24:00 00:29:49 00:31:00
17 106 Pelham 40.910321 -73.810242 886 00:27:00 00:29:50 00:31:00
18 62 Mt Vernon West 40.912142 -73.851129 1003 00:23:00 00:30:28 00:35:00
19 17 Ludlow 40.924972 -73.904612 647 00:28:00 00:30:48 00:32:00
20 18 Yonkers 40.935795 -73.902668 1071 00:25:00 00:32:10 00:35:00
21 108 New Rochelle 40.911605 -73.783807 970 00:27:00 00:33:02 00:36:00
21 64 Fleetwood 40.92699 -73.83948 955 00:25:00 00:33:02 00:37:00
23 65 Bronxville 40.93978 -73.835208 955 00:28:00 00:35:40 00:40:00
24 19 Glenwood 40.950496 -73.899062 647 00:33:00 00:35:47 00:37:00
25 110 Larchmont 40.933394 -73.759792 922 00:30:00 00:36:53 00:39:00
26 66 Tuckahoe 40.949393 -73.830166 955 00:30:00 00:37:42 00:43:00
27 20 Greystone 40.972705 -73.889069 659 00:28:00 00:38:57 00:41:00
28 111 Mamaroneck 40.954061 -73.736125 922 00:33:00 00:39:57 00:42:00
29 68 Crestwood 40.958997 -73.820564 1080 00:26:00 00:39:58 00:46:00
30 22 Hastings-on-Hudson 40.994109 -73.884512 951 00:31:00 00:40:17 00:44:00
31 71 Scarsdale 40.989168 -73.808634 924 00:29:00 00:42:33 00:56:00
32 112 Harrison 40.969432 -73.712964 970 00:33:00 00:42:56 00:47:00
33 23 Dobbs Ferry 41.012459 -73.87949 951 00:34:00 00:43:06 00:47:00
34 74 White Plains 41.032589 -73.775208 1621 00:33:00 00:44:14 01:05:00
35 24 Ardsley-on-Hudson 41.026198 -73.876543 695 00:36:00 00:45:55 00:49:00
36 72 Hartsdale 41.010333 -73.796407 924 00:32:00 00:46:02 01:00:00
37 78 Valhalla 41.072819 -73.772599 644 00:40:00 00:46:39 01:04:00
37 114 Rye 40.985922 -73.682553 994 00:34:00 00:46:39 00:50:00
39 25 Irvington 41.039993 -73.873083 951 00:38:00 00:47:03 00:52:00
40 27 Tarrytown 41.076473 -73.864563 1364 00:36:00 00:47:27 00:55:00
41 115 Port Chester 41.000732 -73.6647 994 00:37:00 00:49:40 00:54:00
42 79 Mount Pleasant 41.095877 -73.793822 45 00:46:00 00:50:20 00:53:00
43 80 Hawthorne 41.108581 -73.79625 736 00:44:00 00:50:50 01:08:00
44 76 North White Plains 41.049806 -73.773142 1525 00:36:00 00:51:36 01:12:00
45 116 Greenwich 41.021277 -73.624621 1126 00:41:00 00:52:27 00:57:00
46 29 Philipse Manor 41.09492 -73.869755 755 00:39:00 00:53:22 00:58:00
47 81 Pleasantville 41.135222 -73.792661 736 00:48:00 00:54:50 01:12:00
48 127 Noroton Heights 41.069041 -73.49788 627 00:52:00 00:56:28 01:17:00
49 31 Ossining 41.157663 -73.869281 1352 00:45:00 00:56:30 01:05:00
50 83 Chappaqua 41.158015 -73.774885 832 00:46:00 00:56:43 01:15:00
51 30 Scarborough 41.135763 -73.866163 837 00:43:00 00:56:58 01:02:00
52 118 Cos Cob 41.030171 -73.598306 922 00:44:00 00:57:56 01:02:00
53 37 Cortlandt 41.246259 -73.921884 567 00:52:00 00:59:53 01:10:00
54 128 Darien 41.076913 -73.472966 860 00:56:00 01:00:03 01:21:00
55 120 Riverside 41.031682 -73.588173 922 00:47:00 01:00:53 01:05:00
56 153 Glenbrook 41.070547 -73.520021 108 00:51:00 01:01:07 01:18:00
57 124 Stamford 41.046611 -73.542846 1991 00:47:00 01:02:11 01:30:00
58 33 Croton-Harmon 41.189903 -73.882394 1545 00:44:00 01:02:12 01:15:00
59 84 Mount Kisco 41.208242 -73.729778 844 00:51:00 01:02:57 01:21:00
60 129 Rowayton 41.077456 -73.445527 591 00:59:00 01:02:58 01:24:00
61 121 Old Greenwich 41.033817 -73.565859 922 00:49:00 01:03:16 01:07:00
62 154 Springdale 41.08876 -73.517828 108 00:54:00 01:04:07 01:21:00
63 39 Peekskill 41.285962 -73.93042 567 00:57:00 01:04:53 01:15:00
64 131 South Norwalk 41.09673 -73.421132 944 00:59:00 01:06:06 01:28:00
65 85 Bedford Hills 41.237316 -73.699936 712 00:55:00 01:06:43 01:25:00
66 155 Talmadge Hill 41.116012 -73.498149 108 00:58:00 01:08:13 01:25:00
67 133 East Norwalk 41.103996 -73.404588 615 01:01:00 01:09:17 01:31:00
68 86 Katonah 41.259552 -73.684155 760 00:58:00 01:09:57 01:29:00
69 134 Westport 41.118928 -73.371413 876 01:02:00 01:12:08 01:35:00
70 88 Goldens Bridge 41.294338 -73.677655 784 01:02:00 01:13:37 01:32:00
70 40 Manitou 41.332601 -73.970426 78 01:06:00 01:13:37 01:15:00
72 42 Garrison 41.38178 -73.947202 567 01:08:00 01:16:39 01:27:00
73 158 Merritt 7 41.146618 -73.427859 48 01:11:00 01:16:45 01:20:00
74 157 New Canaan 41.146305 -73.495626 108 01:08:00 01:17:13 01:34:00
75 89 Purdy's 41.325775 -73.659061 736 01:06:00 01:17:32 01:36:00
76 136 Green's Farms 41.122265 -73.315408 591 01:09:00 01:17:40 01:40:00
77 90 Croton Falls 41.347722 -73.662269 736 01:09:00 01:20:35 01:39:00
78 43 Cold Spring 41.415283 -73.95809 567 01:13:00 01:20:50 01:31:00
79 137 Southport 41.134844 -73.28897 603 01:12:00 01:21:53 01:45:00
80 160 Wilton 41.196202 -73.432434 48 01:17:00 01:22:45 01:26:00
81 138 Fairfield 41.143077 -73.257742 912 01:09:00 01:23:05 01:49:00
82 91 Brewster 41.39447 -73.619802 808 01:15:00 01:26:23 01:45:00
83 161 Cannondale 41.21662 -73.426703 48 01:21:00 01:26:45 01:30:00
84 188 Fairfield Metro 41.161 -73.234336 912 01:13:00 01:26:56 01:53:00
85 46 Beacon 41.504007 -73.984528 604 01:12:00 01:29:02 01:41:00
86 44 Breakneck Ridge 41.450181 -73.982449 66 01:30:00 01:30:00 01:30:00
87 97 Patterson 41.511827 -73.604584 48 01:29:00 01:32:30 01:35:00
88 162 Branchville 41.26763 -73.441421 48 01:28:00 01:33:45 01:37:00
89 140 Bridgeport 41.178677 -73.187076 936 01:16:00 01:34:07 02:01:00
90 94 Southeast 41.413203 -73.623787 784 01:20:00 01:36:47 01:56:00
91 49 New Hamburg 41.587448 -73.947226 578 01:20:00 01:37:36 01:56:00
92 98 Pawling 41.564205 -73.600524 48 01:35:00 01:38:30 01:41:00
93 143 Stratford 41.194255 -73.131532 912 01:23:00 01:40:33 02:07:00
94 163 Redding 41.325684 -73.4338 48 01:36:00 01:41:45 01:45:00
95 100 HM Valley-Wingdale 41.637525 -73.57145 48 01:43:00 01:46:15 01:49:00
96 145 Milford 41.223231 -73.057647 912 01:30:00 01:47:54 02:15:00
97 164 Bethel 41.376225 -73.418171 48 01:44:00 01:49:45 01:53:00
98 51 Poughkeepsie 41.705839 -73.937946 578 01:36:00 01:53:53 02:08:00
99 190 West Haven 41.27142 -72.963488 912 01:37:00 01:54:54 02:22:00
100 101 Dover Plains 41.740401 -73.576502 48 01:52:00 01:55:00 01:57:00
101 176 Tenmile River 41.779938 -73.558204 48 01:57:00 02:00:00 02:02:00
102 165 Danbury 41.396363 -73.450163 48 01:58:00 02:04:00 02:07:00
103 177 Wassaic 41.814722 -73.562197 48 02:05:00 02:07:45 02:10:00
104 149 New Haven 41.296501 -72.92829 912 01:49:00 02:08:14 02:35:00
105 151 NH-State St. 41.304979 -72.921747 60 02:08:00 02:12:18 02:18:00
SELECT
rank() OVER (ORDER BY avg), *
FROM
(
SELECT
stats.stop_id AS stop_id,
stop_name,
stop_lat,
stop_lon,
count(trip_id) AS trips,
min(time_from_gct::INT8)::INTERVAL,
round(avg(time_from_gct::INT8))::INTERVAL
AS avg,
max(time_from_gct::INT8)::INTERVAL
FROM
(
SELECT
trip_id,
stop_id,
arrival_time,
arrival_time
- min(departure_time) OVER (
PARTITION BY trip_id
)
AS time_from_gct
FROM
stop_times
WHERE
trip_id
IN (
SELECT
DISTINCT trip_id
FROM
stop_times
WHERE
stop_id = 1
AND stop_sequence = 1
)
)
AS stats
JOIN stops ON stats.stop_id = stops.stop_id
GROUP BY
stats.stop_id, stop_name, stop_lat, stop_lon
)
ORDER BY
rank ASC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment