NIXMLNodesHandler.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Importer for network nodes stored in XML
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 <iostream>
00032 #include <xercesc/sax/HandlerBase.hpp>
00033 #include <xercesc/sax/AttributeList.hpp>
00034 #include <xercesc/sax/SAXParseException.hpp>
00035 #include <xercesc/sax/SAXException.hpp>
00036 #include "NIXMLNodesHandler.h"
00037 #include <utils/xml/SUMOSAXHandler.h>
00038 #include <netbuild/NBNodeCont.h>
00039 #include <utils/xml/SUMOXMLDefinitions.h>
00040 #include <utils/common/MsgHandler.h>
00041 #include <utils/common/TplConvert.h>
00042 #include <utils/common/ToString.h>
00043 #include <utils/common/StringTokenizer.h>
00044 #include <utils/options/OptionsCont.h>
00045 #include <netbuild/NBTrafficLightLogicCont.h>
00046 #include <netbuild/NBOwnTLDef.h>
00047 #include <utils/geom/GeoConvHelper.h>
00048 
00049 #ifdef CHECK_MEMORY_LEAKS
00050 #include <foreign/nvwa/debug_new.h>
00051 #endif // CHECK_MEMORY_LEAKS
00052 
00053 
00054 // ===========================================================================
00055 // method definitions
00056 // ===========================================================================
00057 NIXMLNodesHandler::NIXMLNodesHandler(NBNodeCont &nc,
00058                                      NBTrafficLightLogicCont &tlc,
00059                                      OptionsCont &options)
00060         : SUMOSAXHandler("xml-nodes - file"),
00061         myOptions(options),
00062         myNodeCont(nc), myTLLogicCont(tlc) {}
00063 
00064 
00065 NIXMLNodesHandler::~NIXMLNodesHandler() throw() {}
00066 
00067 
00068 void
00069 NIXMLNodesHandler::myStartElement(SumoXMLTag element,
00070                                   const SUMOSAXAttributes &attrs) throw(ProcessError) {
00071     if (element!=SUMO_TAG_NODE) {
00072         return;
00073     }
00074     // get the id, report a warning if not given or empty...
00075     if (!attrs.setIDFromAttributes("node", myID), false) {
00076         WRITE_WARNING("No node id given... Skipping.");
00077         return;
00078     }
00079     NBNode *node = myNodeCont.retrieve(myID);
00080     // retrieve the position of the node
00081     bool ok = true;
00082     bool xOk = false;
00083     bool yOk = false;
00084     if (node!=0) {
00085         myPosition = node->getPosition();
00086         xOk = yOk = true;
00087     }
00088     if (attrs.hasAttribute(SUMO_ATTR_X)) {
00089         myPosition.set(attrs.getSUMORealReporting(SUMO_ATTR_X, "node", myID.c_str(), ok), myPosition.y());
00090         xOk = true;
00091     }
00092     if (attrs.hasAttribute(SUMO_ATTR_Y)) {
00093         myPosition.set(myPosition.x(), attrs.getSUMORealReporting(SUMO_ATTR_Y, "node", myID.c_str(), ok));
00094         yOk = true;
00095     }
00096     if (xOk&&yOk) {
00097         if (!GeoConvHelper::x2cartesian(myPosition)) {
00098             MsgHandler::getErrorInstance()->inform("Unable to project coordinates for node '" + myID + "'.");
00099         }
00100     } else {
00101         MsgHandler::getErrorInstance()->inform("Missing position (at node ID='" + myID + "').");
00102     }
00103     // check whether the y-axis shall be flipped
00104     if (myOptions.getBool("flip-y")) {
00105         myPosition.mul(1.0, -1.0);
00106     }
00107     // get the type
00108     NBNode::BasicNodeType type = NBNode::NODETYPE_UNKNOWN;
00109     if (node!=0) {
00110         type = node->getType();
00111     }
00112     if (attrs.hasAttribute(SUMO_ATTR_TYPE)) {
00113         std::string typeS = attrs.getOptStringReporting(SUMO_ATTR_TYPE, "node", myID.c_str(), ok, "");
00114         if (typeS=="priority") {
00115             type = NBNode::NODETYPE_PRIORITY_JUNCTION;
00116         } else if (typeS=="right_before_left") {
00117             type = NBNode::NODETYPE_RIGHT_BEFORE_LEFT;
00118         } else if (typeS=="traffic_light") {
00119             type = NBNode::NODETYPE_TRAFFIC_LIGHT;
00120         }
00121     }
00122     // check whether a prior node shall be modified
00123     if (node==0) {
00124         node = new NBNode(myID, myPosition, type);
00125         if (!myNodeCont.insert(node)) {
00126             throw ProcessError("Could not insert node though checked this before (id='" + myID + "').");
00127         }
00128     } else {
00129         // remove previously set tls if this node is not controlled by a tls
00130         std::set<NBTrafficLightDefinition*> tls = node->getControllingTLS();
00131         node->removeTrafficLights();
00132         for (std::set<NBTrafficLightDefinition*>::iterator i=tls.begin(); i!=tls.end(); ++i) {
00133             if ((*i)->getNodes().size()==0) {
00134                 myTLLogicCont.remove((*i)->getID());
00135             }
00136         }
00137         // patch information
00138         node->reinit(myPosition, type);
00139     }
00140     // process traffic light definition
00141     if (type==NBNode::NODETYPE_TRAFFIC_LIGHT) {
00142         processTrafficLightDefinitions(attrs, node);
00143     }
00144 }
00145 
00146 
00147 void
00148 NIXMLNodesHandler::processTrafficLightDefinitions(const SUMOSAXAttributes &attrs,
00149         NBNode *currentNode) {
00150     // try to get the tl-id
00151     // if a tl-id is given, we will look whether this tl already exists
00152     //  if so, we will add the node to it, otherwise allocate a new one with this id
00153     // if no tl-id exists, we will build a tl with the node's id
00154     NBTrafficLightDefinition *tlDef = 0;
00155     bool ok = true;
00156     std::string tlID = attrs.getOptStringReporting(SUMO_ATTR_TLID, "node", 0, ok, "");
00157     if (tlID!="") {
00158         // ok, the traffic light has a name
00159         tlDef = myTLLogicCont.getDefinition(tlID);
00160         if (tlDef==0) {
00161             // this traffic light is visited the first time
00162             tlDef = new NBOwnTLDef(tlID, currentNode);
00163             if (!myTLLogicCont.insert(tlDef)) {
00164                 // actually, nothing should fail here
00165                 delete tlDef;
00166                 throw ProcessError("Could not allocate tls '" + tlID + "'.");
00167             }
00168         } else {
00169             tlDef->addNode(currentNode);
00170         }
00171     } else {
00172         // ok, this node is a traffic light node where no other nodes
00173         //  participate
00174         tlDef = new NBOwnTLDef(myID, currentNode);
00175         if (!myTLLogicCont.insert(tlDef)) {
00176             // actually, nothing should fail here
00177             delete tlDef;
00178             throw ProcessError("Could not allocate tls '" + myID + "'.");
00179         }
00180     }
00181     // process inner edges which shall be controlled
00182     std::vector<std::string> controlledInner;
00183     SUMOSAXAttributes::parseStringVector(attrs.getOptStringReporting(SUMO_ATTR_CONTROLLED_INNER, "node", 0, ok, ""), controlledInner);
00184     if (controlledInner.size()!=0) {
00185         tlDef->addControlledInnerEdges(controlledInner);
00186     }
00187 }
00188 
00189 
00190 
00191 /****************************************************************************/
00192 

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