SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NIXMLNodesHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // Importer for network nodes stored in XML
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
13 // Copyright (C) 2001-2014 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <string>
35 #include <iostream>
36 #include <xercesc/sax/HandlerBase.hpp>
37 #include <xercesc/sax/AttributeList.hpp>
38 #include <xercesc/sax/SAXParseException.hpp>
39 #include <xercesc/sax/SAXException.hpp>
44 #include <utils/common/ToString.h>
48 #include <netbuild/NBNodeCont.h>
50 #include <netbuild/NBOwnTLDef.h>
51 #include <netbuild/NBNetBuilder.h>
52 #include "NIXMLNodesHandler.h"
53 #include "NIImporter_SUMO.h"
54 
55 #ifdef CHECK_MEMORY_LEAKS
56 #include <foreign/nvwa/debug_new.h>
57 #endif // CHECK_MEMORY_LEAKS
58 
59 
60 // ===========================================================================
61 // method definitions
62 // ===========================================================================
65  OptionsCont& options) :
66  SUMOSAXHandler("xml-nodes - file"),
67  myOptions(options),
68  myNodeCont(nc),
69  myTLLogicCont(tlc),
70  myLocation(0)
71 {}
72 
73 
75  delete myLocation;
76 }
77 
78 
79 void
81  const SUMOSAXAttributes& attrs) {
82  switch (element) {
83  case SUMO_TAG_LOCATION:
85  break;
86  case SUMO_TAG_NODE:
87  addNode(attrs);
88  break;
89  case SUMO_TAG_JOIN:
90  addJoinCluster(attrs);
91  break;
93  addJoinExclusion(attrs);
94  break;
95  case SUMO_TAG_DELETE:
96  deleteNode(attrs);
97  break;
98  default:
99  break;
100  }
101 }
102 
103 
104 void
106  bool ok = true;
107  // get the id, report a warning if not given or empty...
108  myID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
109  if (!ok) {
110  return;
111  }
112  NBNode* node = myNodeCont.retrieve(myID);
113  // retrieve the position of the node
114  bool xOk = false;
115  bool yOk = false;
116  bool needConversion = true;
117  if (node != 0) {
118  myPosition = node->getPosition();
119  xOk = yOk = true;
120  needConversion = false;
121  }
122  if (attrs.hasAttribute(SUMO_ATTR_X)) {
123  myPosition.set(attrs.get<SUMOReal>(SUMO_ATTR_X, myID.c_str(), ok), myPosition.y());
124  xOk = true;
125  needConversion = true;
126  }
127  if (attrs.hasAttribute(SUMO_ATTR_Y)) {
128  myPosition.set(myPosition.x(), attrs.get<SUMOReal>(SUMO_ATTR_Y, myID.c_str(), ok));
129  yOk = true;
130  needConversion = true;
131  }
132  if (attrs.hasAttribute(SUMO_ATTR_Z)) {
133  myPosition.set(myPosition.x(), myPosition.y(), attrs.get<SUMOReal>(SUMO_ATTR_Z, myID.c_str(), ok));
134  }
135  if (xOk && yOk) {
136  if (needConversion && !NBNetBuilder::transformCoordinates(myPosition, true, myLocation)) {
137  WRITE_ERROR("Unable to project coordinates for node '" + myID + "'.");
138  }
139  } else {
140  WRITE_ERROR("Missing position (at node ID='" + myID + "').");
141  }
142  bool updateEdgeGeometries = node != 0 && myPosition != node->getPosition();
143  // check whether the y-axis shall be flipped
144  if (myOptions.getBool("flip-y-axis")) {
145  myPosition.mul(1.0, -1.0);
146  }
147  // get the type
149  if (node != 0) {
150  type = node->getType();
151  }
152  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, myID.c_str(), ok, "");
153  if (SUMOXMLDefinitions::NodeTypes.hasString(typeS)) {
154  type = SUMOXMLDefinitions::NodeTypes.get(typeS);
155  if (type == NODETYPE_DEAD_END_DEPRECATED || type == NODETYPE_DEAD_END) {
156  // dead end is a computed status. Reset this to unknown so it will
157  // be corrected if additional connections are loaded
158  type = NODETYPE_UNKNOWN;
159  }
160  }
161  // check whether a prior node shall be modified
162  if (node == 0) {
163  node = new NBNode(myID, myPosition, type);
164  if (!myNodeCont.insert(node)) {
165  throw ProcessError("Could not insert node though checked this before (id='" + myID + "').");
166  }
167  } else {
168  // remove previously set tls if this node is not controlled by a tls
169  std::set<NBTrafficLightDefinition*> tls = node->getControllingTLS();
170  node->removeTrafficLights();
171  for (std::set<NBTrafficLightDefinition*>::iterator i = tls.begin(); i != tls.end(); ++i) {
172  if ((*i)->getNodes().size() == 0) {
173  myTLLogicCont.removeFully((*i)->getID());
174  }
175  }
176  // patch information
177  node->reinit(myPosition, type, updateEdgeGeometries);
178  }
179  // process traffic light definition
181  processTrafficLightDefinitions(attrs, node);
182  }
183 }
184 
185 
186 void
188  bool ok = true;
189  // get the id, report a warning if not given or empty...
190  myID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
191  if (!ok) {
192  return;
193  }
194  NBNode* node = myNodeCont.retrieve(myID);
195  if (node == 0) {
196  WRITE_WARNING("Ignoring tag '" + toString(SUMO_TAG_DELETE) + "' for unknown node '" +
197  myID + "'");
198  return;
199  } else {
200  myNodeCont.extract(node, true);
201  }
202 }
203 
204 
205 void
207  bool ok = true;
208  const std::string clusterString = attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok);
209  const std::vector<std::string> ids = StringTokenizer(clusterString).getVector();
210  if (ok) {
211  myNodeCont.addCluster2Join(std::set<std::string>(ids.begin(), ids.end()));
212  }
213 }
214 
215 
216 void
218  bool ok = true;
219  const std::vector<std::string> ids = StringTokenizer(
220  attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok)).getVector();
221  if (ok) {
223  }
224 }
225 
226 
227 void
229  NBNode* currentNode) {
230  // try to get the tl-id
231  // if a tl-id is given, we will look whether this tl already exists
232  // if so, we will add the node to it (and to all programs with this id), otherwise allocate a new one with this id
233  // if no tl-id exists, we will build a tl with the node's id
234  std::set<NBTrafficLightDefinition*> tlDefs;
235  bool ok = true;
236  std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, 0, ok, "");
237  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TLTYPE, 0, ok,
238  OptionsCont::getOptions().getString("tls.default-type"));
239  TrafficLightType type;
240  if (SUMOXMLDefinitions::TrafficLightTypes.hasString(typeS)) {
242  } else {
243  WRITE_ERROR("Unknown traffic light type '" + typeS + "' for node '" + myID + "'.");
244  return;
245  }
246  if (tlID != "" && myTLLogicCont.getPrograms(tlID).size() > 0) {
247  // we already have definitions for this tlID
248  const std::map<std::string, NBTrafficLightDefinition*>& programs = myTLLogicCont.getPrograms(tlID);
249  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
250  for (it = programs.begin(); it != programs.end(); it++) {
251  if (it->second->getType() != type) {
252  WRITE_ERROR("Mismatched traffic light type '" + typeS + "' for tl '" + tlID + "'.");
253  ok = false;
254  } else {
255  tlDefs.insert(it->second);
256  it->second->addNode(currentNode);
257  }
258  }
259  } else {
260  // we need to add a new defition
261  tlID = (tlID == "" ? myID : tlID);
262  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(tlID, currentNode, 0, type);
263  if (!myTLLogicCont.insert(tlDef)) {
264  // actually, nothing should fail here
265  delete tlDef;
266  throw ProcessError("Could not allocate tls '" + myID + "'.");
267  }
268  tlDefs.insert(tlDef);
269  }
270  // process inner edges which shall be controlled
271  std::vector<std::string> controlledInner;
272  SUMOSAXAttributes::parseStringVector(attrs.getOpt<std::string>(SUMO_ATTR_CONTROLLED_INNER, 0, ok, ""), controlledInner);
273  if (controlledInner.size() != 0) {
274  for (std::set<NBTrafficLightDefinition*>::iterator it = tlDefs.begin(); it != tlDefs.end(); it++) {
275  (*it)->addControlledInnerEdges(controlledInner);
276  }
277  }
278 }
279 
280 
281 
282 /****************************************************************************/
283 
static StringBijection< SumoXMLNodeType > NodeTypes
GeoConvHelper * myLocation
The coordinate transformation which was used compute the node coordinates.
a list of node ids, used for controlling joining
static bool transformCoordinates(Position &from, bool includeInBoundary=true, GeoConvHelper *from_srs=0)
transforms loaded coordinates handles projections, offsets (using GeoConvHelper) and import of height...
std::string myID
The id of the currently parsed node.
void addJoinExclusion(const std::vector< std::string > &ids, bool check=false)
Definition: NBNodeCont.cpp:435
A container for traffic light definitions and built programs.
~NIXMLNodesHandler()
Destructor.
void reinit(const Position &position, SumoXMLNodeType type, bool updateEdgeGeometries=false)
Resets initial values.
Definition: NBNode.cpp:216
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
Position myPosition
The position of the currently parsed node.
The base class for traffic light logic definitions.
NBTrafficLightLogicCont & myTLLogicCont
The traffic lights container to add built tls to.
SAX-handler base for SUMO-files.
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:196
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:67
NBNodeCont & myNodeCont
The node container to add built nodes to.
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
const Position & getPosition() const
Returns the position of this node.
Definition: NBNode.h:158
const std::set< NBTrafficLightDefinition * > & getControllingTLS() const
Returns the traffic lights that were assigned to this node.
Definition: NBNode.h:235
Encapsulated SAX-Attributes.
static GeoConvHelper * loadLocation(const SUMOSAXAttributes &attrs)
Parses network location description and registers it with GeoConveHelper::setLoaded.
static StringBijection< TrafficLightType > TrafficLightTypes
void deleteNode(const SUMOSAXAttributes &attrs)
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:195
const std::map< std::string, NBTrafficLightDefinition * > & getPrograms(const std::string &id) const
Returns all programs for the given tl-id.
void removeTrafficLights()
Removes all references to traffic lights that control this tls.
Definition: NBNode.cpp:266
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:52
OptionsCont & myOptions
A reference to the program's options.
void addJoinExclusion(const SUMOSAXAttributes &attrs)
std::vector< std::string > getVector()
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:201
void addCluster2Join(std::set< std::string > cluster)
add ids of nodes which shall be joined into a single node
Definition: NBNodeCont.cpp:451
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
void addJoinCluster(const SUMOSAXAttributes &attrs)
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
A storage for options typed value containers)
Definition: OptionsCont.h:108
void set(SUMOReal x, SUMOReal y)
Definition: Position.h:78
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:79
void mul(SUMOReal val)
Multiplies both positions with the given value.
Definition: Position.h:99
Represents a single node (junction) during network building.
Definition: NBNode.h:74
T get(const std::string &str) const
bool removeFully(const std::string id)
Removes a logic definition (and all programs) from the dictionary.
bool insert(NBTrafficLightDefinition *logic, bool forceInsert=false)
Adds a logic definition to the dictionary.
#define SUMOReal
Definition: config.h:215
void addNode(const SUMOSAXAttributes &attrs)
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue, bool report=true) const
Tries to read given attribute assuming it is an int.
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:124
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:64
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:54
bool extract(NBNode *node, bool remember=false)
Removes the given node but does not delete it.
Definition: NBNodeCont.cpp:165
void processTrafficLightDefinitions(const SUMOSAXAttributes &attrs, NBNode *currentNode)
Builds the defined traffic light or adds a node to it.
TrafficLightType
NIXMLNodesHandler(NBNodeCont &nc, NBTrafficLightLogicCont &tlc, OptionsCont &options)
Constructor.