Eclipse 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-2022 German Aerospace Center (DLR) and others.
4// This program and the accompanying materials are made available under the
5// terms of the Eclipse Public License 2.0 which is available at
6// https://www.eclipse.org/legal/epl-2.0/
7// This Source Code may also be made available under the following Secondary
8// Licenses when the conditions for such availability set forth in the Eclipse
9// Public License 2.0 are satisfied: GNU General Public License, version 2
10// or later which is available at
11// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13/****************************************************************************/
22// Main for OD2TRIPS
23/****************************************************************************/
24#include <config.h>
25
26#ifdef HAVE_VERSION_H
27#include <version.h>
28#endif
29
30#include <iostream>
31#include <algorithm>
32#include <cmath>
33#include <cstdlib>
34#include <string>
43#include <utils/xml/XMLSubSys.h>
45#include <od/ODDistrictCont.h>
47#include <od/ODMatrix.h>
55
56
57// ===========================================================================
58// functions
59// ===========================================================================
60void
63 oc.addCallExample("-c <CONFIGURATION>", "run with configuration file");
64
65 // insert options sub-topics
66 SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
67 oc.addOptionSubTopic("Input");
68 oc.addOptionSubTopic("Output");
69 oc.addOptionSubTopic("Time");
70 oc.addOptionSubTopic("Processing");
71 oc.addOptionSubTopic("Defaults");
72 SystemFrame::addReportOptions(oc); // fill this subtopic, too
73
74
75 // register the file input options
76 oc.doRegister("taz-files", 'n', new Option_FileName());
77 oc.addSynonyme("taz-files", "taz");
78 oc.addSynonyme("taz-files", "net-file");
79 oc.addSynonyme("taz-files", "net");
80 oc.addDescription("taz-files", "Input", "Loads TAZ (districts; also from networks) from FILE(s)");
81
82 oc.doRegister("od-matrix-files", 'd', new Option_FileName());
83 oc.addSynonyme("od-matrix-files", "od-files");
84 oc.addSynonyme("od-matrix-files", "od");
85 oc.addDescription("od-matrix-files", "Input", "Loads O/D-files from FILE(s)");
86
87 oc.doRegister("od-amitran-files", new Option_FileName());
88 oc.addSynonyme("od-amitran-files", "amitran-files");
89 oc.addSynonyme("od-amitran-files", "amitran");
90 oc.addDescription("od-amitran-files", "Input", "Loads O/D-matrix in Amitran format from FILE(s)");
91
92 oc.doRegister("tazrelation-files", 'z', new Option_FileName());
93 oc.addDescription("tazrelation-files", "Input", "Loads O/D-matrix in tazRelation format from FILE(s)");
94
95 oc.doRegister("tazrelation-attribute", new Option_String("count"));
96 oc.addSynonyme("tazrelation-attribute", "attribute");
97 oc.addDescription("tazrelation-attribute", "Input", "Define data attribute for loading counts (default 'count')");
98
99 // register the file output options
100 oc.doRegister("output-file", 'o', new Option_FileName());
101 oc.addSynonyme("output-file", "output", true);
102 oc.addDescription("output-file", "Output", "Writes trip definitions into FILE");
103
104 oc.doRegister("flow-output", new Option_FileName());
105 oc.addDescription("flow-output", "Output", "Writes flow definitions into FILE");
106
107 oc.doRegister("flow-output.probability", new Option_Bool(false));
108 oc.addDescription("flow-output.probability", "Output", "Writes probabilistic flow instead of evenly spaced flow");
109
110 oc.doRegister("pedestrians", new Option_Bool(false));
111 oc.addDescription("pedestrians", "Output", "Writes pedestrians instead of vehicles");
112
113 oc.doRegister("persontrips", new Option_Bool(false));
114 oc.addDescription("persontrips", "Output", "Writes persontrips instead of vehicles");
115
116 oc.doRegister("persontrips.modes", new Option_StringVector());
117 oc.addDescription("persontrips.modes", "Output", "Add modes attribute to personTrips");
118
119 oc.doRegister("ignore-vehicle-type", new Option_Bool(false));
120 oc.addSynonyme("ignore-vehicle-type", "no-vtype", true);
121 oc.addDescription("ignore-vehicle-type", "Output", "Does not save vtype information");
122
123 oc.doRegister("junctions", new Option_Bool(false));
124 oc.addDescription("junctions", "Output", "Writes trips between junctions");
125
126
127 // register the time settings
128 oc.doRegister("begin", 'b', new Option_String("0", "TIME"));
129 oc.addDescription("begin", "Time", "Defines the begin time; Previous trips will be discarded");
130
131 oc.doRegister("end", 'e', new Option_String("-1", "TIME"));
132 oc.addDescription("end", "Time", "Defines the end time; Later trips will be discarded; Defaults to the maximum time that SUMO can represent");
133
134
135 // register the data processing options
136 oc.doRegister("scale", 's', new Option_Float(1));
137 oc.addDescription("scale", "Processing", "Scales the loaded flows by FLOAT");
138
139 oc.doRegister("spread.uniform", new Option_Bool(false));
140 oc.addDescription("spread.uniform", "Processing", "Spreads trips uniformly over each time period");
141
142 oc.doRegister("different-source-sink", new Option_Bool(false));
143 oc.addDescription("different-source-sink", "Processing", "Always choose source and sink edge which are not identical");
144
145 oc.doRegister("vtype", new Option_String(""));
146 oc.addDescription("vtype", "Processing", "Defines the name of the vehicle type to use");
147
148 oc.doRegister("prefix", new Option_String(""));
149 oc.addDescription("prefix", "Processing", "Defines the prefix for vehicle names");
150
151 oc.doRegister("timeline", new Option_StringVector());
152 oc.addDescription("timeline", "Processing", "Uses STR[] as a timeline definition");
153
154 oc.doRegister("timeline.day-in-hours", new Option_Bool(false));
155 oc.addDescription("timeline.day-in-hours", "Processing", "Uses STR as a 24h-timeline definition");
156
157 oc.doRegister("ignore-errors", new Option_Bool(false)); // !!! describe, document
158 oc.addSynonyme("ignore-errors", "dismiss-loading-errors", true);
159 oc.addDescription("ignore-errors", "Report", "Continue on broken input");
160
161 oc.doRegister("no-step-log", new Option_Bool(false));
162 oc.addDescription("no-step-log", "Processing", "Disable console output of current time step");
163
164
165 // register defaults options
166 oc.doRegister("departlane", new Option_String("free"));
167 oc.addDescription("departlane", "Defaults", "Assigns a default depart lane");
168
169 oc.doRegister("departpos", new Option_String());
170 oc.addDescription("departpos", "Defaults", "Assigns a default depart position");
171
172 oc.doRegister("departspeed", new Option_String("max"));
173 oc.addDescription("departspeed", "Defaults", "Assigns a default depart speed");
174
175 oc.doRegister("arrivallane", new Option_String());
176 oc.addDescription("arrivallane", "Defaults", "Assigns a default arrival lane");
177
178 oc.doRegister("arrivalpos", new Option_String());
179 oc.addDescription("arrivalpos", "Defaults", "Assigns a default arrival position");
180
181 oc.doRegister("arrivalspeed", new Option_String());
182 oc.addDescription("arrivalspeed", "Defaults", "Assigns a default arrival speed");
183
184 // add rand options
186}
187
188bool
192 bool ok = true;
193 if (!oc.isSet("taz-files")) {
194 WRITE_ERROR(TL("No TAZ input file (-n) specified."));
195 ok = false;
196 }
197 if (!oc.isSet("od-matrix-files") && !oc.isSet("od-amitran-files") && !oc.isSet("tazrelation-files")) {
198 WRITE_ERROR(TL("No input specified."));
199 ok = false;
200 }
201 if (!oc.isSet("output-file") && !oc.isSet("flow-output")) {
202 WRITE_ERROR(TL("No trip table output file (-o) or flow-output is specified."));
203 ok = false;
204 }
205 if (oc.getBool("pedestrians") && oc.getBool("persontrips")) {
206 WRITE_ERROR(TL("Only one of the the options 'pedestrians' and 'persontrips' may be set."));
207 ok = false;
208 }
209 //
211 std::string error;
212 if (oc.isSet("departlane") && !SUMOVehicleParameter::parseDepartLane(oc.getString("departlane"), "option", "departlane", p.departLane, p.departLaneProcedure, error)) {
213 WRITE_ERROR(error);
214 ok = false;
215 }
216 if (oc.isSet("departpos") && !SUMOVehicleParameter::parseDepartPos(oc.getString("departpos"), "option", "departpos", p.departPos, p.departPosProcedure, error)) {
217 WRITE_ERROR(error);
218 ok = false;
219 }
220 if (oc.isSet("departspeed") && !SUMOVehicleParameter::parseDepartSpeed(oc.getString("departspeed"), "option", "departspeed", p.departSpeed, p.departSpeedProcedure, error)) {
221 WRITE_ERROR(error);
222 ok = false;
223 }
224 if (oc.isSet("arrivallane") && !SUMOVehicleParameter::parseArrivalLane(oc.getString("arrivallane"), "option", "arrivallane", p.arrivalLane, p.arrivalLaneProcedure, error)) {
225 WRITE_ERROR(error);
226 ok = false;
227 }
228 if (oc.isSet("arrivalpos") && !SUMOVehicleParameter::parseArrivalPos(oc.getString("arrivalpos"), "option", "arrivalpos", p.arrivalPos, p.arrivalPosProcedure, error)) {
229 WRITE_ERROR(error);
230 ok = false;
231 }
232 if (oc.isSet("arrivalspeed") && !SUMOVehicleParameter::parseArrivalSpeed(oc.getString("arrivalspeed"), "option", "arrivalspeed", p.arrivalSpeed, p.arrivalSpeedProcedure, error)) {
233 WRITE_ERROR(error);
234 ok = false;
235 }
237 return ok;
238}
239
240
241
242
243/* -------------------------------------------------------------------------
244 * main
245 * ----------------------------------------------------------------------- */
246int
247main(int argc, char** argv) {
249 // give some application descriptions
250 oc.setApplicationDescription("Importer of O/D-matrices for the microscopic, multi-modal traffic simulation SUMO.");
251 oc.setApplicationName("od2trips", "Eclipse SUMO od2trips Version " VERSION_STRING);
252 int ret = 0;
253 try {
254 // initialise subsystems
256 fillOptions();
257 OptionsIO::setArgs(argc, argv);
259 if (oc.processMetaOptions(argc < 2)) {
261 return 0;
262 }
263 XMLSubSys::setValidation(oc.getString("xml-validation"), "never", "never");
265 if (!checkOptions()) {
266 throw ProcessError();
267 }
269 // load the districts
270 // check whether the user gave a net filename
271 if (!oc.isSet("taz-files")) {
272 throw ProcessError("You must supply a TAZ, network or districts file ('-n').");
273 }
274 // get the file name and set it
275 ODDistrictCont districts;
276 districts.loadDistricts(oc.getStringVector("taz-files"));
277 if (districts.size() == 0) {
278 throw ProcessError("No districts loaded.");
279 }
280 // load the matrix
281 ODMatrix matrix(districts, oc.getFloat("scale"));
282 matrix.loadMatrix(oc);
283 if (matrix.getNumLoaded() == 0) {
284 throw ProcessError("No vehicles loaded.");
285 }
286 if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
287 throw ProcessError("Loading failed.");
288 }
289 WRITE_MESSAGE(toString(matrix.getNumLoaded()) + " vehicles loaded.");
290 // apply a curve if wished
291 if (oc.isSet("timeline")) {
292 matrix.applyCurve(matrix.parseTimeLine(oc.getStringVector("timeline"), oc.getBool("timeline.day-in-hours")));
293 }
294 const std::string modes = toString(oc.getStringVector("persontrips.modes"));
295 // write
296 bool haveOutput = false;
297 const SUMOTime end = oc.isDefault("end") ? SUMOTime_MAX : string2time(oc.getString("end"));
298 if (OutputDevice::createDeviceByOption("output-file", "routes", "routes_file.xsd")) {
299 matrix.write(string2time(oc.getString("begin")), end,
300 OutputDevice::getDeviceByOption("output-file"),
301 oc.getBool("spread.uniform"), oc.getBool("different-source-sink"),
302 oc.getBool("ignore-vehicle-type"),
303 oc.getString("prefix"), !oc.getBool("no-step-log"),
304 oc.getBool("pedestrians"),
305 oc.getBool("persontrips"), modes);
306 haveOutput = true;
307 }
308 if (OutputDevice::createDeviceByOption("flow-output", "routes", "routes_file.xsd")) {
309 matrix.writeFlows(string2time(oc.getString("begin")), end,
310 OutputDevice::getDeviceByOption("flow-output"),
311 oc.getBool("ignore-vehicle-type"), oc.getString("prefix"),
312 oc.getBool("flow-output.probability"), oc.getBool("pedestrians"),
313 oc.getBool("persontrips"), modes);
314 haveOutput = true;
315 }
316 if (!haveOutput) {
317 throw ProcessError("No output file given.");
318 }
319 WRITE_MESSAGE(toString(matrix.getNumDiscarded()) + " vehicles discarded.");
320 WRITE_MESSAGE(toString(matrix.getNumWritten()) + " vehicles written.");
321 } catch (const ProcessError& e) {
322 if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
323 WRITE_ERROR(e.what());
324 }
325 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
326 ret = 1;
327#ifndef _DEBUG
328 } catch (const std::exception& e) {
329 if (std::string(e.what()) != std::string("")) {
330 WRITE_ERROR(e.what());
331 }
332 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
333 ret = 1;
334 } catch (...) {
335 MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
336 ret = 1;
337#endif
338 }
340 if (ret == 0) {
341 std::cout << "Success." << std::endl;
342 }
343 return ret;
344}
345
346
347/****************************************************************************/
long long int SUMOTime
Definition: GUI.h:36
#define WRITE_MESSAGE(msg)
Definition: MsgHandler.h:267
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:274
#define TL(string)
Definition: MsgHandler.h:282
SUMOTime string2time(const std::string &r)
convert string to SUMOTime
Definition: SUMOTime.cpp:45
#define SUMOTime_MAX
Definition: SUMOTime.h:33
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
static MsgHandler * getErrorInstance()
Returns the instance to add errors to.
Definition: MsgHandler.cpp:79
virtual void inform(std::string msg, bool addType=true)
adds a new error to the list
Definition: MsgHandler.cpp:116
static void setupI18n(const std::string &locale="")
set up gettext stuff
Definition: MsgHandler.cpp:228
static void initOutputOptions()
init output options
Definition: MsgHandler.cpp:255
int size() const
Returns the number of stored items within the container.
A container for districts.
void loadDistricts(std::vector< std::string > files)
load districts from files
An O/D (origin/destination) matrix.
Definition: ODMatrix.h:68
double getNumLoaded() const
Returns the number of loaded vehicles.
Definition: ODMatrix.cpp:595
void writeFlows(const SUMOTime begin, const SUMOTime end, OutputDevice &dev, const bool noVtype, const std::string &prefix, bool asProbability=false, bool pedestrians=false, bool persontrips=false, const std::string &modes="")
Writes the flows stored in the matrix.
Definition: ODMatrix.cpp:338
void applyCurve(const Distribution_Points &ps)
Splits the stored cells dividing them on the given time line.
Definition: ODMatrix.cpp:629
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, const std::string &modes)
Writes the vehicles stored in the matrix assigning the sources and sinks.
Definition: ODMatrix.cpp:232
Distribution_Points parseTimeLine(const std::vector< std::string > &def, bool timelineDayInHours)
split the given timeline
Definition: ODMatrix.cpp:727
void loadMatrix(OptionsCont &oc)
read a matrix in one of several formats
Definition: ODMatrix.cpp:642
double getNumWritten() const
Returns the number of written vehicles.
Definition: ODMatrix.cpp:601
double getNumDiscarded() const
Returns the number of discarded vehicles.
Definition: ODMatrix.cpp:607
A storage for options typed value containers)
Definition: OptionsCont.h:89
void addDescription(const std::string &name, const std::string &subtopic, const std::string &description)
Adds a description for an option.
void doRegister(const std::string &name, Option *v)
Adds an option under the given name.
Definition: OptionsCont.cpp:76
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
double getFloat(const std::string &name) const
Returns the double-value of the named option (only for Option_Float)
void setApplicationName(const std::string &appName, const std::string &fullName)
Sets the application name.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void addSynonyme(const std::string &name1, const std::string &name2, bool isDeprecated=false)
Adds a synonyme for an options name (any order)
Definition: OptionsCont.cpp:97
bool isDefault(const std::string &name) const
Returns the information whether the named option has still the default value.
void setApplicationDescription(const std::string &appDesc)
Sets the application description.
void addOptionSubTopic(const std::string &topic)
Adds an option subtopic.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const StringVector & getStringVector(const std::string &name) const
Returns the list of string-value of the named option (only for Option_StringVector)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:59
bool processMetaOptions(bool missingOptions)
Checks for help and configuration output, returns whether we should exit.
void addCallExample(const std::string &example, const std::string &desc)
Add a call example.
static void setArgs(int argc, char **argv)
Stores the command line arguments for later parsing.
Definition: OptionsIO.cpp:58
static void getOptions(const bool commandLineOnly=false)
Parses the command line arguments and loads the configuration.
Definition: OptionsIO.cpp:74
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.
static OutputDevice & getDeviceByOption(const std::string &name)
Returns the device described by the option.
static void insertRandOptions()
Initialises the given options container with random number options.
Definition: RandHelper.cpp:58
static void initRandGlobal(SumoRNG *which=nullptr)
Reads the given random number options and initialises the random number generator in accordance.
Definition: RandHelper.cpp:87
Structure representing possible vehicle parameter.
int departLane
(optional) The lane the vehicle shall depart from (index in edge)
ArrivalSpeedDefinition arrivalSpeedProcedure
Information how the vehicle's end speed shall be chosen.
double departSpeed
(optional) The initial speed of the vehicle
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.
ArrivalLaneDefinition arrivalLaneProcedure
Information how the vehicle shall choose the lane to arrive on.
DepartLaneDefinition departLaneProcedure
Information how the vehicle shall choose the lane to depart from.
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 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 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.
double departPos
(optional) The position the vehicle shall depart from
DepartSpeedDefinition departSpeedProcedure
Information how the vehicle's initial speed shall be chosen.
double arrivalPos
(optional) The position the vehicle shall arrive on
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.
ArrivalPosDefinition arrivalPosProcedure
Information how the vehicle shall choose the arrival position.
double arrivalSpeed
(optional) The final speed of the vehicle (not used yet)
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.
DepartPosDefinition departPosProcedure
Information how the vehicle shall choose the departure position.
static void close()
Closes all of an applications subsystems.
static void addConfigurationOptions(OptionsCont &oc)
Adds configuration options to the given container.
Definition: SystemFrame.cpp:38
static void addReportOptions(OptionsCont &oc)
Adds reporting options to the given container.
Definition: SystemFrame.cpp:67
static bool checkOptions()
checks shared options and sets StdDefs
static void setValidation(const std::string &validationScheme, const std::string &netValidationScheme, const std::string &routeValidationScheme)
Enables or disables validation.
Definition: XMLSubSys.cpp:65
static void init()
Initialises the xml-subsystem.
Definition: XMLSubSys.cpp:54
bool checkOptions()
void fillOptions()
int main(int argc, char **argv)