Пример #1
0
 /// <summary>
 /// Merges two collections into one.
 /// </summary>
 /// <param name="other">The other collection that should be merged.</param>
 public void MergeCollections(RoutesInfo other)
 {
     foreach (var item in other)
     {
         var info = item.Value;
         info.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, info);
     }
     other = null;
 }
Пример #2
0
        /// <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();
        }