SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
od2trips_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // Main for OD2TRIPS
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2002-2016 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #ifdef HAVE_VERSION_H
36 #include <version.h>
37 #endif
38 
39 #include <iostream>
40 #include <algorithm>
41 #include <math.h>
42 #include <cstdlib>
43 #include <string>
44 #include <xercesc/parsers/SAXParser.hpp>
45 #include <xercesc/sax2/SAX2XMLReader.hpp>
46 #include <utils/options/Option.h>
53 #include <utils/common/ToString.h>
54 #include <utils/xml/XMLSubSys.h>
56 #include <od/ODDistrictCont.h>
57 #include <od/ODDistrictHandler.h>
58 #include <od/ODMatrix.h>
60 #include <utils/common/SUMOTime.h>
66 
67 #ifdef CHECK_MEMORY_LEAKS
68 #include <foreign/nvwa/debug_new.h>
69 #endif // CHECK_MEMORY_LEAKS
70 
71 
72 // ===========================================================================
73 // functions
74 // ===========================================================================
75 void
78  oc.addCallExample("-c <CONFIGURATION>", "run with configuration file");
79 
80  // insert options sub-topics
81  SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
82  oc.addOptionSubTopic("Input");
83  oc.addOptionSubTopic("Output");
84  oc.addOptionSubTopic("Time");
85  oc.addOptionSubTopic("Processing");
86  oc.addOptionSubTopic("Defaults");
87  SystemFrame::addReportOptions(oc); // fill this subtopic, too
88 
89 
90  // register the file input options
91  oc.doRegister("taz-files", 'n', new Option_FileName());
92  oc.addSynonyme("taz-files", "taz");
93  oc.addSynonyme("taz-files", "net-file");
94  oc.addSynonyme("taz-files", "net");
95  oc.addDescription("taz-files", "Input", "Loads TAZ (districts; also from networks) from FILE(s)");
96 
97  oc.doRegister("od-matrix-files", 'd', new Option_FileName());
98  oc.addSynonyme("od-matrix-files", "od-files");
99  oc.addSynonyme("od-matrix-files", "od");
100  oc.addDescription("od-matrix-files", "Input", "Loads O/D-files from FILE(s)");
101 
102  oc.doRegister("od-amitran-files", new Option_FileName());
103  oc.addSynonyme("od-amitran-files", "amitran-files");
104  oc.addSynonyme("od-amitran-files", "amitran");
105  oc.addDescription("od-amitran-files", "Input", "Loads O/D-matrix in Amitran format from FILE(s)");
106 
107 
108  // register the file output options
109  oc.doRegister("output-file", 'o', new Option_FileName());
110  oc.addSynonyme("output-file", "output", true);
111  oc.addDescription("output-file", "Output", "Writes trip definitions into FILE");
112 
113  oc.doRegister("flow-output", new Option_FileName());
114  oc.addDescription("flow-output", "Output", "Writes flow definitions into FILE");
115 
116  oc.doRegister("ignore-vehicle-type", new Option_Bool(false));
117  oc.addSynonyme("ignore-vehicle-type", "no-vtype", true);
118  oc.addDescription("ignore-vehicle-type", "Output", "Does not save vtype information");
119 
120 
121  // register the time settings
122  oc.doRegister("begin", 'b', new Option_String("0", "TIME"));
123  oc.addDescription("begin", "Time", "Defines the begin time; Previous trips will be discarded");
124 
125  oc.doRegister("end", 'e', new Option_String(SUMOTIME_MAXSTRING, "TIME"));
126  oc.addDescription("end", "Time", "Defines the end time; Later trips will be discarded; Defaults to the maximum time that SUMO can represent");
127 
128 
129  // register the data processing options
130  oc.doRegister("scale", 's', new Option_Float(1));
131  oc.addDescription("scale", "Processing", "Scales the loaded flows by FLOAT");
132 
133  oc.doRegister("spread.uniform", new Option_Bool(false));
134  oc.addDescription("spread.uniform", "Processing", "Spreads trips uniformly over each time period");
135 
136  oc.doRegister("different-source-sink", new Option_Bool(false));
137  oc.addDescription("different-source-sink", "Processing", "Always choose source and sink edge which are not identical");
138 
139  oc.doRegister("vtype", new Option_String(""));
140  oc.addDescription("vtype", "Processing", "Defines the name of the vehicle type to use");
141 
142  oc.doRegister("prefix", new Option_String(""));
143  oc.addDescription("prefix", "Processing", "Defines the prefix for vehicle names");
144 
145  oc.doRegister("timeline", new Option_String());
146  oc.addDescription("timeline", "Processing", "Uses STR as a timeline definition");
147 
148  oc.doRegister("timeline.day-in-hours", new Option_Bool(false));
149  oc.addDescription("timeline.day-in-hours", "Processing", "Uses STR as a 24h-timeline definition");
150 
151  oc.doRegister("ignore-errors", new Option_Bool(false)); // !!! describe, document
152  oc.addSynonyme("ignore-errors", "dismiss-loading-errors", true);
153  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
154 
155  oc.doRegister("no-step-log", new Option_Bool(false));
156  oc.addDescription("no-step-log", "Processing", "Disable console output of current time step");
157 
158 
159  // register defaults options
160  oc.doRegister("departlane", new Option_String("free"));
161  oc.addDescription("departlane", "Defaults", "Assigns a default depart lane");
162 
163  oc.doRegister("departpos", new Option_String());
164  oc.addDescription("departpos", "Defaults", "Assigns a default depart position");
165 
166  oc.doRegister("departspeed", new Option_String("max"));
167  oc.addDescription("departspeed", "Defaults", "Assigns a default depart speed");
168 
169  oc.doRegister("arrivallane", new Option_String());
170  oc.addDescription("arrivallane", "Defaults", "Assigns a default arrival lane");
171 
172  oc.doRegister("arrivalpos", new Option_String());
173  oc.addDescription("arrivalpos", "Defaults", "Assigns a default arrival position");
174 
175  oc.doRegister("arrivalspeed", new Option_String());
176  oc.addDescription("arrivalspeed", "Defaults", "Assigns a default arrival speed");
177 
178  // add rand options
180 }
181 
182 bool
185  bool ok = true;
186  if (!oc.isSet("taz-files")) {
187  WRITE_ERROR("No TAZ input file (-n) specified.");
188  ok = false;
189  }
190  if (!oc.isSet("od-matrix-files") && !oc.isSet("od-amitran-files")) {
191  WRITE_ERROR("No input specified.");
192  ok = false;
193  }
194  if (!oc.isSet("output-file")) {
195  WRITE_ERROR("No trip table output file (-o) specified.");
196  ok = false;
197  }
198  //
200  std::string error;
201  if (oc.isSet("departlane") && !SUMOVehicleParameter::parseDepartLane(oc.getString("departlane"), "option", "departlane", p.departLane, p.departLaneProcedure, error)) {
202  WRITE_ERROR(error);
203  ok = false;
204  }
205  if (oc.isSet("departpos") && !SUMOVehicleParameter::parseDepartPos(oc.getString("departpos"), "option", "departpos", p.departPos, p.departPosProcedure, error)) {
206  WRITE_ERROR(error);
207  ok = false;
208  }
209  if (oc.isSet("departspeed") && !SUMOVehicleParameter::parseDepartSpeed(oc.getString("departspeed"), "option", "departspeed", p.departSpeed, p.departSpeedProcedure, error)) {
210  WRITE_ERROR(error);
211  ok = false;
212  }
213  if (oc.isSet("arrivallane") && !SUMOVehicleParameter::parseArrivalLane(oc.getString("arrivallane"), "option", "arrivallane", p.arrivalLane, p.arrivalLaneProcedure, error)) {
214  WRITE_ERROR(error);
215  ok = false;
216  }
217  if (oc.isSet("arrivalpos") && !SUMOVehicleParameter::parseArrivalPos(oc.getString("arrivalpos"), "option", "arrivalpos", p.arrivalPos, p.arrivalPosProcedure, error)) {
218  WRITE_ERROR(error);
219  ok = false;
220  }
221  if (oc.isSet("arrivalspeed") && !SUMOVehicleParameter::parseArrivalSpeed(oc.getString("arrivalspeed"), "option", "arrivalspeed", p.arrivalSpeed, p.arrivalSpeedProcedure, error)) {
222  WRITE_ERROR(error);
223  ok = false;
224  }
225  return ok;
226 }
227 
228 
229 
230 
231 /* -------------------------------------------------------------------------
232  * main
233  * ----------------------------------------------------------------------- */
234 int
235 main(int argc, char** argv) {
237  // give some application descriptions
238  oc.setApplicationDescription("Importer of O/D-matrices for the road traffic simulation SUMO.");
239  oc.setApplicationName("od2trips", "SUMO od2trips Version " VERSION_STRING);
240  int ret = 0;
241  try {
242  // initialise subsystems
243  XMLSubSys::init();
244  fillOptions();
245  OptionsIO::setArgs(argc, argv);
247  if (oc.processMetaOptions(argc < 2)) {
249  return 0;
250  }
251  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
253  if (!checkOptions()) {
254  throw ProcessError();
255  }
257  // load the districts
258  // check whether the user gave a net filename
259  if (!oc.isSet("taz-files")) {
260  throw ProcessError("You must supply a TAZ, network or districts file ('-n').");
261  }
262  // get the file name and set it
263  ODDistrictCont districts;
264  districts.loadDistricts(oc.getStringVector("taz-files"));
265  if (districts.size() == 0) {
266  throw ProcessError("No districts loaded.");
267  }
268  // load the matrix
269  ODMatrix matrix(districts);
270  matrix.loadMatrix(oc);
271  if (matrix.getNumLoaded() == 0) {
272  throw ProcessError("No vehicles loaded.");
273  }
274  if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
275  throw ProcessError("Loading failed.");
276  }
277  WRITE_MESSAGE(toString(matrix.getNumLoaded()) + " vehicles loaded.");
278  // apply a curve if wished
279  if (oc.isSet("timeline")) {
280  matrix.applyCurve(matrix.parseTimeLine(oc.getStringVector("timeline"), oc.getBool("timeline.day-in-hours")));
281  }
282  // write
283  bool haveOutput = false;
284  if (OutputDevice::createDeviceByOption("output-file", "routes", "routes_file.xsd")) {
285  matrix.write(string2time(oc.getString("begin")), string2time(oc.getString("end")),
286  OutputDevice::getDeviceByOption("output-file"),
287  oc.getBool("spread.uniform"), oc.getBool("different-source-sink"),
288  oc.getBool("ignore-vehicle-type"),
289  oc.getString("prefix"), !oc.getBool("no-step-log"));
290  haveOutput = true;
291  }
292  if (OutputDevice::createDeviceByOption("flow-output", "routes", "routes_file.xsd")) {
293  matrix.writeFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
294  OutputDevice::getDeviceByOption("flow-output"),
295  oc.getBool("ignore-vehicle-type"), oc.getString("prefix"));
296  haveOutput = true;
297  }
298  if (!haveOutput) {
299  throw ProcessError("No output file given.");
300  }
301  WRITE_MESSAGE(toString(matrix.getNumDiscarded()) + " vehicles discarded.");
302  WRITE_MESSAGE(toString(matrix.getNumWritten()) + " vehicles written.");
303  } catch (const ProcessError& e) {
304  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
305  WRITE_ERROR(e.what());
306  }
307  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
308  ret = 1;
309 #ifndef _DEBUG
310  } catch (const std::exception& e) {
311  if (std::string(e.what()) != std::string("")) {
312  WRITE_ERROR(e.what());
313  }
314  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
315  ret = 1;
316  } catch (...) {
317  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
318  ret = 1;
319 #endif
320  }
322  if (ret == 0) {
323  std::cout << "Success." << std::endl;
324  }
325  return ret;
326 }
327 
328 
329 
330 /****************************************************************************/
331 
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:86
std::vector< std::string > getStringVector(const std::string &name) const
Returns the list of string-vector-value of the named option (only for Option_String) ...
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:58
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:80
bool checkOptions()
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:53
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:72
static bool parseDepartSpeed(const std::string &val, const std::string &element, const std::string &id, SUMOReal &speed, DepartSpeedDefinition &dsd, std::string &error)
Validates a given departSpeed value.
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:75
static bool parseArrivalPos(const std::string &val, const std::string &element, const std::string &id, SUMOReal &pos, ArrivalPosDefinition &apd, std::string &error)
Validates a given arrivalPos value.
SUMOReal getNumLoaded() const
Returns the number of loaded vehicles.
Definition: ODMatrix.cpp:486
ArrivalLaneDefinition arrivalLaneProcedure
Information how the vehicle shall choose the lane to arrive on.
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:69
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
DepartLaneDefinition departLaneProcedure
Information how the vehicle shall choose the lane to depart from.
SUMOReal departSpeed
(optional) The initial speed of the vehicle
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
SUMOReal arrivalSpeed
(optional) The final speed of the vehicle (not used yet)
SUMOReal arrivalPos
(optional) The position the vehicle shall arrive on
ArrivalSpeedDefinition arrivalSpeedProcedure
Information how the vehicle's end speed shall be chosen.
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:50
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:65
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
static void initRandGlobal(MTRand *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:68
void loadMatrix(OptionsCont &oc)
read a matrix in one of several formats
Definition: ODMatrix.cpp:532
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
DepartSpeedDefinition departSpeedProcedure
Information how the vehicle's initial speed shall be chosen.
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
An O/D (origin/destination) matrix.
Definition: ODMatrix.h:76
void fillOptions()
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
int arrivalLane
(optional) The lane the vehicle shall arrive on (not used yet)
SUMOReal getNumWritten() const
Returns the number of written vehicles.
Definition: ODMatrix.cpp:492
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:46
A container for districts.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
static bool parseArrivalLane(const std::string &val, const std::string &element, const std::string &id, int &lane, ArrivalLaneDefinition &ald, std::string &error)
Validates a given arrivalLane value.
#define SUMOTIME_MAXSTRING
Definition: SUMOTime.h:46
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
static bool parseDepartPos(const std::string &val, const std::string &element, const std::string &id, SUMOReal &pos, DepartPosDefinition &dpd, std::string &error)
Validates a given departPos value.
void writeFlows(const SUMOTime begin, const SUMOTime end, OutputDevice &dev, const bool noVtype, const std::string &prefix)
Writes the flows stored in the matrix.
Definition: ODMatrix.cpp:291
int departLane
(optional) The lane the vehicle shall depart from (index in edge)
void loadDistricts(std::vector< std::string > files)
load districts from files
#define VERSION_STRING
Definition: config.h:226
static bool parseArrivalSpeed(const std::string &val, const std::string &element, const std::string &id, SUMOReal &speed, ArrivalSpeedDefinition &asd, std::string &error)
Validates a given arrivalSpeed value.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
int main(int argc, char **argv)
Structure representing possible vehicle parameter.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:89
A storage for options typed value containers)
Definition: OptionsCont.h:99
void applyCurve(const Distribution_Points &ps)
Splits the stored cells dividing them on the given time line.
Definition: ODMatrix.cpp:519
SUMOReal departPos
(optional) The position the vehicle shall depart from
static bool createDeviceByOption(const std::string &optionName, const std::string &rootElement="", const std::string &schemaFile="")
Creates the device using the output definition stored in the named option.
void write(SUMOTime begin, const SUMOTime end, OutputDevice &dev, const bool uniform, const bool differSourceSink, const bool noVtype, const std::string &prefix, const bool stepLog)
Writes the vehicles stored in the matrix assigning the sources and sinks.
Definition: ODMatrix.cpp:221
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:201
static void initOutputOptions()
Definition: MsgHandler.cpp:197
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.
SUMOReal getNumDiscarded() const
Returns the number of discarded vehicles.
Definition: ODMatrix.cpp:498
Distribution_Points parseTimeLine(const std::vector< std::string > &def, bool timelineDayInHours)
split the given timeline
Definition: ODMatrix.cpp:595
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
int size() const
Returns the number of items within the container.
static bool parseDepartLane(const std::string &val, const std::string &element, const std::string &id, int &lane, DepartLaneDefinition &dld, std::string &error)
Validates a given departLane value.
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.