SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
od2trips_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Main for OD2TRIPS
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #ifdef HAVE_VERSION_H
35 #include <version.h>
36 #endif
37 
38 #include <iostream>
39 #include <algorithm>
40 #include <math.h>
41 #include <cstdlib>
42 #include <string>
43 #include <xercesc/parsers/SAXParser.hpp>
44 #include <xercesc/sax2/SAX2XMLReader.hpp>
45 #include <utils/options/Option.h>
52 #include <utils/common/ToString.h>
53 #include <utils/xml/XMLSubSys.h>
57 #include <od2trips/ODMatrix.h>
59 #include <utils/common/SUMOTime.h>
65 
66 #ifdef CHECK_MEMORY_LEAKS
67 #include <foreign/nvwa/debug_new.h>
68 #endif // CHECK_MEMORY_LEAKS
69 
70 
71 // ===========================================================================
72 // functions
73 // ===========================================================================
74 void
77  oc.addCallExample("-c <CONFIGURATION>", "run with configuration file");
78 
79  // insert options sub-topics
80  SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
81  oc.addOptionSubTopic("Input");
82  oc.addOptionSubTopic("Output");
83  oc.addOptionSubTopic("Time");
84  oc.addOptionSubTopic("Processing");
85  oc.addOptionSubTopic("Defaults");
86  SystemFrame::addReportOptions(oc); // fill this subtopic, too
87 
88 
89  // register the file input options
90  oc.doRegister("net-file", 'n', new Option_FileName());
91  oc.addSynonyme("net-file", "net");
92  oc.addDescription("net-file", "Input", "Loads network (districts) from FILE");
93 
94  oc.doRegister("od-matrix-files", 'd', new Option_FileName());
95  oc.addSynonyme("od-matrix-files", "od-files");
96  oc.addSynonyme("od-matrix-files", "od");
97  oc.addDescription("od-matrix-files", "Input", "Loads O/D-files from FILE(s)");
98 
99 
100  // register the file output options
101  oc.doRegister("output-file", 'o', new Option_FileName());
102  oc.addSynonyme("output-file", "output", true);
103  oc.addDescription("output-file", "Output", "Writes trip definitions into FILE");
104 
105  oc.doRegister("flow-output", new Option_FileName());
106  oc.addDescription("flow-output", "Output", "Writes flow definitions into FILE");
107 
108  oc.doRegister("ignore-vehicle-type", new Option_Bool(false));
109  oc.addSynonyme("ignore-vehicle-type", "no-vtype", true);
110  oc.addDescription("ignore-vehicle-type", "Output", "Does not save vtype information");
111 
112 
113  // register the time settings
114  oc.doRegister("begin", 'b', new Option_String("0", "TIME"));
115  oc.addDescription("begin", "Time", "Defines the begin time; Previous trips will be discarded");
116 
117  oc.doRegister("end", 'e', new Option_String(SUMOTIME_MAXSTRING, "TIME"));
118  oc.addDescription("end", "Time", "Defines the end time; Later trips will be discarded; Defaults to the maximum time that SUMO can represent");
119 
120 
121  // register the data processing options
122  oc.doRegister("scale", 's', new Option_Float(1));
123  oc.addDescription("scale", "Processing", "Scales the loaded flows by FLOAT");
124 
125  oc.doRegister("spread.uniform", new Option_Bool(false));
126  oc.addDescription("spread.uniform", "Processing", "Spreads trips uniformly over each time period");
127 
128  oc.doRegister("vtype", new Option_String(""));
129  oc.addDescription("vtype", "Processing", "Defines the name of the vehicle type to use");
130 
131  oc.doRegister("prefix", new Option_String(""));
132  oc.addDescription("prefix", "Processing", "Defines the prefix for vehicle names");
133 
134  oc.doRegister("timeline", new Option_String());
135  oc.addDescription("timeline", "Processing", "Uses STR as a timeline definition");
136 
137  oc.doRegister("timeline.day-in-hours", new Option_Bool(false));
138  oc.addDescription("timeline.day-in-hours", "Processing", "Uses STR as a 24h-timeline definition");
139 
140  oc.doRegister("ignore-errors", new Option_Bool(false)); // !!! describe, document
141  oc.addSynonyme("ignore-errors", "dismiss-loading-errors", true);
142  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
143 
144  oc.doRegister("no-step-log", new Option_Bool(false));
145  oc.addDescription("no-step-log", "Processing", "Disable console output of current time step");
146 
147 
148  // register defaults options
149  oc.doRegister("departlane", new Option_String("free"));
150  oc.addDescription("departlane", "Defaults", "Assigns a default depart lane");
151 
152  oc.doRegister("departpos", new Option_String());
153  oc.addDescription("departpos", "Defaults", "Assigns a default depart position");
154 
155  oc.doRegister("departspeed", new Option_String("max"));
156  oc.addDescription("departspeed", "Defaults", "Assigns a default depart speed");
157 
158  oc.doRegister("arrivallane", new Option_String());
159  oc.addDescription("arrivallane", "Defaults", "Assigns a default arrival lane");
160 
161  oc.doRegister("arrivalpos", new Option_String());
162  oc.addDescription("arrivalpos", "Defaults", "Assigns a default arrival position");
163 
164  oc.doRegister("arrivalspeed", new Option_String());
165  oc.addDescription("arrivalspeed", "Defaults", "Assigns a default arrival speed");
166 
167  // add rand options
169 }
170 
171 bool
174  bool ok = true;
175  if (!oc.isSet("net-file")) {
176  WRITE_ERROR("No net input file (-n) specified.");
177  ok = false;
178  }
179  if (!oc.isSet("od-matrix-files")) {
180  WRITE_ERROR("No input specified.");
181  ok = false;
182  }
183  if (!oc.isSet("output-file")) {
184  WRITE_ERROR("No trip table output file (-o) specified.");
185  ok = false;
186  }
187  //
189  std::string error;
190  if (oc.isSet("departlane") && !SUMOVehicleParameter::parseDepartLane(oc.getString("departlane"), "option", "departlane", p.departLane, p.departLaneProcedure, error)) {
191  WRITE_ERROR(error);
192  ok = false;
193  }
194  if (oc.isSet("departpos") && !SUMOVehicleParameter::parseDepartPos(oc.getString("departpos"), "option", "departpos", p.departPos, p.departPosProcedure, error)) {
195  WRITE_ERROR(error);
196  ok = false;
197  }
198  if (oc.isSet("departspeed") && !SUMOVehicleParameter::parseDepartSpeed(oc.getString("departspeed"), "option", "departspeed", p.departSpeed, p.departSpeedProcedure, error)) {
199  WRITE_ERROR(error);
200  ok = false;
201  }
202  if (oc.isSet("arrivallane") && !SUMOVehicleParameter::parseArrivalLane(oc.getString("arrivallane"), "option", "arrivallane", p.arrivalLane, p.arrivalLaneProcedure, error)) {
203  WRITE_ERROR(error);
204  ok = false;
205  }
206  if (oc.isSet("arrivalpos") && !SUMOVehicleParameter::parseArrivalPos(oc.getString("arrivalpos"), "option", "arrivalpos", p.arrivalPos, p.arrivalPosProcedure, error)) {
207  WRITE_ERROR(error);
208  ok = false;
209  }
210  if (oc.isSet("arrivalspeed") && !SUMOVehicleParameter::parseArrivalSpeed(oc.getString("arrivalspeed"), "option", "arrivalspeed", p.arrivalSpeed, p.arrivalSpeedProcedure, error)) {
211  WRITE_ERROR(error);
212  ok = false;
213  }
214  return ok;
215 }
216 
217 
218 
219 
220 /* -------------------------------------------------------------------------
221  * main
222  * ----------------------------------------------------------------------- */
223 int
224 main(int argc, char** argv) {
226  // give some application descriptions
227  oc.setApplicationDescription("Importer of O/D-matrices for the road traffic simulation SUMO.");
228  oc.setApplicationName("od2trips", "SUMO od2trips Version " + (std::string)VERSION_STRING);
229  int ret = 0;
230  try {
231  // initialise subsystems
232  XMLSubSys::init();
233  fillOptions();
234  OptionsIO::getOptions(true, argc, argv);
235  if (oc.processMetaOptions(argc < 2)) {
237  return 0;
238  }
239  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
241  if (!checkOptions()) {
242  throw ProcessError();
243  }
245  // load the districts
246  // check whether the user gave a net filename
247  if (!oc.isSet("net-file")) {
248  throw ProcessError("You must supply a network or districts file ('-n').");
249  }
250  // get the file name and set it
251  ODDistrictCont districts;
252  districts.loadDistricts(oc.getString("net-file"));
253  if (districts.size() == 0) {
254  throw ProcessError("No districts loaded.");
255  }
256  // load the matrix
257  ODMatrix matrix(districts);
258  matrix.loadMatrix(oc);
259  if (matrix.getNoLoaded() == 0) {
260  throw ProcessError("No vehicles loaded.");
261  }
262  if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
263  throw ProcessError("Loading failed.");
264  }
265  WRITE_MESSAGE(toString(matrix.getNoLoaded()) + " vehicles loaded.");
266  // apply a curve if wished
267  if (oc.isSet("timeline")) {
268  matrix.applyCurve(matrix.parseTimeLine(oc.getStringVector("timeline"), oc.getBool("timeline.day-in-hours")));
269  }
270  // write
271  bool haveOutput = false;
272  if (OutputDevice::createDeviceByOption("output-file", "routes", "routes_file.xsd")) {
273  matrix.write(string2time(oc.getString("begin")), string2time(oc.getString("end")),
274  OutputDevice::getDeviceByOption("output-file"),
275  oc.getBool("spread.uniform"), oc.getBool("ignore-vehicle-type"),
276  oc.getString("prefix"), !oc.getBool("no-step-log"));
277  haveOutput = true;
278  }
279  if (OutputDevice::createDeviceByOption("flow-output", "routes", "routes_file.xsd")) {
280  matrix.writeFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
281  OutputDevice::getDeviceByOption("flow-output"),
282  oc.getBool("ignore-vehicle-type"), oc.getString("prefix"));
283  haveOutput = true;
284  }
285  if (!haveOutput) {
286  throw ProcessError("No output file given.");
287  }
288  WRITE_MESSAGE(toString(matrix.getNoDiscarded()) + " vehicles discarded.");
289  WRITE_MESSAGE(toString(matrix.getNoWritten()) + " vehicles written.");
290  } catch (const ProcessError& e) {
291  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
292  WRITE_ERROR(e.what());
293  }
294  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
295  ret = 1;
296 #ifndef _DEBUG
297  } catch (const std::exception& e) {
298  if (std::string(e.what()) != std::string("")) {
299  WRITE_ERROR(e.what());
300  }
301  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
302  ret = 1;
303  } catch (...) {
304  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
305  ret = 1;
306 #endif
307  }
309  if (ret == 0) {
310  std::cout << "Success." << std::endl;
311  }
312  return ret;
313 }
314 
315 
316 
317 /****************************************************************************/
318 
SUMOReal getNoLoaded() const
Returns the number of loaded vehicles.
Definition: ODMatrix.cpp:427
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:84
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 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:74
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.
static void getOptions(bool loadConfig, int argc=0, char **argv=0)
Parses the command line arguments and loads the configuration optionally.
Definition: OptionsIO.cpp:64
ArrivalLaneDefinition arrivalLaneProcedure
Information how the vehicle shall choose the lane to arrive on.
void write(SUMOTime begin, const SUMOTime end, OutputDevice &dev, const bool uniform, 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:165
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
void loadDistricts(std::string districtfile)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
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 VISUM-matrix with the V Format
Definition: ODMatrix.cpp:472
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)
SUMOReal getNoDiscarded() const
Returns the number of discarded vehicles.
Definition: ODMatrix.cpp:439
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:73
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)
SUMOTime string2time(const std::string &r)
Definition: SUMOTime.cpp:48
unsigned int size() const
Returns the number of items within the container.
A container for districts.
SUMOReal getNoWritten() const
Returns the number of written vehicles.
Definition: ODMatrix.cpp:433
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:52
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:232
int departLane
(optional) The lane the vehicle shall depart from (index in edge)
#define VERSION_STRING
Definition: config.h:227
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:201
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:108
void applyCurve(const Distribution_Points &ps)
Splits the stored cells dividing them on the given time line.
Definition: ODMatrix.cpp:460
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 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:197
static void initOutputOptions()
Definition: MsgHandler.cpp:193
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.
Distribution_Points parseTimeLine(const std::vector< std::string > &def, bool timelineDayInHours)
split the given timeline
Definition: ODMatrix.cpp:510
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
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.