NIImporter_DlrNavteq.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Importer for networks stored in Elmar's format
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
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 // method definitions
00058 // ===========================================================================
00059 // ---------------------------------------------------------------------------
00060 // static methods
00061 // ---------------------------------------------------------------------------
00062 void
00063 NIImporter_DlrNavteq::loadNetwork(const OptionsCont &oc, NBNetBuilder &nb) {
00064     // check whether the option is set (properly)
00065     if (!oc.isSet("dlr-navteq")) {
00066         return;
00067     }
00068     // parse file(s)
00069     LineReader lr;
00070     // load nodes
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     // load traffic lights
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     // load edges
00091     MsgHandler::getMessageInstance()->beginProcessMsg("Loading edges...");
00092     file = oc.getString("dlr-navteq") + "_links_unsplitted.txt";
00093     // parse the file
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 // definitions of NIImporter_DlrNavteq::NodesHandler-methods
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     // parse
00125     std::istringstream stream(result);
00126     // id
00127     stream >> id;
00128     if (stream.fail()) {
00129         throw ProcessError("Something is wrong with the following data line\n" + result);
00130     }
00131     // intermediate?
00132     stream >> intermediate;
00133     if (stream.fail()) {
00134         throw ProcessError("Non-numerical value for intermediate status in node " + id + ".");
00135     }
00136     // number of geometrical information
00137     stream >> no_geoms;
00138     if (stream.fail()) {
00139         throw ProcessError("Non-numerical value for number of geometries in node " + id + ".");
00140     }
00141     // geometrical information
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 // definitions of NIImporter_DlrNavteq::EdgesHandler-methods
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 //  0: LINK_ID  NODE_ID_FROM    NODE_ID_TO  BETWEEN_NODE_ID
00188 //  4: length   vehicle_type    form_of_way brunnel_type
00189 //  7: street_type  speed_category  number_of_lanes average_speed
00190 //  10: NAME_ID1    NAME_ID2    housenumbers_right  housenumbers_left
00191 //  ZIP_CODE    AREA_ID SUBAREA_ID  through_traffic special_restrictions
00192 //  extended_number_of_lanes  isRamp    (these two only exist in networks extracted since 05/2009)
00193 //  connection (this may be omitted)
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     // parse
00204     StringTokenizer st(result, StringTokenizer::WHITECHARS);
00205     // id
00206     id = st.next();
00207     // from node id
00208     fromID = st.next();
00209     // to node id
00210     toID = st.next();
00211     // intermediate node id
00212     interID = st.next();
00213     // length
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     // vehicle_type
00220     std::string veh_type = st.next();
00221     // form_of_way
00222     std::string form_of_way = st.next();
00223     // brunnel_type
00224     std::string brunnel_type = st.next();
00225     // street_type
00226     std::string street_type = st.next();
00227     speed = NINavTeqHelper::getSpeed(id, st.next());
00228     // number of lanes
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         // post 05/2009 network
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     // try to get the nodes
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     // build the edge
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     // add vehicle type information to the edge
00270     NINavTeqHelper::addVehicleClasses(*e, veh_type);
00271     // insert the edge to the network
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 // definitions of NIImporter_DlrNavteq::TrafficlightsHandler-methods
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 // #ID     POICOL-TYPE     DESCRIPTION     LONGITUDE       LATITUDE        NAVTEQ_LINK_ID  NODEID
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                 // actually, nothing should fail here
00310                 delete tlDef;
00311                 throw ProcessError("Could not allocate tls for '" + nodeID + "'.");
00312             }
00313         }
00314     }
00315     return true;
00316 }
00317 
00318 /****************************************************************************/

Generated on Wed May 5 00:06:34 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6