/// <summary> /// Initializes object using GTFS data feed. /// </summary> /// <param name="trips">Trips.</param> /// <param name="routesInfo">Routes Info.</param> public GtfsRoutes(Trips trips, RoutesInfo routesInfo) { foreach (var trip in trips) { List <Stops.Stop> stopsSequence = new List <Stops.Stop>(); foreach (var stopTime in trip.Value.StopTimes) { stopsSequence.Add(stopTime.Stop); } Route r = new Route(Count, trip.Value.RouteInfo, stopsSequence, trip.Value.Headsign); if (!list.Contains(r)) { list.Add(r); } else { r = list.Find(x => x.Equals(r)); } trip.Value.Route = r; r.Trips.Add(trip.Value); } }
/// <summary> /// Merges two collections into one. /// </summary> /// <param name="other">The other collection that should be merged.</param> public void MergeCollections(Trips other) { foreach (var item in other) { var trip = item.Value; trip.ID = Count; // Reindex the item. string key; while (list.ContainsKey(key = DataFeed.RandomString())) { ; // We can index the item using some random string, since this identificator is only used while initialization. Both data are already initialized. } list.Add(key, trip); } other = null; }
/// <summary> /// Creates data feed that is required for the application. /// </summary> /// <param name="path">Path to the folder that will be the feed saved to.</param> public void CreateDataFeed(string path) { if (Directory.Exists(path)) { Directory.Delete(path, true); } Directory.CreateDirectory(path); Trips.Write(new StreamWriter(path + "/trips.tfd")); // This MUST come first because of trip reindexation (based on sorting). Stations.Write(new StreamWriter(path + "/stations.tfd")); // This MUST come first because of stations reindexation (based on sorting). Calendar.Write(new StreamWriter(path + "/calendar.tfd")); CalendarDates.Write(new StreamWriter(path + "/calendar_dates.tfd")); RoutesInfo.Write(new StreamWriter(path + "/routes_info.tfd")); Stops.Write(new StreamWriter(path + "/stops.tfd")); Footpaths.Write(new StreamWriter(path + "/footpaths.tfd")); StopTimes.Write(new StreamWriter(path + "/stop_times.tfd")); Routes.Write(new StreamWriter(path + "/routes.tfd")); using (var expiration = new StreamWriter(path + "/expires.tfd")) expiration.Write(ExpirationDate.ToString("yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture)); }
/// <summary> /// Initializes object using GTFS data feed. /// </summary> /// <param name="stops">Stops.</param> /// <param name="stopTimes">Stop Times.</param> /// <param name="trips">Trip.</param> public GtfsStopTimes(System.IO.StreamReader stopTimes, Trips trips, Stops stops) { // Get order of field names. string[] fieldNames = stopTimes.ReadLine().Split(','); Dictionary <string, int> dic = new Dictionary <string, int>(); for (int i = 0; i < fieldNames.Length; i++) { dic.Add(fieldNames[i].Replace("\"", ""), i); } // These fields are required for our purpose. if (!dic.ContainsKey("arrival_time")) { throw new FormatException("Arrival time field name missing."); } if (!dic.ContainsKey("departure_time")) { throw new FormatException("Departure time field name missing."); } if (!dic.ContainsKey("trip_id")) { throw new FormatException("Trip ID field name missing."); } if (!dic.ContainsKey("stop_id")) { throw new FormatException("Stop ID field name missing."); } while (!stopTimes.EndOfStream) { IList <string> tokens = GtfsDataFeed.SplitGtfs(stopTimes.ReadLine()); Trips.Trip trip = null; try { trip = trips[tokens[dic["trip_id"]]]; } catch { DataFeed.LogError($"Preprocessor tried to parse a stop-time, but the trip with ID { tokens[dic["trip_id"]] } does not exist. Skipping this item to recover the parsing process."); continue; } if (trip.StopTimes.Count == 0) // Set departure time of the trip. { trip.DepartureTime = ConvertTimeToSecondsSinceMidnight(tokens[dic["departure_time"]]); } Stops.Stop stop = null; try { stop = stops[tokens[dic["stop_id"]]]; } catch { DataFeed.LogError($"Preprocessor tried to parse a stop-time to the trip { trip.ID }, line { trip.RouteInfo.ShortName } in direction to { trip.Headsign }, but the stop with ID { tokens[dic["stop_id"]] } does not exist. Skipping this item to recover the parsing process."); continue; } StopTime st = new StopTime(trip, ConvertTimeToSecondsSinceMidnight(tokens[dic["arrival_time"]]) - trip.DepartureTime, ConvertTimeToSecondsSinceMidnight(tokens[dic["departure_time"]]) - trip.DepartureTime, stop); st.Trip.StopTimes.Add(st); if (!st.Stop.ThroughgoingRoutes.Contains(st.Trip.RouteInfo)) { st.Stop.ThroughgoingRoutes.Add(st.Trip.RouteInfo); } list.Add(st); } stopTimes.Dispose(); }