SUMO - Simulation of Urban MObility
od2trips_main.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
21 // Main for OD2TRIPS
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 <cmath>
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>
55 #include <od/ODDistrictCont.h>
56 #include <od/ODDistrictHandler.h>
57 #include <od/ODMatrix.h>
59 #include <utils/common/SUMOTime.h>
65 
66 
67 // ===========================================================================
68 // functions
69 // ===========================================================================
70 void
73  oc.addCallExample("-c <CONFIGURATION>", "run with configuration file");
74 
75  // insert options sub-topics
76  SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
77  oc.addOptionSubTopic("Input");
78  oc.addOptionSubTopic("Output");
79  oc.addOptionSubTopic("Time");
80  oc.addOptionSubTopic("Processing");
81  oc.addOptionSubTopic("Defaults");
82  SystemFrame::addReportOptions(oc); // fill this subtopic, too
83 
84 
85  // register the file input options
86  oc.doRegister("taz-files", 'n', new Option_FileName());
87  oc.addSynonyme("taz-files", "taz");
88  oc.addSynonyme("taz-files", "net-file");
89  oc.addSynonyme("taz-files", "net");
90  oc.addDescription("taz-files", "Input", "Loads TAZ (districts; also from networks) from FILE(s)");
91 
92  oc.doRegister("od-matrix-files", 'd', new Option_FileName());
93  oc.addSynonyme("od-matrix-files", "od-files");
94  oc.addSynonyme("od-matrix-files", "od");
95  oc.addDescription("od-matrix-files", "Input", "Loads O/D-files from FILE(s)");
96 
97  oc.doRegister("od-amitran-files", new Option_FileName());
98  oc.addSynonyme("od-amitran-files", "amitran-files");
99  oc.addSynonyme("od-amitran-files", "amitran");
100  oc.addDescription("od-amitran-files", "Input", "Loads O/D-matrix in Amitran format from FILE(s)");
101 
102 
103  // register the file output options
104  oc.doRegister("output-file", 'o', new Option_FileName());
105  oc.addSynonyme("output-file", "output", true);
106  oc.addDescription("output-file", "Output", "Writes trip definitions into FILE");
107 
108  oc.doRegister("flow-output", new Option_FileName());
109  oc.addDescription("flow-output", "Output", "Writes flow definitions into FILE");
110 
111  oc.doRegister("flow-output.probability", new Option_Bool(false));
112  oc.addDescription("flow-output.probability", "Output", "Writes probabilistic flow instead of evenly spaced flow");
113 
114  oc.doRegister("pedestrians", new Option_Bool(false));
115  oc.addDescription("pedestrians", "Output", "Writes pedestrians instead of vehicles");
116 
117  oc.doRegister("persontrips", new Option_Bool(false));
118  oc.addDescription("persontrips", "Output", "Writes persontrips instead of vehicles");
119 
120  oc.doRegister("ignore-vehicle-type", new Option_Bool(false));
121  oc.addSynonyme("ignore-vehicle-type", "no-vtype", true);
122  oc.addDescription("ignore-vehicle-type", "Output", "Does not save vtype information");
123 
124 
125  // register the time settings
126  oc.doRegister("begin", 'b', new Option_String("0", "TIME"));
127  oc.addDescription("begin", "Time", "Defines the begin time; Previous trips will be discarded");
128 
129  oc.doRegister("end", 'e', new Option_String(SUMOTIME_MAXSTRING, "TIME"));
130  oc.addDescription("end", "Time", "Defines the end time; Later trips will be discarded; Defaults to the maximum time that SUMO can represent");
131 
132 
133  // register the data processing options
134  oc.doRegister("scale", 's', new Option_Float(1));
135  oc.addDescription("scale", "Processing", "Scales the loaded flows by FLOAT");
136 
137  oc.doRegister("spread.uniform", new Option_Bool(false));
138  oc.addDescription("spread.uniform", "Processing", "Spreads trips uniformly over each time period");
139 
140  oc.doRegister("different-source-sink", new Option_Bool(false));
141  oc.addDescription("different-source-sink", "Processing", "Always choose source and sink edge which are not identical");
142 
143  oc.doRegister("vtype", new Option_String(""));
144  oc.addDescription("vtype", "Processing", "Defines the name of the vehicle type to use");
145 
146  oc.doRegister("prefix", new Option_String(""));
147  oc.addDescription("prefix", "Processing", "Defines the prefix for vehicle names");
148 
149  oc.doRegister("timeline", new Option_String());
150  oc.addDescription("timeline", "Processing", "Uses STR as a timeline definition");
151 
152  oc.doRegister("timeline.day-in-hours", new Option_Bool(false));
153  oc.addDescription("timeline.day-in-hours", "Processing", "Uses STR as a 24h-timeline definition");
154 
155  oc.doRegister("ignore-errors", new Option_Bool(false)); // !!! describe, document
156  oc.addSynonyme("ignore-errors", "dismiss-loading-errors", true);
157  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
158 
159  oc.doRegister("no-step-log", new Option_Bool(false));
160  oc.addDescription("no-step-log", "Processing", "Disable console output of current time step");
161 
162 
163  // register defaults options
164  oc.doRegister("departlane", new Option_String("free"));
165  oc.addDescription("departlane", "Defaults", "Assigns a default depart lane");
166 
167  oc.doRegister("departpos", new Option_String());
168  oc.addDescription("departpos", "Defaults", "Assigns a default depart position");
169 
170  oc.doRegister("departspeed", new Option_String("max"));
171  oc.addDescription("departspeed", "Defaults", "Assigns a default depart speed");
172 
173  oc.doRegister("arrivallane", new Option_String());
174  oc.addDescription("arrivallane", "Defaults", "Assigns a default arrival lane");
175 
176  oc.doRegister("arrivalpos", new Option_String());
177  oc.addDescription("arrivalpos", "Defaults", "Assigns a default arrival position");
178 
179  oc.doRegister("arrivalspeed", new Option_String());
180  oc.addDescription("arrivalspeed", "Defaults", "Assigns a default arrival speed");
181 
182  // add rand options
184 }
185 
186 bool
189  bool ok = true;
190  if (!oc.isSet("taz-files")) {
191  WRITE_ERROR("No TAZ input file (-n) specified.");
192  ok = false;
193  }
194  if (!oc.isSet("od-matrix-files") && !oc.isSet("od-amitran-files")) {
195  WRITE_ERROR("No input specified.");
196  ok = false;
197  }
198  if (!oc.isSet("output-file")) {
199  WRITE_ERROR("No trip table output file (-o) specified.");
200  ok = false;
201  }
202  if (oc.getBool("pedestrians") && oc.getBool("persontrips")) {
203  WRITE_ERROR("Only of the the options 'pedestrians' and 'persontrips' may be set.");
204  ok = false;
205  }
206  //
208  std::string error;
209  if (oc.isSet("departlane") && !SUMOVehicleParameter::parseDepartLane(oc.getString("departlane"), "option", "departlane", p.departLane, p.departLaneProcedure, error)) {
210  WRITE_ERROR(error);
211  ok = false;
212  }
213  if (oc.isSet("departpos") && !SUMOVehicleParameter::parseDepartPos(oc.getString("departpos"), "option", "departpos", p.departPos, p.departPosProcedure, error)) {
214  WRITE_ERROR(error);
215  ok = false;
216  }
217  if (oc.isSet("departspeed") && !SUMOVehicleParameter::parseDepartSpeed(oc.getString("departspeed"), "option", "departspeed", p.departSpeed, p.departSpeedProcedure, error)) {
218  WRITE_ERROR(error);
219  ok = false;
220  }
221  if (oc.isSet("arrivallane") && !SUMOVehicleParameter::parseArrivalLane(oc.getString("arrivallane"), "option", "arrivallane", p.arrivalLane, p.arrivalLaneProcedure, error)) {
222  WRITE_ERROR(error);
223  ok = false;
224  }
225  if (oc.isSet("arrivalpos") && !SUMOVehicleParameter::parseArrivalPos(oc.getString("arrivalpos"), "option", "arrivalpos", p.arrivalPos, p.arrivalPosProcedure, error)) {
226  WRITE_ERROR(error);
227  ok = false;
228  }
229  if (oc.isSet("arrivalspeed") && !SUMOVehicleParameter::parseArrivalSpeed(oc.getString("arrivalspeed"), "option", "arrivalspeed", p.arrivalSpeed, p.arrivalSpeedProcedure, error)) {
230  WRITE_ERROR(error);
231  ok = false;
232  }
233  return ok;
234 }
235 
236 
237 
238 
239 /* -------------------------------------------------------------------------
240  * main
241  * ----------------------------------------------------------------------- */
242 int
243 main(int argc, char** argv) {
245  // give some application descriptions
246  oc.setApplicationDescription("Importer of O/D-matrices for the road traffic simulation SUMO.");
247  oc.setApplicationName("od2trips", "SUMO od2trips Version " VERSION_STRING);
248  int ret = 0;
249  try {
250  // initialise subsystems
251  XMLSubSys::init();
252  fillOptions();
253  OptionsIO::setArgs(argc, argv);
255  if (oc.processMetaOptions(argc < 2)) {
257  return 0;
258  }
259  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
261  if (!checkOptions()) {
262  throw ProcessError();
263  }
265  // load the districts
266  // check whether the user gave a net filename
267  if (!oc.isSet("taz-files")) {
268  throw ProcessError("You must supply a TAZ, network or districts file ('-n').");
269  }
270  // get the file name and set it
271  ODDistrictCont districts;
272  districts.loadDistricts(oc.getStringVector("taz-files"));
273  if (districts.size() == 0) {
274  throw ProcessError("No districts loaded.");
275  }
276  // load the matrix
277  ODMatrix matrix(districts);
278  matrix.loadMatrix(oc);
279  if (matrix.getNumLoaded() == 0) {
280  throw ProcessError("No vehicles loaded.");
281  }
282  if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
283  throw ProcessError("Loading failed.");
284  }
285  WRITE_MESSAGE(toString(matrix.getNumLoaded()) + " vehicles loaded.");
286  // apply a curve if wished
287  if (oc.isSet("timeline")) {
288  matrix.applyCurve(matrix.parseTimeLine(oc.getStringVector("timeline"), oc.getBool("timeline.day-in-hours")));
289  }
290  // write
291  bool haveOutput = false;
292  if (OutputDevice::createDeviceByOption("output-file", "routes", "routes_file.xsd")) {
293  matrix.write(string2time(oc.getString("begin")), string2time(oc.getString("end")),
294  OutputDevice::getDeviceByOption("output-file"),
295  oc.getBool("spread.uniform"), oc.getBool("different-source-sink"),
296  oc.getBool("ignore-vehicle-type"),
297  oc.getString("prefix"), !oc.getBool("no-step-log"),
298  oc.getBool("pedestrians"),
299  oc.getBool("persontrips"));
300  haveOutput = true;
301  }
302  if (OutputDevice::createDeviceByOption("flow-output", "routes", "routes_file.xsd")) {
303  matrix.writeFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
304  OutputDevice::getDeviceByOption("flow-output"),
305  oc.getBool("ignore-vehicle-type"), oc.getString("prefix"),
306  oc.getBool("flow-output.probability"));
307  haveOutput = true;
308  }
309  if (!haveOutput) {
310  throw ProcessError("No output file given.");
311  }
312  WRITE_MESSAGE(toString(matrix.getNumDiscarded()) + " vehicles discarded.");
313  WRITE_MESSAGE(toString(matrix.getNumWritten()) + " vehicles written.");
314  } catch (const ProcessError& e) {
315  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
316  WRITE_ERROR(e.what());
317  }
318  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
319  ret = 1;
320 #ifndef _DEBUG
321  } catch (const std::exception& e) {
322  if (std::string(e.what()) != std::string("")) {
323  WRITE_ERROR(e.what());
324  }
325  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
326  ret = 1;
327  } catch (...) {
328  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
329  ret = 1;
330 #endif
331  }
333  if (ret == 0) {
334  std::cout << "Success." << std::endl;
335  }
336  return ret;
337 }
338 
339 
340 
341 /****************************************************************************/
342 
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, bool pedestrians, bool persontrips)
Writes the vehicles stored in the matrix assigning the sources and sinks.
Definition: ODMatrix.cpp:216
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:81
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:53
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:75
bool checkOptions()
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:47
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:82
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:71
double getNumWritten() const
Returns the number of written vehicles.
Definition: ODMatrix.cpp:519
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:64
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.
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)
double getNumDiscarded() const
Returns the number of discarded vehicles.
Definition: ODMatrix.cpp:525
ArrivalSpeedDefinition arrivalSpeedProcedure
Information how the vehicle&#39;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:46
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:61
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
void loadMatrix(OptionsCont &oc)
read a matrix in one of several formats
Definition: ODMatrix.cpp:560
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)
double departSpeed
(optional) The initial speed of the vehicle
DepartSpeedDefinition departSpeedProcedure
Information how the vehicle&#39;s initial speed shall be chosen.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
An O/D (origin/destination) matrix.
Definition: ODMatrix.h:75
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: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 VERSION_STRING
Definition: version.h:1
#define SUMOTIME_MAXSTRING
Definition: SUMOTime.h:45
int departLane
(optional) The lane the vehicle shall depart from (index in edge)
void loadDistricts(std::vector< std::string > files)
load districts from files
double arrivalPos
(optional) The position the vehicle shall arrive on
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
double departPos
(optional) The position the vehicle shall depart from
int main(int argc, char **argv)
Structure representing possible vehicle parameter.
static bool parseDepartPos(const std::string &val, const std::string &element, const std::string &id, double &pos, DepartPosDefinition &dpd, std::string &error)
Validates a given departPos value.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
static bool parseArrivalSpeed(const std::string &val, const std::string &element, const std::string &id, double &speed, ArrivalSpeedDefinition &asd, std::string &error)
Validates a given arrivalSpeed value.
void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:84
A storage for options typed value containers)
Definition: OptionsCont.h:98
double getNumLoaded() const
Returns the number of loaded vehicles.
Definition: ODMatrix.cpp:513
static bool parseArrivalPos(const std::string &val, const std::string &element, const std::string &id, double &pos, ArrivalPosDefinition &apd, std::string &error)
Validates a given arrivalPos value.
static void initRandGlobal(std::mt19937 *which=0)
Reads the given random number options and initialises the random number generator in accordance...
Definition: RandHelper.cpp:76
void applyCurve(const Distribution_Points &ps)
Splits the stored cells dividing them on the given time line.
Definition: ODMatrix.cpp:547
static bool parseDepartSpeed(const std::string &val, const std::string &element, const std::string &id, double &speed, DepartSpeedDefinition &dsd, std::string &error)
Validates a given departSpeed value.
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.
double arrivalSpeed
(optional) The final speed of the vehicle (not used yet)
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:200
static void initOutputOptions()
Definition: MsgHandler.cpp:192
void writeFlows(const SUMOTime begin, const SUMOTime end, OutputDevice &dev, const bool noVtype, const std::string &prefix, bool asProbability=false)
Writes the flows stored in the matrix.
Definition: ODMatrix.cpp:305
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:623
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 stored 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.