-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtfs.cs
575 lines (447 loc) · 23.4 KB
/
gtfs.cs
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Musliw
{
public partial class gtfs : Form
{
public gtfs()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string rep = "";
if (folderBrowserDialog1.ShowDialog()==DialogResult.OK)
{
rep = folderBrowserDialog1.SelectedPath+"/";
this.Cursor = Cursors.WaitCursor;
//string rep="F:/offre_tc/Bordeaux/2011_2012/";
DateTime debut_cal, fin_cal;
debut_cal = dateTimePicker1.Value.Date;
fin_cal = dateTimePicker2.Value.Date;
string nom_musliw;// = rep + "test_musliw.txt";
Dictionary<string, Google_Stop> google_stops = lit_google_stops(rep + "stops.txt");
Dictionary<string, Google_Route> google_routes = lit_google_routes(rep + "routes.txt");
Dictionary<string, Google_Trip> google_trips = lit_google_trips(rep + "trips.txt");
Dictionary<string, List<Google_Calendar_Date>> google_calendar_dates = lit_google_calendar_dates(rep + "calendar_dates.txt");
Dictionary<string, Google_Calendar> google_calendars = lit_google_calendars(rep + "calendar.txt", debut_cal, fin_cal, google_calendar_dates);
Dictionary<string, SortedList<int, Google_Stop_Time>> google_stop_times = lit_google_stop_times(rep + "stop_times.txt");
Dictionary<string, List<Google_Trip>> google_chainages = cree_chainages(google_routes, google_trips, google_calendars, google_stop_times);
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
this.Cursor = Cursors.WaitCursor;
nom_musliw = saveFileDialog1.FileName;
cree_musliw(nom_musliw, google_routes, google_trips, google_calendars, google_stop_times, google_chainages, google_stops);
this.Cursor = Cursors.Default;
}
}
}
public Dictionary<string, Google_Stop> lit_google_stops(string nom_stops)
{
Dictionary<string, Google_Stop> google_stops = new Dictionary<string, Google_Stop>();
System.IO.StreamReader fichier_stops = new System.IO.StreamReader(nom_stops);
List<string> header = new List<string>((string[])(fichier_stops.ReadLine().Split(',')));
Dictionary<string, int> headers = new Dictionary<string, int>();
int ii;
for (ii = 0; ii < header.Count; ii++)
{
headers[header[ii].Trim('"')] = ii;
}
while (fichier_stops.EndOfStream == false)
{
string[] delim={"\""};
//List<string> head = new List<string>((string[])(fichier_stops.ReadLine()).Split(delim,StringSplitOptions.None));
List<string> elements =new List<string>((string[])System.Text.RegularExpressions.Regex.Split(fichier_stops.ReadLine(),",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)"));
for (int ch=0; ch< elements.Count;ch++)
{
elements[ch]=elements[ch].Replace("\"", "");
}
//string[] elements = chaine.Split(',');
Google_Stop google_stop = new Google_Stop();
google_stop.numero = elements[headers["stop_id"]];
google_stop.nom = elements[headers["stop_name"]];
if (System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator == ",")
{
google_stop.x = float.Parse(elements[headers["stop_lon"]].Replace(".", ","));
google_stop.y = float.Parse(elements[headers["stop_lat"]].Replace(".", ","));
}
else
{
google_stop.x = float.Parse(elements[headers["stop_lon"]]);
google_stop.y = float.Parse(elements[headers["stop_lat"]]);
}
google_stops[google_stop.numero] = google_stop;
}
fichier_stops.Close();
return google_stops;
}
public Dictionary<string, Google_Route> lit_google_routes(string nom_routes)
{
Dictionary<string, Google_Route> google_routes = new Dictionary<string, Google_Route>();
System.IO.StreamReader fichier_routes = new System.IO.StreamReader(nom_routes);
List<string> header = new List<string>((string[])(fichier_routes.ReadLine().Split(',')));
Dictionary<string, int> headers = new Dictionary<string, int>();
int ii;
for (ii = 0; ii < header.Count; ii++)
{
headers[header[ii].Trim('"')] = ii;
}
while (fichier_routes.EndOfStream == false)
{
List<string> h = new List<string>();
string[] delim = { "\"" };
List<string> head = new List<string>((string[])(fichier_routes.ReadLine()).Split(delim, StringSplitOptions.None));
foreach (string ch in head)
{
h.Add(ch.Replace(", ", "_ "));
}
string chaine = string.Join(@"", h.ToArray());
string[] elements = chaine.Split(',');
Google_Route google_route = new Google_Route();
google_route.numero = elements[headers["route_id"]];
if (headers.ContainsKey("route_short_name"))
{
google_route.nom = elements[headers["route_short_name"]];
}
else if (headers.ContainsKey("route_long_name"))
{
google_route.nom = elements[headers["route_long_name"]];
}
else
{
google_route.nom = " ";
}
google_routes[google_route.numero] = google_route;
}
fichier_routes.Close();
return google_routes;
}
public Dictionary<string, Google_Trip> lit_google_trips(string nom_trips)
{
Dictionary<string, Google_Trip> google_trips = new Dictionary<string, Google_Trip>();
System.IO.StreamReader fichier_trips = new System.IO.StreamReader(nom_trips);
List<string> header = new List<string>((string[])(fichier_trips.ReadLine().Split(',')));
Dictionary<string, int> headers = new Dictionary<string, int>();
int ii;
for (ii = 0; ii < header.Count; ii++)
{
headers[header[ii].Trim('"')] = ii;
}
while (fichier_trips.EndOfStream == false)
{
List<string> h = new List<string>();
string[] delim = { "\"" };
List<string> head = new List<string>((string[])(fichier_trips.ReadLine()).Split(delim, StringSplitOptions.None));
foreach (string ch in head)
{
h.Add(ch.Replace(", ", "_ "));
}
string chaine = string.Join(@"", h.ToArray());
string[] elements = chaine.Split(',');
Google_Trip google_trip = new Google_Trip();
google_trip.route_id = elements[headers["route_id"]].Trim();
google_trip.service_id = elements[headers["service_id"]].Trim();
google_trip.trip_id = elements[headers["trip_id"]].Trim();
google_trips[google_trip.trip_id] = google_trip;
}
fichier_trips.Close();
return google_trips;
}
public Dictionary<string, List<Google_Calendar_Date>> lit_google_calendar_dates(string nom_calendar_dates)
{
Dictionary<string, List<Google_Calendar_Date>> google_calendar_dates= new Dictionary<string,List<Google_Calendar_Date>>();
System.IO.StreamReader fichier_calendar_dates = new System.IO.StreamReader(nom_calendar_dates);
List<string> header = new List<string>((string[])(fichier_calendar_dates.ReadLine().Split(',')));
Dictionary<string, int> headers = new Dictionary<string, int>();
int ii;
for (ii = 0; ii < header.Count; ii++)
{
headers[header[ii].Trim('"')] = ii;
}
while (fichier_calendar_dates.EndOfStream == false)
{
List<string> h = new List<string>();
string[] delim = { "\"" };
List<string> head = new List<string>((string[])(fichier_calendar_dates.ReadLine()).Split(delim, StringSplitOptions.None));
foreach (string ch in head)
{
h.Add(ch.Replace(", ", "_ "));
}
string chaine = string.Join(@"", h.ToArray());
string[] elements = chaine.Split(',');
Google_Calendar_Date google_calendar_date = new Google_Calendar_Date();
google_calendar_date.date = new DateTime(int.Parse(elements[headers["date"]].Substring(0, 4)), int.Parse(elements[headers["date"]].Substring(4, 2)), int.Parse(elements[headers["date"]].Substring(6, 2)));
google_calendar_date.type = int.Parse(elements[headers["exception_type"]]);
string service_id = elements[headers["service_id"]];
if (google_calendar_dates.ContainsKey(service_id) == true)
{
google_calendar_dates[service_id].Add(google_calendar_date);
}
else
{
google_calendar_dates.Add(service_id,new List<Google_Calendar_Date>());
google_calendar_dates[service_id].Add(google_calendar_date);
}
}
fichier_calendar_dates.Close();
return google_calendar_dates;
}
public Dictionary<string, Google_Calendar> lit_google_calendars(string nom_calendars,DateTime debut_cal, DateTime fin_cal,Dictionary<string, List<Google_Calendar_Date>> google_calendar_dates)
{
Dictionary<string, Google_Calendar> google_calendars = new Dictionary<string, Google_Calendar>();
if (System.IO.File.Exists(nom_calendars))
{
System.IO.StreamReader fichier_calendars = new System.IO.StreamReader(nom_calendars);
List<string> header = new List<string>((string[])(fichier_calendars.ReadLine().Split(',')));
Dictionary<string, int> headers = new Dictionary<string, int>();
int ii;
for (ii = 0; ii < header.Count; ii++)
{
headers[header[ii].Trim('"')] = ii;
}
while (fichier_calendars.EndOfStream == false)
{
List<string> h = new List<string>();
string[] delim = { "\"" };
List<string> head = new List<string>((string[])(fichier_calendars.ReadLine()).Split(delim, StringSplitOptions.None));
foreach (string ch in head)
{
h.Add(ch.Replace(", ", "_ "));
}
string chaine = string.Join(@"", h.ToArray());
string[] elements = chaine.Split(',');
string[] toto = new string[8];
string service_id = elements[headers["service_id"]], calendrier = "";
Array.Copy(elements, 1, toto, 0, 7);
Google_Calendar google_calendar = new Google_Calendar();
google_calendar.semaine = string.Join("", toto);
google_calendar.debut = new DateTime(int.Parse(elements[headers["start_date"]].Substring(0, 4)), int.Parse(elements[headers["start_date"]].Substring(4, 2)), int.Parse(elements[headers["start_date"]].Substring(6, 2)));
google_calendar.fin = new DateTime(int.Parse(elements[headers["end_date"]].Substring(0, 4)), int.Parse(elements[headers["end_date"]].Substring(4, 2)), int.Parse(elements[headers["end_date"]].Substring(6, 2)));
int duree_cal = Math.Max((fin_cal - debut_cal).Days, 1);
DateTime jour = debut_cal;
String semaine = google_calendar.semaine.Substring(6, 1) + google_calendar.semaine.Substring(0, 6);
for (int i = 0; i <= duree_cal; i++)
{
int jour_semaine = (int)jour.DayOfWeek;
if (jour >= google_calendar.debut && jour <= google_calendar.fin && semaine[jour_semaine] == '1')
{
calendrier += "O";
}
else
{
calendrier += "N";
}
jour = jour.AddDays(1);
}
google_calendar.calendrier = calendrier;
//MessageBox.Show(google_calendar.debut.ToString());
google_calendars[service_id] = google_calendar;
}
fichier_calendars.Close();
}
foreach (string cal in google_calendar_dates.Keys)
{
if (google_calendars.ContainsKey(cal) == false)
{
int duree_cal=Math.Max((fin_cal - debut_cal).Days+1,1);
string cal_sem=new string('N',duree_cal);
Google_Calendar gc=new Google_Calendar();
gc.calendrier=cal_sem;
gc.debut=debut_cal;
gc.fin=fin_cal;
gc.semaine=new string('N',7);
google_calendars.Add(cal,gc);
}
if (google_calendar_dates.ContainsKey(cal))
{
foreach (Google_Calendar_Date caldate in google_calendar_dates[cal])
{
DateTime date_jour=caldate.date;
int typjour = caldate.type;
if (date_jour >= debut_cal && date_jour <= fin_cal)
{
int delta=(date_jour-debut_cal).Days;
if (typjour == 1)
{
google_calendars[cal].calendrier = google_calendars[cal].calendrier.Substring(0, delta) + "O" + google_calendars[cal].calendrier.Substring(delta + 1);
}
else if (typjour == 2)
{
google_calendars[cal].calendrier = google_calendars[cal].calendrier.Substring(0, delta) + "N" + google_calendars[cal].calendrier.Substring(delta + 1);
}
}
}
}
}
return google_calendars;
}
public Dictionary<string, SortedList<int,Google_Stop_Time> > lit_google_stop_times(string nom_stop_times)
{
Dictionary<string, SortedList<int,Google_Stop_Time> > google_stop_times = new Dictionary<string, SortedList<int,Google_Stop_Time>>();
System.IO.StreamReader fichier_stop_times = new System.IO.StreamReader(nom_stop_times);
List<string> header = new List<string>((string[])(fichier_stop_times.ReadLine().Split(',')));
Dictionary<string, int> headers = new Dictionary<string, int>();
int ii;
for (ii = 0; ii < header.Count; ii++)
{
headers[header[ii].Trim('"')] = ii;
}
while (fichier_stop_times.EndOfStream == false)
{
List<string> h = new List<string>();
string[] delim = { "\"" };
List<string> head = new List<string>((string[])(fichier_stop_times.ReadLine()).Split(delim, StringSplitOptions.None));
foreach (string ch in head)
{
h.Add(ch.Replace(", ", "_ "));
}
string chaine = string.Join(@"", h.ToArray());
string[] elements = chaine.Split(',');
Google_Stop_Time passage = new Google_Stop_Time();
passage.trip_id=elements[headers["trip_id"]];
string[] h1 = elements[headers["arrival_time"]].Split(':');
string[] h2 = elements[headers["departure_time"]].Split(':');
if (h1.Length > 1)
{
passage.heure_arr = float.Parse(h1[0]) * 60f + float.Parse(h1[1]) + float.Parse(h1[2]) / 60f;
passage.heure_dep = float.Parse(h2[0]) * 60f + float.Parse(h2[1]) + float.Parse(h2[2]) / 60f;
passage.num_arret=elements[headers["stop_id"]];
passage.num_ordre= int.Parse(elements[headers["stop_sequence"]]);
if (google_stop_times.ContainsKey(passage.trip_id) == false)
{
google_stop_times[passage.trip_id] = new SortedList<int, Google_Stop_Time>();
}
google_stop_times[passage.trip_id].Add(passage.num_ordre, passage);
}
//MessageBox.Show(DateTime.Parse(elements[1]).ToString());
}
fichier_stop_times.Close();
return google_stop_times;
}
public Dictionary<string, List<Google_Trip>> cree_chainages(Dictionary<string,Google_Route> google_routes, Dictionary<string,Google_Trip> google_trips, Dictionary<string,Google_Calendar> google_calendars, Dictionary<string,SortedList<int,Google_Stop_Time>> google_stop_times)
{
Dictionary<string, List<Google_Trip>> chainages = new Dictionary<string, List<Google_Trip>>();
foreach (string service in google_stop_times.Keys)
{
string chaine = "";
foreach (int num_ordre in google_stop_times[service].Keys)
{
chaine += google_stop_times[service][num_ordre].num_arret.ToString() + ";";
}
if (chainages.ContainsKey(chaine) == false)
{
chainages[chaine] = new List<Google_Trip>();
}
Google_Trip trip=new Google_Trip();
chainages[chaine].Add(google_trips[service]);
}
return chainages;
}
public void cree_musliw(string nom_musliw,Dictionary<string,Google_Route> google_routes, Dictionary<string,Google_Trip> google_trips,Dictionary<string,Google_Calendar> google_calendars, Dictionary<string,SortedList<int,Google_Stop_Time>> google_stop_times, Dictionary<string,List<Google_Trip>> google_chainages,Dictionary<string,Google_Stop> google_stops)
{
int i=0,n;
System.IO.StreamWriter fichier_musliw = new System.IO.StreamWriter(nom_musliw);
/* fichier_musliw.WriteLine("t nodes");
foreach (Google_Stop arret in google_stops.Values)
{
fichier_musliw.WriteLine(arret.numero.ToString() + ";" + arret.x.ToString() + ";" + arret.y.ToString());
}
fichier_musliw.WriteLine("t links");*/
foreach (string chaine in google_chainages.Keys)
{
i++;
int j = 0;
foreach (Google_Trip mission in google_chainages[chaine])
{
if (google_routes.ContainsKey(mission.route_id) == true)
{
SortedList<int, Google_Stop_Time> elements = google_stop_times[mission.trip_id];
n = elements.Count;
int k;
j++;
String textel = "";
for (k = 0; k < n - 1; k++)
{
if (elements.Values[k].heure_dep > elements.Values[k + 1].heure_dep)
{
elements.Values[k + 1].heure_dep+=1440f;
}
if (elements.Values[k].heure_arr > elements.Values[k + 1].heure_arr)
{
elements.Values[k + 1].heure_arr += 1440f;
}
if (google_stops.ContainsKey(elements.Values[k].num_arret) == false)
{
Google_Stop arret = new Google_Stop();
arret.nom = elements.Values[k].num_arret;
arret.x = 0;
arret.y = 0;
google_stops[elements.Values[k].num_arret] = arret;
}
if (google_stops.ContainsKey(elements.Values[k + 1].num_arret) == false)
{
Google_Stop arret = new Google_Stop();
arret.nom = elements.Values[k + 1].num_arret;
arret.x = 0;
arret.y = 0;
google_stops[elements.Values[k + 1].num_arret] = arret;
}
textel = textBox1.Text + elements.Values[k].num_arret.ToString();
textel += ";" + textBox1.Text + elements.Values[k + 1].num_arret.ToString();
textel += ";0;0";
textel += ";" + i.ToString() + ";" + j.ToString();
textel += ";" + elements.Values[k].heure_dep + ";" + elements.Values[k + 1].heure_arr;
textel += ";" + google_calendars[mission.service_id].calendrier + ";";
textel += google_routes[mission.route_id].nom + "|" + google_stops[elements.Values[k].num_arret].nom + "-" + google_stops[elements.Values[k + 1].num_arret].nom + ";0;0";
if (google_calendars[mission.service_id].calendrier.Split('O').Length > 1)
{
fichier_musliw.WriteLine(textel);
}
}
}
}
}
fichier_musliw.Close();
this.Cursor = Cursors.Default;
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
public class Google_Stop
{
public string numero, nom;
public float x, y;
}
public class Google_Route
{
public string numero,nom;
}
public class Google_Trip
{
public string route_id, service_id,trip_id;
}
public class Google_Calendar
{
public string semaine,calendrier;
public DateTime debut,fin;
}
public class Google_Calendar_Date
{
public DateTime date;
public int type;
}
public class Google_Stop_Time
{
public string trip_id, num_arret;
public float heure_arr, heure_dep;
public int num_ordre;
}
}