forked from eclipse-sumo/sumo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathod2trips_main.cpp
336 lines (291 loc) · 14.3 KB
/
od2trips_main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/****************************************************************************/
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
// Copyright (C) 2002-2020 German Aerospace Center (DLR) and others.
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0/
// This Source Code may also be made available under the following Secondary
// Licenses when the conditions for such availability set forth in the Eclipse
// Public License 2.0 are satisfied: GNU General Public License, version 2
// or later which is available at
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
/****************************************************************************/
/// @file od2trips_main.cpp
/// @author Daniel Krajzewicz
/// @author Jakob Erdmann
/// @author Michael Behrisch
/// @author Laura Bieker
/// @author Yun-Pang Floetteroed
/// @date Thu, 12 September 2002
///
// Main for OD2TRIPS
/****************************************************************************/
#include <config.h>
#ifdef HAVE_VERSION_H
#include <version.h>
#endif
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <string>
#include <utils/options/Option.h>
#include <utils/options/OptionsCont.h>
#include <utils/options/OptionsIO.h>
#include <utils/common/MsgHandler.h>
#include <utils/common/UtilExceptions.h>
#include <utils/common/SystemFrame.h>
#include <utils/common/RandHelper.h>
#include <utils/common/ToString.h>
#include <utils/xml/XMLSubSys.h>
#include <utils/common/StringUtils.h>
#include <od/ODDistrictCont.h>
#include <od/ODDistrictHandler.h>
#include <od/ODMatrix.h>
#include <utils/common/StringUtils.h>
#include <utils/common/SUMOTime.h>
#include <utils/common/StringTokenizer.h>
#include <utils/common/FileHelpers.h>
#include <utils/vehicle/SUMOVehicleParameter.h>
#include <utils/importio/LineReader.h>
#include <utils/iodevices/OutputDevice.h>
// ===========================================================================
// functions
// ===========================================================================
void
fillOptions() {
OptionsCont& oc = OptionsCont::getOptions();
oc.addCallExample("-c <CONFIGURATION>", "run with configuration file");
// insert options sub-topics
SystemFrame::addConfigurationOptions(oc); // fill this subtopic, too
oc.addOptionSubTopic("Input");
oc.addOptionSubTopic("Output");
oc.addOptionSubTopic("Time");
oc.addOptionSubTopic("Processing");
oc.addOptionSubTopic("Defaults");
SystemFrame::addReportOptions(oc); // fill this subtopic, too
// register the file input options
oc.doRegister("taz-files", 'n', new Option_FileName());
oc.addSynonyme("taz-files", "taz");
oc.addSynonyme("taz-files", "net-file");
oc.addSynonyme("taz-files", "net");
oc.addDescription("taz-files", "Input", "Loads TAZ (districts; also from networks) from FILE(s)");
oc.doRegister("od-matrix-files", 'd', new Option_FileName());
oc.addSynonyme("od-matrix-files", "od-files");
oc.addSynonyme("od-matrix-files", "od");
oc.addDescription("od-matrix-files", "Input", "Loads O/D-files from FILE(s)");
oc.doRegister("od-amitran-files", new Option_FileName());
oc.addSynonyme("od-amitran-files", "amitran-files");
oc.addSynonyme("od-amitran-files", "amitran");
oc.addDescription("od-amitran-files", "Input", "Loads O/D-matrix in Amitran format from FILE(s)");
// register the file output options
oc.doRegister("output-file", 'o', new Option_FileName());
oc.addSynonyme("output-file", "output", true);
oc.addDescription("output-file", "Output", "Writes trip definitions into FILE");
oc.doRegister("flow-output", new Option_FileName());
oc.addDescription("flow-output", "Output", "Writes flow definitions into FILE");
oc.doRegister("flow-output.probability", new Option_Bool(false));
oc.addDescription("flow-output.probability", "Output", "Writes probabilistic flow instead of evenly spaced flow");
oc.doRegister("pedestrians", new Option_Bool(false));
oc.addDescription("pedestrians", "Output", "Writes pedestrians instead of vehicles");
oc.doRegister("persontrips", new Option_Bool(false));
oc.addDescription("persontrips", "Output", "Writes persontrips instead of vehicles");
oc.doRegister("persontrips.modes", new Option_StringVector());
oc.addDescription("persontrips.modes", "Output", "Add modes attribute to personTrips");
oc.doRegister("ignore-vehicle-type", new Option_Bool(false));
oc.addSynonyme("ignore-vehicle-type", "no-vtype", true);
oc.addDescription("ignore-vehicle-type", "Output", "Does not save vtype information");
// register the time settings
oc.doRegister("begin", 'b', new Option_String("0", "TIME"));
oc.addDescription("begin", "Time", "Defines the begin time; Previous trips will be discarded");
oc.doRegister("end", 'e', new Option_String(SUMOTIME_MAXSTRING, "TIME"));
oc.addDescription("end", "Time", "Defines the end time; Later trips will be discarded; Defaults to the maximum time that SUMO can represent");
// register the data processing options
oc.doRegister("scale", 's', new Option_Float(1));
oc.addDescription("scale", "Processing", "Scales the loaded flows by FLOAT");
oc.doRegister("spread.uniform", new Option_Bool(false));
oc.addDescription("spread.uniform", "Processing", "Spreads trips uniformly over each time period");
oc.doRegister("different-source-sink", new Option_Bool(false));
oc.addDescription("different-source-sink", "Processing", "Always choose source and sink edge which are not identical");
oc.doRegister("vtype", new Option_String(""));
oc.addDescription("vtype", "Processing", "Defines the name of the vehicle type to use");
oc.doRegister("prefix", new Option_String(""));
oc.addDescription("prefix", "Processing", "Defines the prefix for vehicle names");
oc.doRegister("timeline", new Option_StringVector());
oc.addDescription("timeline", "Processing", "Uses STR[] as a timeline definition");
oc.doRegister("timeline.day-in-hours", new Option_Bool(false));
oc.addDescription("timeline.day-in-hours", "Processing", "Uses STR as a 24h-timeline definition");
oc.doRegister("ignore-errors", new Option_Bool(false)); // !!! describe, document
oc.addSynonyme("ignore-errors", "dismiss-loading-errors", true);
oc.addDescription("ignore-errors", "Report", "Continue on broken input");
oc.doRegister("no-step-log", new Option_Bool(false));
oc.addDescription("no-step-log", "Processing", "Disable console output of current time step");
// register defaults options
oc.doRegister("departlane", new Option_String("free"));
oc.addDescription("departlane", "Defaults", "Assigns a default depart lane");
oc.doRegister("departpos", new Option_String());
oc.addDescription("departpos", "Defaults", "Assigns a default depart position");
oc.doRegister("departspeed", new Option_String("max"));
oc.addDescription("departspeed", "Defaults", "Assigns a default depart speed");
oc.doRegister("arrivallane", new Option_String());
oc.addDescription("arrivallane", "Defaults", "Assigns a default arrival lane");
oc.doRegister("arrivalpos", new Option_String());
oc.addDescription("arrivalpos", "Defaults", "Assigns a default arrival position");
oc.doRegister("arrivalspeed", new Option_String());
oc.addDescription("arrivalspeed", "Defaults", "Assigns a default arrival speed");
// add rand options
RandHelper::insertRandOptions();
}
bool
checkOptions() {
OptionsCont& oc = OptionsCont::getOptions();
bool ok = true;
if (!oc.isSet("taz-files")) {
WRITE_ERROR("No TAZ input file (-n) specified.");
ok = false;
}
if (!oc.isSet("od-matrix-files") && !oc.isSet("od-amitran-files")) {
WRITE_ERROR("No input specified.");
ok = false;
}
if (!oc.isSet("output-file") && !oc.isSet("flow-output")) {
WRITE_ERROR("No trip table output file (-o) or flow-output is specified.");
ok = false;
}
if (oc.getBool("pedestrians") && oc.getBool("persontrips")) {
WRITE_ERROR("Only one of the the options 'pedestrians' and 'persontrips' may be set.");
ok = false;
}
//
SUMOVehicleParameter p;
std::string error;
if (oc.isSet("departlane") && !SUMOVehicleParameter::parseDepartLane(oc.getString("departlane"), "option", "departlane", p.departLane, p.departLaneProcedure, error)) {
WRITE_ERROR(error);
ok = false;
}
if (oc.isSet("departpos") && !SUMOVehicleParameter::parseDepartPos(oc.getString("departpos"), "option", "departpos", p.departPos, p.departPosProcedure, error)) {
WRITE_ERROR(error);
ok = false;
}
if (oc.isSet("departspeed") && !SUMOVehicleParameter::parseDepartSpeed(oc.getString("departspeed"), "option", "departspeed", p.departSpeed, p.departSpeedProcedure, error)) {
WRITE_ERROR(error);
ok = false;
}
if (oc.isSet("arrivallane") && !SUMOVehicleParameter::parseArrivalLane(oc.getString("arrivallane"), "option", "arrivallane", p.arrivalLane, p.arrivalLaneProcedure, error)) {
WRITE_ERROR(error);
ok = false;
}
if (oc.isSet("arrivalpos") && !SUMOVehicleParameter::parseArrivalPos(oc.getString("arrivalpos"), "option", "arrivalpos", p.arrivalPos, p.arrivalPosProcedure, error)) {
WRITE_ERROR(error);
ok = false;
}
if (oc.isSet("arrivalspeed") && !SUMOVehicleParameter::parseArrivalSpeed(oc.getString("arrivalspeed"), "option", "arrivalspeed", p.arrivalSpeed, p.arrivalSpeedProcedure, error)) {
WRITE_ERROR(error);
ok = false;
}
ok &= SystemFrame::checkOptions();
return ok;
}
/* -------------------------------------------------------------------------
* main
* ----------------------------------------------------------------------- */
int
main(int argc, char** argv) {
OptionsCont& oc = OptionsCont::getOptions();
// give some application descriptions
oc.setApplicationDescription("Importer of O/D-matrices for the microscopic, multi-modal traffic simulation SUMO.");
oc.setApplicationName("od2trips", "Eclipse SUMO od2trips Version " VERSION_STRING);
int ret = 0;
try {
// initialise subsystems
XMLSubSys::init();
fillOptions();
OptionsIO::setArgs(argc, argv);
OptionsIO::getOptions();
if (oc.processMetaOptions(argc < 2)) {
SystemFrame::close();
return 0;
}
XMLSubSys::setValidation(oc.getString("xml-validation"), "never", "never");
MsgHandler::initOutputOptions();
if (!checkOptions()) {
throw ProcessError();
}
RandHelper::initRandGlobal();
// load the districts
// check whether the user gave a net filename
if (!oc.isSet("taz-files")) {
throw ProcessError("You must supply a TAZ, network or districts file ('-n').");
}
// get the file name and set it
ODDistrictCont districts;
districts.loadDistricts(oc.getStringVector("taz-files"));
if (districts.size() == 0) {
throw ProcessError("No districts loaded.");
}
// load the matrix
ODMatrix matrix(districts);
matrix.loadMatrix(oc);
if (matrix.getNumLoaded() == 0) {
throw ProcessError("No vehicles loaded.");
}
if (MsgHandler::getErrorInstance()->wasInformed() && !oc.getBool("ignore-errors")) {
throw ProcessError("Loading failed.");
}
WRITE_MESSAGE(toString(matrix.getNumLoaded()) + " vehicles loaded.");
// apply a curve if wished
if (oc.isSet("timeline")) {
matrix.applyCurve(matrix.parseTimeLine(oc.getStringVector("timeline"), oc.getBool("timeline.day-in-hours")));
}
const std::string modes = toString(oc.getStringVector("persontrips.modes"));
// write
bool haveOutput = false;
if (OutputDevice::createDeviceByOption("output-file", "routes", "routes_file.xsd")) {
matrix.write(string2time(oc.getString("begin")), string2time(oc.getString("end")),
OutputDevice::getDeviceByOption("output-file"),
oc.getBool("spread.uniform"), oc.getBool("different-source-sink"),
oc.getBool("ignore-vehicle-type"),
oc.getString("prefix"), !oc.getBool("no-step-log"),
oc.getBool("pedestrians"),
oc.getBool("persontrips"), modes);
haveOutput = true;
}
if (OutputDevice::createDeviceByOption("flow-output", "routes", "routes_file.xsd")) {
matrix.writeFlows(string2time(oc.getString("begin")), string2time(oc.getString("end")),
OutputDevice::getDeviceByOption("flow-output"),
oc.getBool("ignore-vehicle-type"), oc.getString("prefix"),
oc.getBool("flow-output.probability"), oc.getBool("pedestrians"),
oc.getBool("persontrips"), modes);
haveOutput = true;
}
if (!haveOutput) {
throw ProcessError("No output file given.");
}
WRITE_MESSAGE(toString(matrix.getNumDiscarded()) + " vehicles discarded.");
WRITE_MESSAGE(toString(matrix.getNumWritten()) + " vehicles written.");
} catch (const ProcessError& e) {
if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
WRITE_ERROR(e.what());
}
MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
ret = 1;
#ifndef _DEBUG
} catch (const std::exception& e) {
if (std::string(e.what()) != std::string("")) {
WRITE_ERROR(e.what());
}
MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
ret = 1;
} catch (...) {
MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
ret = 1;
#endif
}
SystemFrame::close();
if (ret == 0) {
std::cout << "Success." << std::endl;
}
return ret;
}
/****************************************************************************/