コード例 #1
0
        /// <summary>
        /// Initializes object using GTFS data feed.
        /// </summary>
        /// <param name="calendar">Calendar.</param>
        /// <param name="calendarDates">Calendar Dates.</param>
        public GtfsCalendarDates(System.IO.StreamReader calendarDates, Calendar calendar)
        {
            // Get order of field names.
            string[] fieldNames          = calendarDates.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("service_id"))
            {
                throw new FormatException("Service ID field name missing.");
            }
            if (!dic.ContainsKey("date"))
            {
                throw new FormatException("Date field name missing.");
            }
            if (!dic.ContainsKey("exception_type"))
            {
                throw new FormatException("Exception type field name missing.");
            }

            while (!calendarDates.EndOfStream)
            {
                IList <string> tokens = GtfsDataFeed.SplitGtfs(calendarDates.ReadLine());

                Calendar.Service service = null;

                try
                {
                    service = calendar[tokens[dic["service_id"]]];
                }
                catch
                {
                    DataFeed.LogError($"Preprocessor tried to parse an exception, but the service with ID { tokens[dic["service_id"]] } does not exist. Skipping this item to recover the parsing process.");
                    continue;
                }

                bool type = tokens[dic["exception_type"]] == "1" ? true : false;

                service.ExtraordinaryEvents.Add(new Tuple <string, bool>(tokens[dic["date"]], type));

                ExtraordinaryEvent ev = new ExtraordinaryEvent(service, tokens[dic["date"]], type);

                list.Add(ev);
            }
            calendarDates.Dispose();
        }
コード例 #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();
        }