-
Notifications
You must be signed in to change notification settings - Fork 1
/
GTFS2NeTEx-converter.py
281 lines (244 loc) · 15.8 KB
/
GTFS2NeTEx-converter.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from pathlib import Path
import sqlite3
import argparse
import textwrap
import os
import glob
import CheckMandatoryFilesComponent
import CreateExtraFramesElementsComponent
import CreateLoadDBComponent
import ResourceFrameComponent
import SiteFrameComponent
import ServiceCalendarFrameComponent
import ServiceFrameComponent
import TimetableFrameComponent
import SupportUtilitiesSubComponent
import gzip
import shutil
import datetime
import time
class StartConversionProcess():
parser=argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('--folder', dest='folder', action='store', required=True, help=textwrap.dedent('''\
Name of the GTFS expanded relative subfolder.
No blanks allowed.
Recommended examples for Liguria:
GTFS-RL-RT,
GTFS-RL-TPLLINEA,
GTFS-RL-AMT,
GTFS-RL-AMTEXT,
GTFS-RL-ATC,
GTFS-RL-TRENITALIA'''))
parser.add_argument('--NUTS', dest='nuts', action='store', required=True, help=textwrap.dedent('''\
Combination of country code/NUTS Level2.
Examples for Italy:
IT:ITC1 (Piemonte),
IT:ITC2 (Valle d''Aosta),
IT:ITC3 (Liguria),
IT:ITC4 (Lombardia),
IT:ITF1 (Abruzzo),
IT:ITF2 (Molise),
IT:ITF3 (Campania),
IT:ITF4 (Puglia),
IT:ITF5 (Basilicata),
IT:ITF6 (Calabria),
IT:ITG1 (Sicilia),
IT:ITG2 (Sardegna),
IT:ITH1 (Provincia Autonoma di Bolzano/Bozen),
IT:ITH2 (Provincia Autonoma di Trento),
IT:ITH3 (Veneto),
IT:ITH4 (Friuli-Venezia Giulia),
IT:ITH5 (Emilia-Romagna),
IT:ITI1 (Toscana),
IT:ITI2 (Umbria),
IT:ITI3 (Marche),
IT:ITI4 (Lazio),
IT:ITZZ (Extra-Regio NUTS 1) '''))
parser.add_argument('--db', dest='db', action='store', required=True, help=textwrap.dedent('''\
Name of the working database.
No blanks allowed.
Recommended examples for Liguria:
RT,
TPLLINEA,
AMT,
AMTEXT,
ATC,
TRENITALIA'''))
parser.add_argument('--az', dest='az', action='store', required=True, help=textwrap.dedent('''\
Acronym of the Transport Agency.
No blanks allowed.
Recommended examples for Liguria:
RT,
TPLLINEA,
AMT,
AMTEXT,
ATC,
TRENITALIA'''))
parser.add_argument('--vat', dest='vat', action='store', required=True, help=textwrap.dedent('''\
11-chars VAT Number (Partita IVA) of the Transport Agency.
No blanks allowed.
Examples of correct values:
12345678903,
44444444440,
null,
00000000000
Examples of wrong values:
123,
00000+00000,
xxxxxxxxxxxx,
123456789012
00000000001'''))
parser.add_argument('--version', dest='version', action='store', required=True, help=textwrap.dedent('''\
NeTEx file version for the Agency in the year:
reccomended syntax [yy][Agency acronym][2-digits progressive in the year]
Avoid blanks or special chars
Examples:
23AMT01,
23ATC02,
23TPERBO02
'''))
args=parser.parse_args()
#global iFolder, iNUTS, iDb, iAz, iStartDay, iEndDay, GTFS_exploded_feed_folder, iVersion
global iFolder, iNUTS, iDb, iAz, GTFS_exploded_feed_folder, iVAT, iVersion
iFolder=args.folder
iNUTS=args.nuts
iDb=args.db
iAz=args.az
iVAT=args.vat
iVersion=args.version
GTFS_exploded_feed_folder=iFolder
if os.path.exists(GTFS_exploded_feed_folder ):
print('Exploded GTFS feed folder is: ', GTFS_exploded_feed_folder)
else:
print('Cannot find a directory named: ', GTFS_exploded_feed_folder)
exit()
###############################################
VATNumber=iVAT
VATCheck=SupportUtilitiesSubComponent.StringUtilities
VATCheckClean=VATCheck.controllaPIVA(VATNumber)
if VATCheckClean == False:
exit()
else:
print('VAT number is well-formed.')
##############################################
CheckMandatoryFilesComponent_start=CheckMandatoryFilesComponent.CheckMandatoryFilesProcessing
CheckMandatoryFilesComponent_start.checkMandatoryFiles(GTFS_exploded_feed_folder)
Path(GTFS_exploded_feed_folder + os.sep +iDb + '.db').touch()
def sequenceComponents(self):
print("Start processing feed...")
iNUTSDash=iNUTS.replace(":","-")
# print("iNUTSDash: ", iNUTSDash)
startTime=time.time()
# importing GTFS files and loading support database
CreateLoadDB_component_start=CreateLoadDBComponent.StartImportProcess
CreateLoadDB_component_start.importGTFSFiles(self, GTFS_exploded_feed_folder, iDb)
conn=sqlite3.connect(GTFS_exploded_feed_folder + '/' +iDb + '.db')
c=conn.cursor()
# read start day (cut) and end day (cut) from
c.execute('''SELECT MIN(date_start_min), MAX(date_end_max) FROM srv_calendar_dates_summary''')
records=c.fetchall()
for row in records:
if(row[0]):
iStartDayCut=row[0]
print("iStartDayCut: ", iStartDayCut)
if(row[1]):
iEndDayCut=row[1]
print("iEndDayCut: ", iEndDayCut)
# read earliest day and latest day for synthetic calendar
c.execute('''SELECT MIN(candidate_date), MAX(candidate_date) FROM srv_dates_range''')
records=c.fetchall()
for row in records:
if(row[0]):
iStartDay0=row[0]
iStartDay=iStartDay0[0:4]+"-01-01"
print(">>>>>>>>>>>>>>>>>>>>>>>>>>> Earliest date for synthetic calendar: ", iStartDay0, " ======> ", iStartDay)
if(row[1]):
iEndDay0=row[1]
iEndDay=iEndDay0[0:4]+"-12-31"
print(">>>>>>>>>>>>>>>>>>>>>>>>>>> Latest date for synthetic calendar: ", iEndDay0, " ======> ", iEndDay)
print("NeTEx version for the Agency: ", iVersion)
# create extra-frames element files
CreateExtraFramesElements_component_start=CreateExtraFramesElementsComponent.CreateExtraFramesElementsProcessing
CreateExtraFramesElements_component_start.processExtraFramesElements(self, GTFS_exploded_feed_folder, iNUTS, iAz, iStartDay, iEndDay, iStartDayCut, iEndDayCut)
# creating NeTEx ResourceFrame
ResourceFC_component_start=ResourceFrameComponent.ResourceFrameProcessing
ResourceFC_component_start.processResourceFrame(self, GTFS_exploded_feed_folder, iNUTS, iDb, iAz, iStartDayCut, iEndDayCut, iVersion, iVAT)
# creating NeTEx SiteFrame
SiteFC_component_start=SiteFrameComponent.SiteFrameProcessing
SiteFC_component_start.processSiteFrame(self, GTFS_exploded_feed_folder, iNUTS, iDb, iAz, iVersion)
# creating NeTEx ServiceCalendarFrame
ServiceCalendarFC_component_start=ServiceCalendarFrameComponent.ServiceCalendarFrameProcessing
ServiceCalendarFC_component_start.processServiceCalendarFrame(self, GTFS_exploded_feed_folder, iNUTS, iDb, iAz, iStartDay, iEndDay, iStartDayCut, iEndDayCut, iVersion)
# creating NeTEx ServiceFrame
ServiceFC_component_start=ServiceFrameComponent.ServiceFrameProcessing
ServiceFC_component_start.processServiceFrame(self, GTFS_exploded_feed_folder, iNUTS, iDb, iAz, iVersion, iVAT)
#creating NeTEx TimetableFrame
TimetableFC_component_start=TimetableFrameComponent.TimetableFrameProcessing
TimetableFC_component_start.processTimetableFrame(self, GTFS_exploded_feed_folder, iNUTS, iDb, iAz, iVersion, iVAT)
# sequencing the temporary XML files in a single output NeTEx (XML) file
dataFull=""
dataPreXMLText=""
dataResourceFrameXMLText=""
dataSiteFrameXMLText=""
dataServiceCalendarFrameXMLText=""
dataServiceFrameXMLText=""
dataTimetableFrameXMLText=""
dataPostXMLText=""
tempExtraFramesElementsPreXMLData=GTFS_exploded_feed_folder + os.sep + iAz + '-ExtraFramesPreXMLText-temp.xml'
tempResourceFrameXMLData=GTFS_exploded_feed_folder + os.sep + iAz + '-ResourceFrame-temp.xml'
tempSiteFrameXMLData=GTFS_exploded_feed_folder + os.sep + iAz + '-SiteFrame-temp.xml'
tempServiceCalendarFrameXMLData=GTFS_exploded_feed_folder + os.sep + iAz + '-ServiceCalendarFrame-temp.xml'
tempServiceFrameXMLData=GTFS_exploded_feed_folder + os.sep + iAz + '-ServiceFrame-temp.xml'
tempTimetableFrameXMLData=GTFS_exploded_feed_folder + os.sep + iAz + '-TimetableFrame-temp.xml'
tempExtraFramesElementsPostXMLData=GTFS_exploded_feed_folder + os.sep + iAz + '-ExtraFramesPostXMLText-temp.xml'
finalXMLFile=GTFS_exploded_feed_folder + os.sep + iNUTSDash + '-' + iAz + '-NeTEx_L1.xml'
finalGZFile=finalXMLFile + '.gz'
with open(tempExtraFramesElementsPreXMLData) as fp:
dataPreXMLText=fp.read()
with open(tempResourceFrameXMLData) as fp:
dataResourceFrameXMLText=fp.read()
with open(tempSiteFrameXMLData) as fp:
dataSiteFrameXMLText=fp.read()
with open(tempServiceCalendarFrameXMLData) as fp:
dataServiceCalendarFrameXMLText=fp.read()
with open(tempServiceFrameXMLData) as fp:
dataServiceFrameXMLText=fp.read()
with open(tempTimetableFrameXMLData) as fp:
dataTimetableFrameXMLText=fp.read()
with open(tempExtraFramesElementsPostXMLData) as fp:
dataPostXMLText=fp.read()
print("Queing temporary files...")
#dataFull += '\n'
dataFull += dataPreXMLText + '\n'
dataFull += dataResourceFrameXMLText + '\n'
dataFull += dataSiteFrameXMLText + '\n'
dataFull += dataServiceCalendarFrameXMLText + '\n'
dataFull += dataServiceFrameXMLText + '\n'
dataFull += dataTimetableFrameXMLText + '\n'
dataFull += dataPostXMLText
with open(finalXMLFile, 'w', encoding='utf-8') as fp:
fp.write(dataFull)
with open(finalXMLFile, 'rb') as f_in:
with gzip.open(finalGZFile, 'wb') as f_out:
print("Compressing XML file...")
shutil.copyfileobj(f_in, f_out)
# clean up temporary files
print("Clean up temporary xml files...")
fileList=glob.glob(GTFS_exploded_feed_folder + os.sep + '*-temp.xml')
for filePath in fileList:
try:
os.remove(filePath)
except:
print("Error while deleting file : ", filePath)
endTime=time.time()
tempTime=endTime-startTime
hours=tempTime//3600
tempTime=tempTime-3600*hours
minutes=tempTime//60
seconds=tempTime-60*minutes
print("Time elapsed: "+ '%02d:%02d:%02d' %(hours,minutes,seconds))
def main():
app=StartConversionProcess()
app.sequenceComponents()
if __name__ == '__main__':
main()