Example #1
0
        /// <summary>
        /// Initializes object using GTFS data feed.
        /// </summary>
        /// <param name="routesInfo">Routes Info.</param>
        public GtfsRoutesInfo(System.IO.StreamReader routesInfo)
        {
            // Get order of field names.
            string[] fieldNames          = routesInfo.ReadLine().Split(',');
            Dictionary <string, int> dic = new Dictionary <string, int>();

            for (int i = 0; i < fieldNames.Length; i++)
            {
                dic.Add(fieldNames[i].Replace("\"", ""), i);
            }

            bool useDefaultColor = !dic.ContainsKey("route_text_color");

            // These fields are required for our purpose.
            if (!dic.ContainsKey("route_id"))
            {
                throw new FormatException("Route ID field name missing.");
            }
            if (!dic.ContainsKey("route_short_name"))
            {
                throw new FormatException("Route short name field name missing.");
            }
            if (!dic.ContainsKey("route_long_name"))
            {
                throw new FormatException("Route long name field name missing.");
            }
            if (!dic.ContainsKey("route_type"))
            {
                throw new FormatException("Route type field name missing.");
            }
            if (!dic.ContainsKey("route_color"))
            {
                throw new FormatException("Route color field name missing.");
            }

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

                int intType = int.Parse(tokens[dic["route_type"]]);

                if (intType == 800)
                {
                    intType = 3;                                 // Trolleybus converted to bus (Palmovka - Letňany). Temporary solution.
                }
                if (!(intType >= 0 && intType <= 7))
                {
                    throw new FormatException("Invalid mean of transport.");
                }

                RouteInfo.RouteType type = (RouteInfo.RouteType)(1 << intType);

                RouteInfo routeInfo = new RouteInfo(Count, tokens[dic["route_short_name"]], tokens[dic["route_long_name"]], type, tokens[dic["route_color"]], useDefaultColor ? string.Empty : tokens[dic["route_text_color"]]);

                list.Add(tokens[dic["route_id"]], routeInfo);
            }
            routesInfo.Dispose();
        }
Example #2
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();
        }
Example #3
0
        /// <summary>
        /// Initializes object using GTFS data feed.
        /// </summary>
        /// <param name="routesInfo">Routes Info.</param>
        /// <param name="services">Services.</param>
        /// <param name="trips">Trips.</param>
        public GtfsTrips(System.IO.StreamReader trips, Calendar services, RoutesInfo routesInfo)
        {
            // Get order of field names.
            string[] fieldNames          = trips.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("route_id"))
            {
                throw new FormatException("Route ID field name missing.");
            }
            if (!dic.ContainsKey("service_id"))
            {
                throw new FormatException("Service ID field name missing.");
            }
            if (!dic.ContainsKey("trip_id"))
            {
                throw new FormatException("Trip ID field name missing.");
            }
            if (!dic.ContainsKey("trip_headsign"))
            {
                throw new FormatException("Trip headsign field name missing.");
            }

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

                // Since this application is optimized for Prague, we have to union stations of type Florenc, Florenc - B, Florenc - C. Otherwise they would become three separate stations.

                string headsign = tokens[dic["trip_headsign"]].Length > 4 && tokens[dic["trip_headsign"]][tokens[dic["trip_headsign"]].Length - 3] == '-' ? tokens[dic["trip_headsign"]].Substring(0, tokens[dic["trip_headsign"]].Length - 4) : tokens[dic["trip_headsign"]];

                // The same for Újezd LD and Újezd.

                headsign = headsign.Length > 4 && headsign.Substring(headsign.Length - 4, 4) == "  LD" ? headsign.Substring(0, headsign.Length - 4) : headsign;
                headsign = headsign.Length > 3 && headsign.Substring(headsign.Length - 3, 3) == " LD" ? headsign.Substring(0, headsign.Length - 3) : headsign;

                Trip trip = new Trip(Count, headsign, routesInfo[tokens[dic["route_id"]]], services[tokens[dic["service_id"]]]);

                list.Add(tokens[dic["trip_id"]], trip);
            }
            trips.Dispose();
        }
Example #4
0
        /// <summary>
        /// Initializes object using GTFS data feed.
        /// </summary>
        /// <param name="stops">Stops.</param>
        public GtfsStops(System.IO.StreamReader stops)
        {
            // Get order of field names.
            string[] fieldNames          = stops.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("stop_id"))
            {
                throw new FormatException("Stop ID field name missing.");
            }
            if (!dic.ContainsKey("stop_name"))
            {
                throw new FormatException("Stop name field name missing.");
            }
            if (!dic.ContainsKey("stop_lat"))
            {
                throw new FormatException("Stop latitude field name missing.");
            }
            if (!dic.ContainsKey("stop_lon"))
            {
                throw new FormatException("Stop longitude field name missing.");
            }

            bool containsLocationType = dic.ContainsKey("location_type");             // Optional, but we "need" it (partionally).

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

                if (!containsLocationType || (containsLocationType && (tokens[dic["location_type"]] == "0" || tokens[dic["location_type"]] == string.Empty)))
                {
                    Stop stop = new Stop(Count, tokens[dic["stop_name"]], double.Parse(tokens[dic["stop_lat"]], CultureInfo.InvariantCulture), double.Parse(tokens[dic["stop_lon"]], CultureInfo.InvariantCulture));

                    list.Add(tokens[dic["stop_id"]], stop);
                }
            }

            stops.Dispose();
        }
Example #5
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();
        }
Example #6
0
        /// <summary>
        /// Initializes object using GTFS data feed.
        /// </summary>
        /// <param name="calendar">Calendar.</param>
        public GtfsCalendar(System.IO.StreamReader calendar)
        {
            // Get order of field names.
            string[] fieldNames          = calendar.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("monday"))
            {
                throw new FormatException("Monday field name missing.");
            }
            if (!dic.ContainsKey("tuesday"))
            {
                throw new FormatException("Tuesday field name missing.");
            }
            if (!dic.ContainsKey("wednesday"))
            {
                throw new FormatException("Wednesday field name missing.");
            }
            if (!dic.ContainsKey("thursday"))
            {
                throw new FormatException("Thursday field name missing.");
            }
            if (!dic.ContainsKey("friday"))
            {
                throw new FormatException("Friday field name missing.");
            }
            if (!dic.ContainsKey("saturday"))
            {
                throw new FormatException("Saturday field name missing.");
            }
            if (!dic.ContainsKey("sunday"))
            {
                throw new FormatException("Sunday field name missing.");
            }
            if (!dic.ContainsKey("start_date"))
            {
                throw new FormatException("Start date field name missing.");
            }
            if (!dic.ContainsKey("end_date"))
            {
                throw new FormatException("End date field name missing.");
            }

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

                bool mon = tokens[dic["monday"]] == "1" ? true : false;
                bool tue = tokens[dic["tuesday"]] == "1" ? true : false;
                bool wed = tokens[dic["wednesday"]] == "1" ? true : false;
                bool thu = tokens[dic["thursday"]] == "1" ? true : false;
                bool fri = tokens[dic["friday"]] == "1" ? true : false;
                bool sat = tokens[dic["saturday"]] == "1" ? true : false;
                bool sun = tokens[dic["sunday"]] == "1" ? true : false;

                Service service = new Service(Count, mon, tue, wed, thu, fri, sat, sun, tokens[dic["start_date"]], tokens[dic["end_date"]]);

                list.Add(tokens[dic["service_id"]], service);
            }
            calendar.Dispose();
        }