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 /****************************************************************************/
11 // Main for OD2TRIPS
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
14 // Copyright (C) 2002-2014 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>
58 #include <od2trips/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("net-file", 'n', new Option_FileName());
92  oc.addSynonyme("net-file", "net");
93  oc.addDescription("net-file", "Input", "Loads network (districts) from FILE");
94 
95  oc.doRegister("od-matrix-files", 'd', new Option_FileName());
96  oc.addSynonyme("od-matrix-files", "od-files");
97  oc.addSynonyme("od-matrix-files", "od");
98  oc.addDescription("od-matrix-files", "Input", "Loads O/D-files from FILE(s)");
99 
100  oc.doRegister("od-amitran-files", new Option_FileName());
101  oc.addSynonyme("od-amitran-files", "amitran-files");
102  oc.addSynonyme("od-amitran-files", "amitran");
103  oc.addDescription("od-amitran-files", "Input", "Loads O/D-matrix in Amitran format from FILE(s)");
104 
105 
106  // register the file output options
107  oc.doRegister("output-file", 'o', new Option_FileName());
108  oc.addSynonyme("output-file", "output", true);
109  oc.addDescription("output-file", "Output", "Writes trip definitions into FILE");
110 
111  oc.doRegister("flow-output", new Option_FileName());
112  oc.addDescription("flow-output", "Output", "Writes flow definitions into FILE");
113 
114  oc.doRegister("ignore-vehicle-type", new Option_Bool(false));
115  oc.addSynonyme("ignore-vehicle-type", "no-vtype", true);
116  oc.addDescription("ignore-vehicle-type", "Output", "Does not save vtype information");
117 
118 
119  // register the time settings
120  oc.doRegister("begin", 'b', new Option_String("0", "TIME"));
121  oc.addDescription("begin", "Time", "Defines the begin time; Previous trips will be discarded");
122 
123  oc.doRegister("end", 'e', new Option_String(SUMOTIME_MAXSTRING, "TIME"));
124  oc.addDescription("end", "Time", "Defines the end time; Later trips will be discarded; Defaults to the maximum time that SUMO can represent");
125 
126 
127  // register the data processing options
128  oc.doRegister("scale", 's', new Option_Float(1));
129  oc.addDescription("scale", "Processing", "Scales the loaded flows by FLOAT");
130 
131  oc.doRegister("spread.uniform", new Option_Bool(false));
132  oc.addDescription("spread.uniform", "Processing", "Spreads trips uniformly over each time period");
133 
134  oc.doRegister("vtype", new Option_String(""));
135  oc.addDescription("vtype", "Processing", "Defines the name of the vehicle type to use");
136 
137  oc.doRegister("prefix", new Option_String(""));
138  oc.addDescription("prefix", "Processing", "Defines the prefix for vehicle names");
139 
140  oc.doRegister("timeline", new Option_String());
141  oc.addDescription("timeline", "Processing", "Uses STR as a timeline definition");
142 
143  oc.doRegister("timeline.day-in-hours", new Option_Bool(false));
144  oc.addDescription("timeline.day-in-hours", "Processing", "Uses STR as a 24h-timeline definition");
145 
146  oc.doRegister("ignore-errors", new Option_Bool(false)); // !!! describe, document
147  oc.addSynonyme("ignore-errors", "dismiss-loading-errors", true);
148  oc.addDescription("ignore-errors", "Processing", "Continue on broken input");
149 
150  oc.doRegister("no-step-log", new Option_Bool(false));
151  oc.addDescription("no-step-log", "Processing", "Disable console output of current time step");
152 
153 
154  // register defaults options
155  oc.doRegister("departlane", new Option_String("free"));
156  oc.addDescription("departlane", "Defaults", "Assigns a default depart lane");
157 
158  oc.doRegister("departpos", new Option_String());
159  oc.addDescription("departpos", "Defaults", "Assigns a default depart position");
160 
161  oc.doRegister("departspeed", new Option_String("max"));
162  oc.addDescription("departspeed", "Defaults", "Assigns a default depart speed");
163 
164  oc.doRegister("arrivallane", new Option_String());
165  oc.addDescription("arrivallane", "Defaults", "Assigns a default arrival lane");
166 
167  oc.doRegister("arrivalpos", new Option_String());
168  oc.addDescription("arrivalpos", "Defaults", "Assigns a default arrival position");
169 
170  oc.doRegister("arrivalspeed", new Option_String());
171  oc.addDescription("arrivalspeed", "Defaults", "Assigns a default arrival speed");
172 
173  // add rand options
175 }
176 
177 bool
180  bool ok = true;
181  if (!oc.isSet("net-file")) {
182  WRITE_ERROR("No net input file (-n) specified.");
183  ok = false;
184  }
185  if (!oc.isSet("od-matrix-files") && !oc.isSet("od-amitran-files")) {
186  WRITE_ERROR("No input specified.");
187  ok = false;
188  }
189  if (!oc.isSet("output-file")) {
190  WRITE_ERROR("No trip table output file (-o) specified.");
191  ok = false;
192  }
193  //
195  std::string error;
196  if (oc.isSet("departlane") && !SUMOVehicleParameter::parseDepartLane(oc.getString("departlane"), "option", "departlane", p.departLane, p.departLaneProcedure, error)) {
197  WRITE_ERROR(error);
198  ok = false;
199  }
200  if (oc.isSet("departpos") && !SUMOVehicleParameter::parseDepartPos(oc.getString("departpos"), "option", "departpos", p.departPos, p.departPosProcedure, error)) {
201  WRITE_ERROR(error);
202  ok = false;
203  }
204  if (oc.isSet("departspeed") && !SUMOVehicleParameter::parseDepartSpeed(oc.getString("departspeed"), "option", "departspeed", p.departSpeed, p.departSpeedProcedure, error)) {
205  WRITE_ERROR(error);
206  ok = false;
207  }
208  if (oc.isSet("arrivallane") && !SUMOVehicleParameter::parseArrivalLane(oc.getString("arrivallane"), "option", "arrivallane", p.arrivalLane, p.arrivalLaneProcedure, error)) {
209  WRITE_ERROR(error);
210  ok = false;
211  }
212  if (oc.isSet("arrivalpos") && !SUMOVehicleParameter::parseArrivalPos(oc.getString("arrivalpos"), "option", "arrivalpos", p.arrivalPos, p.arrivalPosProcedure, error)) {
213  WRITE_ERROR(error);
214  ok = false;
215  }
216  if (oc.isSet("arrivalspeed") && !SUMOVehicleParameter::parseArrivalSpeed(oc.getString("arrivalspeed"), "option", "arrivalspeed", p.arrivalSpeed, p.arrivalSpeedProcedure, error)) {
217  WRITE_ERROR(error);
218  ok = false;
219  }
220  return ok;
221 }
222 
223 
224 
225 
226 /* -------------------------------------------------------------------------
227  * main
228  * ----------------------------------------------------------------------- */
229 int
230 main(int argc, char** argv) {
232  // give some application descriptions
233  oc.setApplicationDescription("Importer of O/D-matrices for the road traffic simulation SUMO.");
234  oc.setApplicationName("od2trips", "SUMO od2trips Version " + (std::string)VERSION_STRING);
235  int ret = 0;
236  try {
237  // initialise subsystems
238  XMLSubSys::init();
239  fillOptions();
240  OptionsIO::getOptions(true, argc, argv);
241  if (oc.processMetaOptions(argc < 2)) {
243  return 0;
244  }
245  XMLSubSys::setValidation(oc.getString("xml-validation"), oc.getString("xml-validation.net"));
247  if (!checkOptions()) {
248  throw ProcessError();
249  }
251  // load the districts
252  // check whether the user gave a net filename
253  if (!oc.isSet("net-file")) {
254  throw ProcessError("You must supply a network or districts file ('-n').");
255  }
256  // get the file name and set it
257  ODDistrictCont districts;
258  districts.loadDistricts(oc.getString("net-file"));
259  if (districts.size() == 0) {
260  throw ProcessError("No districts loaded.");
261  }
262  // load the matrix
263  ODMatrix matrix(districts);
264  matrix.loadMatrix(oc);
265  if (matrix.getNoLoaded() == 0) {
266  throw ProcessError("No vehicles loaded.");
267  }
268  if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
269  throw ProcessError("Loading failed.");
270  }
271  WRITE_MESSAGE(toString(matrix.getNoLoaded()) + " vehicles loaded.");
272  // apply a curve if wished
273  if (oc.isSet("timeline")) {
274  matrix.applyCurve(matrix.parseTimeLine(oc.getStringVector("timeline"), oc.getBool("timeline.day-in-hours")));
275  }
276  // write
277  bool haveOutput = false;
278  if (OutputDevice::createDeviceByOption("output-file", "routes", "routes_file.xsd")) {
279  matrix.write(string2time(oc.getString("begin")), string2time(oc.getString("end")),
280  OutputDevice::getDeviceByOption("output-file"),
281  oc.getBool("spread.uniform"), oc.getBool("ignore-vehicle-type"),
282  oc.getString("prefix"), !oc.getBool("no-step-log"));
283  haveOutput = true;
284  }
285  if (OutputDevice::createDeviceByOption("flow-output", "routes", "routes_file.xsd")) {
286  matrix.writeFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
287  OutputDevice::getDeviceByOption("flow-output"),
288  oc.getBool("ignore-vehicle-type"), oc.getString("prefix"));
289  haveOutput = true;
290  }
291  if (!haveOutput) {
292  throw ProcessError("No output file given.");
293  }
294  WRITE_MESSAGE(toString(matrix.getNoDiscarded()) + " vehicles discarded.");
295  WRITE_MESSAGE(toString(matrix.getNoWritten()) + " vehicles written.");
296  } catch (const ProcessError& e) {
297  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
298  WRITE_ERROR(e.what());
299  }
300  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
301  ret = 1;
302 #ifndef _DEBUG
303  } catch (const std::exception& e) {
304  if (std::string(e.what()) != std::string("")) {
305  WRITE_ERROR(e.what());
306  }
307  MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
308  ret = 1;
309  } catch (...) {
310  MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
311  ret = 1;
312 #endif
313  }
315  if (ret == 0) {
316  std::cout << "Success." << std::endl;
317  }
318  return ret;
319 }
320 
321 
322 
323 /****************************************************************************/
324