00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029
00030 #ifdef HAVE_VERSION_H
00031 #include <version.h>
00032 #endif
00033
00034 #include <xercesc/sax/SAXException.hpp>
00035 #include <xercesc/sax/SAXParseException.hpp>
00036 #include <utils/common/TplConvert.h>
00037 #include <iostream>
00038 #include <string>
00039 #include <limits.h>
00040 #include <ctime>
00041 #include <router/ROLoader.h>
00042 #include <router/RONet.h>
00043 #include "RODFEdgeBuilder.h"
00044 #include <router/ROFrame.h>
00045 #include <utils/common/MsgHandler.h>
00046 #include <utils/options/Option.h>
00047 #include <utils/options/OptionsCont.h>
00048 #include <utils/options/OptionsIO.h>
00049 #include <utils/common/UtilExceptions.h>
00050 #include <utils/common/SystemFrame.h>
00051 #include <utils/common/ToString.h>
00052 #include <utils/xml/XMLSubSys.h>
00053 #include "RODFFrame.h"
00054 #include "RODFNet.h"
00055 #include "RODFEdge.h"
00056 #include "RODFDetector.h"
00057 #include "RODFDetectorHandler.h"
00058 #include "RODFRouteCont.h"
00059 #include "RODFDetectorFlow.h"
00060 #include "RODFDetFlowLoader.h"
00061 #include <utils/xml/XMLSubSys.h>
00062 #include <utils/common/FileHelpers.h>
00063 #include <utils/iodevices/OutputDevice.h>
00064
00065 #ifdef CHECK_MEMORY_LEAKS
00066 #include <foreign/nvwa/debug_new.h>
00067 #endif // CHECK_MEMORY_LEAKS
00068
00069
00070
00071
00072
00073
00074
00075
00076 void
00077 readDetectors(RODFDetectorCon &detectors, OptionsCont &oc, RODFNet *optNet) {
00078 if (!oc.isSet("detector-files")) {
00079 throw ProcessError("No detector file given (use --detector-files <FILE>).");
00080 }
00081
00082 std::vector<std::string> files = oc.getStringVector("detector-files");
00083 for (std::vector<std::string>::const_iterator fileIt=files.begin(); fileIt!=files.end(); ++fileIt) {
00084 if (!FileHelpers::exists(*fileIt)) {
00085 throw ProcessError("Could not open detector file '" + *fileIt + "'");
00086 }
00087 MsgHandler::getMessageInstance()->beginProcessMsg("Loading detector definitions from '" + *fileIt + "'... ");
00088 RODFDetectorHandler handler(optNet, oc.getBool("ignore-invalid-detectors"), detectors, *fileIt);
00089 if (XMLSubSys::runParser(handler, *fileIt)) {
00090 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00091 } else {
00092 MsgHandler::getMessageInstance()->endProcessMsg("failed.");
00093 throw ProcessError();
00094 }
00095 }
00096 }
00097
00098
00099 void
00100 readDetectorFlows(RODFDetectorFlows &flows, OptionsCont &oc, RODFDetectorCon &dc) {
00101 if (!oc.isSet("detector-flow-files")) {
00102
00103 return;
00104 }
00105
00106 std::vector<std::string> files = oc.getStringVector("detector-flow-files");
00107 for (std::vector<std::string>::const_iterator fileIt=files.begin(); fileIt!=files.end(); ++fileIt) {
00108 if (!FileHelpers::exists(*fileIt)) {
00109 throw ProcessError("The detector-flow-file '" + *fileIt + "' can not be opened.");
00110 }
00111
00112 MsgHandler::getMessageInstance()->beginProcessMsg("Loading flows from '" + *fileIt + "'...");
00113 RODFDetFlowLoader dfl(dc, flows, string2time(oc.getString("begin"))/1000., string2time(oc.getString("end"))/1000., string2time(oc.getString("time-offset"))/1000.);
00114 dfl.read(*fileIt);
00115 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00116 }
00117 }
00118
00119
00120 void
00121 startComputation(RODFNet *optNet, RODFDetectorFlows &flows, RODFDetectorCon &detectors, OptionsCont &oc) {
00122 if (oc.getBool("print-absolute-flows")) {
00123 flows.printAbsolute();
00124 }
00125
00126
00127 if (optNet!=0) {
00128 if (oc.getBool("remove-empty-detectors")) {
00129 MsgHandler::getMessageInstance()->beginProcessMsg("Removing empty detectors...");
00130 optNet->removeEmptyDetectors(detectors, flows, string2time(oc.getString("begin"))/1000., string2time(oc.getString("end"))/1000., 60);
00131 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00132 } else if (oc.getBool("report-empty-detectors")) {
00133 MsgHandler::getMessageInstance()->beginProcessMsg("Scanning for empty detectors...");
00134 optNet->reportEmptyDetectors(detectors, flows);
00135 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00136 }
00137
00138 if (!detectors.detectorsHaveCompleteTypes()||oc.getBool("revalidate-detectors")) {
00139 optNet->computeTypes(detectors, oc.getBool("strict-sources"));
00140 }
00141
00142 if (!detectors.detectorsHaveRoutes()||oc.getBool("revalidate-routes")||oc.getBool("guess-empty-flows")) {
00143 MsgHandler::getMessageInstance()->beginProcessMsg("Computing routes...");
00144 optNet->buildRoutes(detectors,
00145 oc.getBool("all-end-follower"), oc.getBool("keep-unfound-ends"),
00146 oc.getBool("routes-for-all"), !oc.getBool("keep-longer-routes"),
00147 oc.getInt("max-nodet-follower"));
00148 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00149 }
00150 }
00151
00152
00153
00154 if (!detectors.detectorsHaveCompleteTypes()) {
00155 throw ProcessError("The detector types are not defined; use in combination with a network");
00156 }
00157
00158 if (!detectors.detectorsHaveRoutes()) {
00159 throw ProcessError("The emitters have no routes; use in combination with a network");
00160 }
00161
00162
00163 if (oc.isSet("detectors-output")) {
00164 detectors.save(oc.getString("detectors-output"));
00165 }
00166
00167 if (oc.isSet("detectors-poi-output")) {
00168 detectors.saveAsPOIs(oc.getString("detectors-poi-output"));
00169 }
00170
00171
00172 if (detectors.detectorsHaveRoutes()&&oc.isSet("routes-output")) {
00173 detectors.saveRoutes(oc.getString("routes-output"));
00174 }
00175
00176
00177 if (oc.getBool("guess-empty-flows")) {
00178 optNet->buildDetectorDependencies(detectors);
00179 detectors.guessEmptyFlows(flows);
00180 }
00181
00182
00183 if (oc.isSet("emitters-output")||oc.isSet("emitters-poi-output")) {
00184 optNet->buildEdgeFlowMap(flows, detectors, string2time(oc.getString("begin"))/1000., string2time(oc.getString("end"))/1000., 60);
00185 if (oc.getBool("revalidate-flows")) {
00186 MsgHandler::getMessageInstance()->beginProcessMsg("Rechecking loaded flows...");
00187 optNet->revalidateFlows(detectors, flows, string2time(oc.getString("begin"))/1000., string2time(oc.getString("end"))/1000., 60);
00188 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00189 }
00190 if (oc.isSet("emitters-output")) {
00191 MsgHandler::getMessageInstance()->beginProcessMsg("Writing emitters...");
00192 detectors.writeEmitters(oc.getString("emitters-output"), flows,
00193 string2time(oc.getString("begin"))/1000., string2time(oc.getString("end"))/1000., 60,
00194 *optNet,
00195 oc.getBool("write-calibrators"),
00196 oc.getBool("include-unused-routes"),
00197 oc.getFloat("scale"),
00198 oc.getInt("max-nodet-follower"),
00199 oc.getBool("emissions-only"));
00200 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00201 }
00202 if (oc.isSet("emitters-poi-output")) {
00203 MsgHandler::getMessageInstance()->beginProcessMsg("Writing emitter pois...");
00204 detectors.writeEmitterPOIs(oc.getString("emitters-poi-output"), flows,
00205 string2time(oc.getString("begin"))/1000., string2time(oc.getString("end"))/1000., 60);
00206 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00207 }
00208 }
00209
00210 if (oc.isSet("speed-trigger-output")) {
00211 MsgHandler::getMessageInstance()->beginProcessMsg("Writing speed triggers...");
00212 detectors.writeSpeedTrigger(optNet, oc.getString("speed-trigger-output"), flows,
00213 string2time(oc.getString("begin"))/1000., string2time(oc.getString("end"))/1000., 60);
00214 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00215 }
00216
00217 if (oc.isSet("validation-output")) {
00218 MsgHandler::getMessageInstance()->beginProcessMsg("Writing validation detectors...");
00219 detectors.writeValidationDetectors(oc.getString("validation-output"),
00220 oc.getBool("validation-output.add-sources"), true, true);
00221 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00222 }
00223
00224 if (oc.isSet("end-reroute-output")) {
00225 MsgHandler::getMessageInstance()->beginProcessMsg("Writing highway end rerouter...");
00226 detectors.writeEndRerouterDetectors(oc.getString("end-reroute-output"));
00227 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00228 }
00229
00230
00231
00232
00233
00234
00235
00236 }
00237
00238
00239
00240
00241
00242 int
00243 main(int argc, char **argv) {
00244 OptionsCont &oc = OptionsCont::getOptions();
00245
00246 oc.setApplicationDescription("Builds vehicle routes for SUMO using detector values.");
00247 oc.setApplicationName("dfrouter", "SUMO dfrouter Version " + (std::string)VERSION_STRING);
00248 int ret = 0;
00249 RODFNet *net = 0;
00250 RODFDetectorCon *detectors = 0;
00251 RODFDetectorFlows *flows = 0;
00252 try {
00253
00254 XMLSubSys::init(false);
00255 RODFFrame::fillOptions();
00256 OptionsIO::getOptions(true, argc, argv);
00257 if (oc.processMetaOptions(argc < 2)) {
00258 SystemFrame::close();
00259 return 0;
00260 }
00261 MsgHandler::initOutputOptions();
00262 if (!RODFFrame::checkOptions()) throw ProcessError();
00263 RandHelper::initRandGlobal();
00264
00265 ROLoader loader(oc, false);
00266 net = new RODFNet(oc.getBool("highway-mode"));
00267 RODFEdgeBuilder builder;
00268 loader.loadNet(*net, builder);
00269 net->buildApproachList();
00270
00271 detectors = new RODFDetectorCon();
00272 readDetectors(*detectors, oc, net);
00273
00274 flows = new RODFDetectorFlows(string2time(oc.getString("begin"))/1000., string2time(oc.getString("end"))/1000., 60);
00275 readDetectorFlows(*flows, oc, *detectors);
00276
00277 startComputation(net, *flows, *detectors, oc);
00278 } catch (ProcessError &e) {
00279 if (std::string(e.what())!=std::string("Process Error") && std::string(e.what())!=std::string("")) {
00280 MsgHandler::getErrorInstance()->inform(e.what());
00281 }
00282 MsgHandler::getErrorInstance()->inform("Quitting (on error).", false);
00283 ret = 1;
00284 #ifndef _DEBUG
00285 } catch (...) {
00286 MsgHandler::getErrorInstance()->inform("Quitting (on unknown error).", false);
00287 ret = 1;
00288 #endif
00289 }
00290 delete net;
00291 delete flows;
00292 delete detectors;
00293 OutputDevice::closeAll();
00294 SystemFrame::close();
00295 if (ret==0) {
00296 std::cout << "Success." << std::endl;
00297 }
00298 return ret;
00299 }
00300
00301
00302
00303
00304