-
Notifications
You must be signed in to change notification settings - Fork 0
/
createSchema.py
executable file
·191 lines (147 loc) · 5.08 KB
/
createSchema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#!/usr/bin/python
#
#Run like createSchema.py localhost gtfszipfile.ziplocal
import os
from os import sys
import zipfile
import MySQLdb
import csv
#types of each table: s for string, i for any number
tableFieldTypes = {'agency':['pk','s', 's', 's', 's', 's'],
'calendar_dates':['i', 's', 'i'],
'routes': ['i','s','s','s','i','s','s'],
'shapes':['i','i','i','i','i'],
'stop_times':['i','s','s','i','i','i','i','s'],
'stops':['i','s','s','s','i','i','i'],
'trips':['i','i','i','s','i','s','i']
}
def connectToDB():
#return MySQLdb.connect(host='njtransit.availabs.org', user='njtransit', passwd='am1238wk')
db = MySQLdb.connect(user='root', passwd='am1238wk')
db.autocommit(True)
return db
def populateSchema(db):
cursor = db.cursor()
cursor.connection.autocommit(True)
for tableName,fieldTypes in tableFieldTypes.iteritems():
print tableName
populateTable(cursor, tableName, fieldTypes)
def populateTable(cursor, tableName, fieldTypes):
rows = ''
f = open(tableName + '.txt', 'r')
f.readline() #throw away first line
rowNum = 1
with open(tableName + '.txt', 'rb') as f:
next(f)
for line in csv.reader(f, strict=True):
if len(rows) + len(''.join(line)) > 90000: #this is so we don't have statements that are too big
executeInsertStatement(cursor, tableName, rows)
rows = ''
i = 0
row = '('
for field in line: #create row tuple
field = '\\\''.join(field.split('\'')) #escape quote
if fieldTypes[i] == 's':
row += '\'' + field.strip() + '\','
elif fieldTypes[i] == 'i':
row += field.strip() + ','
elif fieldTypes[i] == 'pk':
row += str(rowNum) + ','
i += 1
row = row.rstrip(',') + '),' #remove last comma
rows += row
rowNum += 1
executeInsertStatement(cursor, tableName, rows)
def executeInsertStatement(cursor, tableName, rows):
statement = "INSERT INTO " + tableName + " VALUES " + rows.rstrip(",") + ";"
try:
print statement
cursor.execute(statement)
except:
print statement
raise
def createSchema(db, schemaName):
cursor = db.cursor()
cursor.execute("DROP DATABASE IF EXISTS GTFS_" + schemaName + ";")
cursor.execute("CREATE DATABASE IF NOT EXISTS GTFS_" + schemaName)
db.select_db('GTFS_' + schemaName)
cursor.execute("""
CREATE TABLE `agency` (
`agency_id` int(11) NOT NULL DEFAULT '0',
`agency_name` varchar(255) DEFAULT NULL,
`agency_url` varchar(255) DEFAULT NULL,
`agency_timezone` varchar(255) DEFAULT NULL,
`agency_lang` varchar(255) DEFAULT NULL,
`agency_phone` varchar(255) DEFAULT NULL,
PRIMARY KEY (`agency_id`)
);
CREATE TABLE `calendar_dates` (
`service_id` int(11) DEFAULT NULL,
`date` varchar(255) DEFAULT NULL,
`exception_type` int(2) DEFAULT NULL
);
CREATE TABLE `routes` (
`route_id` int(11) NOT NULL DEFAULT '0',
`agency_id` varchar(255) DEFAULT NULL,
`route_short_name` varchar(255) DEFAULT NULL,
`route_long_name` varchar(255) DEFAULT NULL,
`route_type` int(2) DEFAULT NULL,
`route_url` varchar(255) DEFAULT NULL,
`route_color` varchar(255) DEFAULT NULL,
PRIMARY KEY (`route_id`)
);
CREATE TABLE `shapes` (
`shape_id` int(11) DEFAULT NULL,
`shape_pt_lat` float(10,6) DEFAULT NULL,
`shape_pt_lon` float(10,6) DEFAULT NULL,
`shape_pt_sequence` int(11) DEFAULT NULL,
`shape_dist_traveled` float(10,6) DEFAULT NULL,
UNIQUE KEY `singleShape` (`shape_id`,`shape_pt_sequence`)
);
CREATE TABLE `stop_times` (
`trip_id` int(11) DEFAULT NULL,
`arrival_time` time DEFAULT NULL,
`departure_time` time DEFAULT NULL,
`stop_id` int(11) DEFAULT NULL,
`stop_sequence` int(11) DEFAULT NULL,
`pickup_type` int(2) DEFAULT NULL,
`drop_off_type` int(2) DEFAULT NULL,
`shape_dist_traveled` varchar(255) DEFAULT NULL
);
CREATE TABLE `stops` (
`stop_id` int(11) NOT NULL DEFAULT '0',
`stop_code` varchar(255) DEFAULT NULL,
`stop_name` varchar(255) DEFAULT NULL,
`stop_desc` varchar(255) DEFAULT NULL,
`stop_lat` decimal(8,6) DEFAULT NULL,
`stop_lon` decimal(8,6) DEFAULT NULL,
`zone_id` int(11) DEFAULT NULL,
PRIMARY KEY (`stop_id`)
);
CREATE TABLE `trips` (
`route_id` int(11) DEFAULT NULL,
`service_id` int(11) DEFAULT NULL,
`trip_id` int(11) NOT NULL DEFAULT '0',
`trip_headsign` varchar(255) DEFAULT NULL,
`direction_id` tinyint(1) DEFAULT NULL,
`block_id` varchar(255) DEFAULT NULL,
`shape_id` int(11) DEFAULT NULL,
PRIMARY KEY (`trip_id`)
);
""")
cursor.close()
def main(argv):
if len(argv) != 3:
raise RuntimeError('Not enough arguments')
db = connectToDB()
createSchema(db, argv[2])
# try:
# os.mkdir(chdir[1][:-4])
# except OSError:
# pass
#os.chdir(argv[1][:-4])
zipfile.ZipFile(argv[1]).extractall()
populateSchema(db)
db.close()
if __name__ == "__main__":
main(sys.argv)