Transit Planner  1.0
An experiment on transfer patterns robustness
src/GtfsParser.h
00001 // Copyright 2011: Eugen Sawin, Philip Stahl, Jonas Sternisko
00002 #ifndef SRC_GTFSPARSER_H_
00003 #define SRC_GTFSPARSER_H_
00004 
00005 #include <boost/tuple/tuple.hpp>
00006 #include <google/dense_hash_set>
00007 #include <google/dense_hash_map>
00008 #include <bitset>
00009 #include <map>
00010 #include <string>
00011 #include <vector>
00012 #include "./CsvParser.h"
00013 #include "./Logger.h"
00014 
00015 
00016 using std::string;
00017 using std::vector;
00018 using std::map;
00019 using google::dense_hash_set;
00020 using google::dense_hash_map;
00021 
00022 class Line;
00023 class Trip;
00024 class TransitNetwork;
00025 
00026 
00027 class GtfsParser {
00028  public:
00029   // Forward declaration for private data types.
00030   class Frequency;
00031   class Data;
00032   // Map for frequencies.
00033   typedef dense_hash_map<string, vector<Frequency> > FrequencyMap;
00034   // A tuple of weekday activity as bitset, start time and end time.
00035   typedef boost::tuple<std::bitset<7>, int, int> Activity;
00036   // A mapping indicating service activity for each weekday.
00037   typedef dense_hash_map<string, Activity> ActivityMap;
00038 
00039   // Constructor
00040   explicit GtfsParser(Logger* log = NULL);
00041   // Null assignment operator (prohibits assignment)
00042   GtfsParser& operator=(const GtfsParser& other);
00043   // Destructor
00044   ~GtfsParser();
00045 
00046   string parseName(const string& gtfsDir);
00047   // Formats the name of a network from its location and the time period.
00048   string parseName(const vector<string>& gtfsDirs,
00049                  const string& startTimeStr, const string& endTimeStr,
00050                  TransitNetwork* network = NULL);
00051 
00052   // Create the transit network from the GTFS files in 'gtfsDirectory'.
00053   TransitNetwork createTransitNetwork(const string& gtfsDir,
00054                                       const string& startTimeStr,
00055                                       const string& endTimeStr,
00056                                       vector<Line>* lines = NULL);
00057   // Create the transit network from the GTFS files (!) in each directory.
00058   TransitNetwork createTransitNetwork(const vector<string>& gtfsDirs,
00059                                       const string& startTimeStr,
00060                                       const string& endTimeStr,
00061                                       vector<Line>* lines = NULL);
00062 
00063   // Reads the Gtfs files in a directory.
00064   void parseGtfs(const string& gtfsDirectory, TransitNetwork* network);
00065 
00066   // Creates the nodes and arcs for each trip using the last read gtfs data.
00067   // Optionally transforms the gtfs trips into dc-trips by argument 'trips'.
00068   void
00069   translateLastTripsToNetwork(const string& startTimeStr,
00070                               const string& endTimeStr, TransitNetwork* network,
00071                               vector<Trip>* trips = NULL);
00072 
00073   // Removes all arcs starting at transit nodes. Returns the number of deleted.
00074   int removeInterTripArcs(TransitNetwork* network) const;
00075 
00076   // Serializes transit network to a binary file.
00077   bool save(const TransitNetwork& network, const string& filename);
00078   // Deserializes transit network from binary file. Returns false if not exists.
00079   bool load(const string& filename, TransitNetwork* network);
00080 
00081   // Set the logger to an external logger.
00082   void logger(Logger* const log);
00083 
00084   // Accesses the private data.
00085   const GtfsParser::Data& data() const;
00086 
00087  private:
00088   // Check whether a service is active on a certain day.
00089   bool isActive(const string& serviceId, const ActivityMap& activityMap,
00090                 const boost::gregorian::date& day);
00091   FRIEND_TEST(GtfsParserTest, isActive);
00092 
00093   // Generates the vector of starting times of a trip using frequencies.txt's
00094   // content.
00095   vector<int> generateStartTimes(const string& tripId,
00096                                  const FrequencyMap& frequencies) const;
00097 
00098   // Adds a trip for a trip of stop_times.txt converting its relative time
00099   // stamps to absolute times.
00100   void addTrip(const Trip& block, const int timeOffset,
00101                vector<Trip>* trips) const;
00102 
00103   // Generates arrival, departure and transfer node for each stop of a trip
00104   // (which is given as a block of stop_times.txt)
00105   void generateTripNodes(const Trip& trip, const FrequencyMap& frequencies,
00106                          const int timeOffset, TransitNetwork* network) const;
00107 
00108   // Sorts the nodes for each stop by time, add waiting arcs between transit
00109   // nodes, and boarding arcs between transit and departure nodes.
00110   void generateInterTripArcs(TransitNetwork* network) const;
00111 
00112   // Returns a field name -> column index map of given CSV file.
00113   map<string, int> parseFields(const string& filename) const;
00114 
00115   // Parses the GTFS calendar file and create a mapping from service id to a
00116   // bitset describing the active days and two integers for start and end of
00117   // the service period.
00118   GtfsParser::ActivityMap parseCalendarFile(const string& filename);
00119   FRIEND_TEST(GtfsParserTest, calendar_txt);
00120 
00121   // Parses the GTFS trips file. Returns a map from trip_id to service_id.
00122   map<string, string> parseTripsFile(const string& filename);
00123   FRIEND_TEST(GtfsParserTest, trips_txt);
00124 
00125   // Parses the GTFS stops file.
00126   void parseStopsFile(const string& filename, TransitNetwork* network);
00127   FRIEND_TEST(GtfsParserTest, stops_txt);
00128 
00129   // Parses the GTFS frequencies file.
00130   FrequencyMap parseFrequenciesFile(const string& filename);
00131   FRIEND_TEST(GtfsParserTest, frequencies_txt);
00132 
00133   // Parses the GTFS stop-times file.
00134   vector<Trip> parseStopTimesFile(const string& filename,
00135                                   const TransitNetwork& network);
00136   FRIEND_TEST(GtfsParserTest, stop_times_txt);
00137 
00138   // Adds the data from one line of the stop-times file to a vector of stops.
00139   void addStopToTrip(const string& arrTimeStr, const string& depTimeStr,
00140                      const int stopIndex, Trip* trip);
00141 
00142   // Converts a string of format "" into the number of seconds from midnight.
00143   int gtfsTimeStr2Sec(const string& timesStr);
00144   FRIEND_TEST(GtfsParserTest, timeStringToSeconds);
00145 
00146   // Checks, whether two time stamps define a valid time period.
00147   static bool isValidTimePeriod(const string& startTimeStr,
00148                                 const string& endTimeStr);
00149   FRIEND_TEST(GtfsParserTest, isValidTimePeriod);
00150 
00151   Data* _data;
00152   Logger* _log;
00153   friend class ScenarioGenerator;
00154 };
00155 
00156 #endif  // SRC_GTFSPARSER_H_
 All Classes