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 #include <string>
00031 #include <sstream>
00032 #include <utils/importio/LineHandler.h>
00033 #include <utils/common/StringTokenizer.h>
00034 #include <utils/common/MsgHandler.h>
00035 #include <utils/common/UtilExceptions.h>
00036 #include <utils/common/TplConvert.h>
00037 #include <utils/options/OptionsCont.h>
00038 #include <utils/importio/LineReader.h>
00039 #include <utils/geom/GeoConvHelper.h>
00040 #include <netbuild/NBNetBuilder.h>
00041 #include <netbuild/NBNode.h>
00042 #include <netbuild/NBNodeCont.h>
00043 #include <netbuild/NBEdge.h>
00044 #include <netbuild/NBEdgeCont.h>
00045 #include <netbuild/NBTypeCont.h>
00046 #include <netbuild/NBOwnTLDef.h>
00047 #include <netimport/NINavTeqHelper.h>
00048 #include "NIImporter_DlrNavteq.h"
00049
00050
00051 #ifdef CHECK_MEMORY_LEAKS
00052 #include <foreign/nvwa/debug_new.h>
00053 #endif // CHECK_MEMORY_LEAKS
00054
00055
00056
00057
00058
00059
00060
00061
00062 void
00063 NIImporter_DlrNavteq::loadNetwork(const OptionsCont &oc, NBNetBuilder &nb) {
00064
00065 if (!oc.isSet("dlr-navteq")) {
00066 return;
00067 }
00068
00069 LineReader lr;
00070
00071 std::map<std::string, Position2DVector> myGeoms;
00072 MsgHandler::getMessageInstance()->beginProcessMsg("Loading nodes...");
00073 std::string file = oc.getString("dlr-navteq") + "_nodes_unsplitted.txt";
00074 NodesHandler handler1(nb.getNodeCont(), file, myGeoms);
00075 if (!lr.setFile(file)) {
00076 throw ProcessError("The file '" + file + "' could not be opened.");
00077 }
00078 lr.readAll(handler1);
00079 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00080
00081
00082 file = oc.getString("dlr-navteq") + "_traffic_signals.txt";
00083 if (lr.setFile(file)) {
00084 MsgHandler::getMessageInstance()->beginProcessMsg("Loading traffic lights...");
00085 TrafficlightsHandler handler3(nb.getNodeCont(), nb.getTLLogicCont(), file);
00086 lr.readAll(handler3);
00087 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00088 }
00089
00090
00091 MsgHandler::getMessageInstance()->beginProcessMsg("Loading edges...");
00092 file = oc.getString("dlr-navteq") + "_links_unsplitted.txt";
00093
00094 EdgesHandler handler2(nb.getNodeCont(), nb.getEdgeCont(), file, myGeoms);
00095 if (!lr.setFile(file)) {
00096 throw ProcessError("The file '" + file + "' could not be opened.");
00097 }
00098 lr.readAll(handler2);
00099 nb.getEdgeCont().recheckLaneSpread();
00100 MsgHandler::getMessageInstance()->endProcessMsg("done.");
00101 }
00102
00103
00104
00105
00106
00107 NIImporter_DlrNavteq::NodesHandler::NodesHandler(NBNodeCont &nc,
00108 const std::string &file,
00109 std::map<std::string, Position2DVector> &geoms) throw()
00110 : myNodeCont(nc), myGeoms(geoms) {}
00111
00112
00113 NIImporter_DlrNavteq::NodesHandler::~NodesHandler() throw() {}
00114
00115
00116 bool
00117 NIImporter_DlrNavteq::NodesHandler::report(const std::string &result) throw(ProcessError) {
00118 if (result[0]=='#') {
00119 return true;
00120 }
00121 std::string id;
00122 double x, y;
00123 int no_geoms, intermediate;
00124
00125 std::istringstream stream(result);
00126
00127 stream >> id;
00128 if (stream.fail()) {
00129 throw ProcessError("Something is wrong with the following data line\n" + result);
00130 }
00131
00132 stream >> intermediate;
00133 if (stream.fail()) {
00134 throw ProcessError("Non-numerical value for intermediate status in node " + id + ".");
00135 }
00136
00137 stream >> no_geoms;
00138 if (stream.fail()) {
00139 throw ProcessError("Non-numerical value for number of geometries in node " + id + ".");
00140 }
00141
00142 Position2DVector geoms;
00143 for (int i=0; i<no_geoms; i++) {
00144 stream >> x;
00145 if (stream.fail()) {
00146 throw ProcessError("Non-numerical value for x-position in node " + id + ".");
00147 }
00148 stream >> y;
00149 if (stream.fail()) {
00150 throw ProcessError("Non-numerical value for y-position in node " + id + ".");
00151 }
00152 Position2D pos(x, y);
00153 if (!GeoConvHelper::x2cartesian(pos, true, x, y)) {
00154 throw ProcessError("Unable to project coordinates for node " + id + ".");
00155 }
00156 geoms.push_back(pos);
00157 }
00158
00159 if (intermediate==0) {
00160 NBNode *n = new NBNode(id, geoms[0]);
00161 if (!myNodeCont.insert(n)) {
00162 delete n;
00163 throw ProcessError("Could not add node '" + id + "'.");
00164 }
00165 } else {
00166 myGeoms[id] = geoms;
00167 }
00168 return true;
00169 }
00170
00171
00172
00173
00174
00175 NIImporter_DlrNavteq::EdgesHandler::EdgesHandler(NBNodeCont &nc, NBEdgeCont &ec,
00176 const std::string &file,
00177 std::map<std::string,
00178 Position2DVector> &geoms) throw()
00179 : myNodeCont(nc), myEdgeCont(ec), myGeoms(geoms) {}
00180
00181
00182 NIImporter_DlrNavteq::EdgesHandler::~EdgesHandler() throw() {}
00183
00184
00185 bool
00186 NIImporter_DlrNavteq::EdgesHandler::report(const std::string &result) throw(ProcessError) {
00187
00188
00189
00190
00191
00192
00193
00194
00195 if (result[0]=='#') {
00196 return true;
00197 }
00198 std::string id, fromID, toID, interID;
00199 SUMOReal length;
00200 SUMOReal speed = (SUMOReal) 30.0 / (SUMOReal) 3.6;
00201 int nolanes = 1;
00202 int priority = -1;
00203
00204 StringTokenizer st(result, StringTokenizer::WHITECHARS);
00205
00206 id = st.next();
00207
00208 fromID = st.next();
00209
00210 toID = st.next();
00211
00212 interID = st.next();
00213
00214 try {
00215 length = TplConvert<char>::_2SUMOReal(st.next().c_str());
00216 } catch (NumberFormatException &) {
00217 throw ProcessError("Non-numerical value for an edge's length occured (edge '" + id + "'.");
00218 }
00219
00220 std::string veh_type = st.next();
00221
00222 std::string form_of_way = st.next();
00223
00224 std::string brunnel_type = st.next();
00225
00226 std::string street_type = st.next();
00227 speed = NINavTeqHelper::getSpeed(id, st.next());
00228
00229 nolanes = NINavTeqHelper::getLaneNumber(id, st.next(), speed);
00230 std::vector<std::string> theRest = st.getVector();
00231 bool connection = (theRest.size() == 11) && (theRest[10] == "1");
00232 if (theRest.size() > 11) {
00233
00234 if (theRest[11] != "-1") {
00235 try {
00236 nolanes = TplConvert<char>::_2int(theRest[11].c_str());
00237 } catch (NumberFormatException &) {
00238 throw ProcessError("Non-numerical value for the extended number of lanes (edge '" + id + "'.");
00239 }
00240 }
00241 connection = (theRest.size() == 13) && (theRest[12] == "1");
00242 }
00243
00244 NBNode *from = myNodeCont.retrieve(fromID);
00245 NBNode *to = myNodeCont.retrieve(toID);
00246 if (from==0) {
00247 throw ProcessError("The from-node '" + fromID + "' of edge '" + id + "' could not be found");
00248 }
00249 if (to==0) {
00250 throw ProcessError("The to-node '" + toID + "' of edge '" + id + "' could not be found");
00251 }
00252
00253 NBEdge *e = 0;
00254 if (interID=="-1") {
00255 e = new NBEdge(id, from, to, "DEFAULT", speed, nolanes, priority);
00256 } else {
00257 Position2DVector geoms = myGeoms[interID];
00258 if (connection) {
00259 geoms = geoms.reverse();
00260 geoms.push_front(from->getPosition());
00261 geoms.push_back(to->getPosition());
00262 e = new NBEdge(id, from, to, "DEFAULT", speed, nolanes, priority, geoms, NBEdge::LANESPREAD_CENTER);
00263 } else {
00264 geoms.push_front(from->getPosition());
00265 geoms.push_back(to->getPosition());
00266 e = new NBEdge(id, from, to, "DEFAULT", speed, nolanes, priority, geoms, NBEdge::LANESPREAD_CENTER);
00267 }
00268 }
00269
00270 NINavTeqHelper::addVehicleClasses(*e, veh_type);
00271
00272 if (!myEdgeCont.insert(e)) {
00273 delete e;
00274 throw ProcessError("Could not add edge '" + id + "'.");
00275 }
00276 return true;
00277 }
00278
00279
00280
00281
00282
00283 NIImporter_DlrNavteq::TrafficlightsHandler::TrafficlightsHandler(NBNodeCont &nc,
00284 NBTrafficLightLogicCont &tlc,
00285 const std::string &file) throw()
00286 : myNodeCont(nc), myTLLogicCont(tlc) {}
00287
00288
00289 NIImporter_DlrNavteq::TrafficlightsHandler::~TrafficlightsHandler() throw() {}
00290
00291
00292 bool
00293 NIImporter_DlrNavteq::TrafficlightsHandler::report(const std::string &result) throw(ProcessError) {
00294
00295
00296 if (result[0]=='#') {
00297 return true;
00298 }
00299 StringTokenizer st(result, StringTokenizer::WHITECHARS);
00300 std::string nodeID = st.getVector().back();
00301 NBNode *node = myNodeCont.retrieve(nodeID);
00302 if (node==0) {
00303 WRITE_WARNING("The traffic light node '" + nodeID + "' could not be found");
00304 } else {
00305 if (node->getType() != NBNode::NODETYPE_TRAFFIC_LIGHT) {
00306 node->reinit(node->getPosition(), NBNode::NODETYPE_TRAFFIC_LIGHT);
00307 NBTrafficLightDefinition *tlDef = new NBOwnTLDef(nodeID, node);
00308 if (!myTLLogicCont.insert(tlDef)) {
00309
00310 delete tlDef;
00311 throw ProcessError("Could not allocate tls for '" + nodeID + "'.");
00312 }
00313 }
00314 }
00315 return true;
00316 }
00317
00318