SUMO - Simulation of Urban MObility
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.dlr.de/
13 // Copyright (C) 2001-2016 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  } else {
122  myPosition.set(0, 0, 0); // better to reset than to reuse the previous (z)-value
123  }
124  if (attrs.hasAttribute(SUMO_ATTR_X)) {
125  myPosition.set(attrs.get<SUMOReal>(SUMO_ATTR_X, myID.c_str(), ok), myPosition.y());
126  xOk = true;
127  needConversion = true;
128  }
129  if (attrs.hasAttribute(SUMO_ATTR_Y)) {
130  myPosition.set(myPosition.x(), attrs.get<SUMOReal>(SUMO_ATTR_Y, myID.c_str(), ok));
131  yOk = true;
132  needConversion = true;
133  }
134  if (attrs.hasAttribute(SUMO_ATTR_Z)) {
135  myPosition.set(myPosition.x(), myPosition.y(), attrs.get<SUMOReal>(SUMO_ATTR_Z, myID.c_str(), ok));
136  }
137  if (xOk && yOk) {
138  if (needConversion && !NBNetBuilder::transformCoordinates(myPosition, true, myLocation)) {
139  WRITE_ERROR("Unable to project coordinates for node '" + myID + "'.");
140  }
141  } else {
142  WRITE_ERROR("Missing position (at node ID='" + myID + "').");
143  }
144  bool updateEdgeGeometries = node != 0 && myPosition != node->getPosition();
145  // check whether the y-axis shall be flipped
146  if (myOptions.getBool("flip-y-axis")) {
147  myPosition.mul(1.0, -1.0);
148  }
149  processNodeType(attrs, node, myID, myPosition, updateEdgeGeometries, myNodeCont, myTLLogicCont);
150 }
151 
152 
153 void
154 NIXMLNodesHandler::processNodeType(const SUMOSAXAttributes& attrs, NBNode* node, const std::string& nodeID, const Position& position,
155  bool updateEdgeGeometries,
157  bool ok = true;
158  // get the type
160  if (node != 0) {
161  type = node->getType();
162  }
163  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, nodeID.c_str(), ok, "");
164  if (SUMOXMLDefinitions::NodeTypes.hasString(typeS)) {
165  type = SUMOXMLDefinitions::NodeTypes.get(typeS);
166  if (type == NODETYPE_DEAD_END_DEPRECATED || type == NODETYPE_DEAD_END) {
167  // dead end is a computed status. Reset this to unknown so it will
168  // be corrected if additional connections are loaded
169  type = NODETYPE_UNKNOWN;
170  }
171  }
172  std::set<NBTrafficLightDefinition*> oldTLS;
173  // check whether a prior node shall be modified
174  if (node == 0) {
175  node = new NBNode(nodeID, position, type);
176  if (!nc.insert(node)) {
177  throw ProcessError("Could not insert node though checked this before (id='" + nodeID + "').");
178  }
179  } else {
180  // patch information
181  oldTLS = node->getControllingTLS();
182  node->reinit(position, type, updateEdgeGeometries);
183  }
184  // process traffic light definition
185  if (NBNode::isTrafficLight(type)) {
186  processTrafficLightDefinitions(attrs, node, tlc);
187  }
188  // remove previously set tls if this node is not controlled by them
189  for (std::set<NBTrafficLightDefinition*>::iterator i = oldTLS.begin(); i != oldTLS.end(); ++i) {
190  if ((*i)->getNodes().size() == 0) {
191  tlc.removeFully((*i)->getID());
192  }
193  }
194 
195  // set optional shape
196  PositionVector shape;
197  if (attrs.hasAttribute(SUMO_ATTR_SHAPE)) {
198  shape = attrs.getOpt<PositionVector>(SUMO_ATTR_SHAPE, nodeID.c_str(), ok, PositionVector());
199  if (shape.size() > 2) {
200  shape.closePolygon();
201  }
202  node->setCustomShape(shape);
203  }
204  // set optional radius
205  if (attrs.hasAttribute(SUMO_ATTR_RADIUS)) {
206  node->setRadius(attrs.get<SUMOReal>(SUMO_ATTR_RADIUS, nodeID.c_str(), ok));
207  }
208  // set optional keepClear flag
209  if (attrs.hasAttribute(SUMO_ATTR_KEEP_CLEAR)) {
210  node->setKeepClear(attrs.get<bool>(SUMO_ATTR_KEEP_CLEAR, nodeID.c_str(), ok));
211  }
212 }
213 
214 
215 void
217  bool ok = true;
218  // get the id, report a warning if not given or empty...
219  myID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
220  if (!ok) {
221  return;
222  }
223  NBNode* node = myNodeCont.retrieve(myID);
224  if (node == 0) {
225  WRITE_WARNING("Ignoring tag '" + toString(SUMO_TAG_DELETE) + "' for unknown node '" +
226  myID + "'");
227  return;
228  } else {
229  myNodeCont.extract(node, true);
230  }
231 }
232 
233 
234 void
236  bool ok = true;
237  const std::string clusterString = attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok);
238  const std::vector<std::string> ids = StringTokenizer(clusterString).getVector();
239  if (ok) {
240  myNodeCont.addCluster2Join(std::set<std::string>(ids.begin(), ids.end()));
241  }
242 }
243 
244 
245 void
247  bool ok = true;
248  const std::vector<std::string> ids = StringTokenizer(
249  attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok)).getVector();
250  if (ok) {
252  }
253 }
254 
255 
256 void
258  NBNode* currentNode, NBTrafficLightLogicCont& tlc) {
259  // try to get the tl-id
260  // if a tl-id is given, we will look whether this tl already exists
261  // if so, we will add the node to it (and to all programs with this id), otherwise allocate a new one with this id
262  // if no tl-id exists, we will build a tl with the node's id
263  std::set<NBTrafficLightDefinition*> tlDefs;
264  bool ok = true;
265 
266  std::string oldTlID = "";
267  std::string oldTypeS = OptionsCont::getOptions().getString("tls.default-type");
268 
269  if (currentNode->isTLControlled()) {
270  NBTrafficLightDefinition* oldDef = *(currentNode->getControllingTLS().begin());
271  oldTlID = oldDef->getID();
272  oldTypeS = toString(oldDef->getType());
273  }
274  std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, 0, ok, oldTlID);
275  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TLTYPE, 0, ok, oldTypeS);
276  if (tlID != oldTlID || typeS != oldTypeS) {
277  currentNode->removeTrafficLights();
278  }
279  TrafficLightType type;
280  if (SUMOXMLDefinitions::TrafficLightTypes.hasString(typeS)) {
282  } else {
283  WRITE_ERROR("Unknown traffic light type '" + typeS + "' for node '" + currentNode->getID() + "'.");
284  return;
285  }
286  if (tlID != "" && tlc.getPrograms(tlID).size() > 0) {
287  // we already have definitions for this tlID
288  const std::map<std::string, NBTrafficLightDefinition*>& programs = tlc.getPrograms(tlID);
289  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
290  for (it = programs.begin(); it != programs.end(); it++) {
291  if (it->second->getType() != type) {
292  WRITE_ERROR("Mismatched traffic light type '" + typeS + "' for tl '" + tlID + "'.");
293  ok = false;
294  } else {
295  tlDefs.insert(it->second);
296  it->second->addNode(currentNode);
297  }
298  }
299  } else {
300  // we need to add a new defition
301  tlID = (tlID == "" ? currentNode->getID() : tlID);
302  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(tlID, currentNode, 0, type);
303  if (!tlc.insert(tlDef)) {
304  // actually, nothing should fail here
305  delete tlDef;
306  throw ProcessError("Could not allocate tls '" + currentNode->getID() + "'.");
307  }
308  tlDefs.insert(tlDef);
309  }
310  // process inner edges which shall be controlled
311  std::vector<std::string> controlledInner;
312  SUMOSAXAttributes::parseStringVector(attrs.getOpt<std::string>(SUMO_ATTR_CONTROLLED_INNER, 0, ok, ""), controlledInner);
313  if (controlledInner.size() != 0) {
314  for (std::set<NBTrafficLightDefinition*>::iterator it = tlDefs.begin(); it != tlDefs.end(); it++) {
315  (*it)->addControlledInnerEdges(controlledInner);
316  }
317  }
318 }
319 
320 
321 
322 /****************************************************************************/
323 
const std::map< std::string, NBTrafficLightDefinition * > & getPrograms(const std::string &id) const
Returns all programs for the given tl-id.
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:110
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
Whether vehicles must keep the junction clear.
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:450
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:266
TrafficLightType getType() const
get the algorithm type (static etc..)
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.
bool getBool(const std::string &name) const
Returns the boolean-value of the named option (only for Option_Bool)
const std::string & getID() const
Returns the id.
Definition: Named.h:66
void setCustomShape(const PositionVector &shape)
set the junction shape
Definition: NBNode.cpp:1728
SAX-handler base for SUMO-files.
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:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
NBNodeCont & myNodeCont
The node container to add built nodes to.
bool isTLControlled() const
Returns whether this node is controlled by any tls.
Definition: NBNode.h:304
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
The turning radius at an intersection in m.
Encapsulated SAX-Attributes.
void setRadius(SUMOReal radius)
set the turning radius
Definition: NBNode.h:510
static GeoConvHelper * loadLocation(const SUMOSAXAttributes &attrs)
Parses network location description and registers it with GeoConveHelper::setLoaded.
static StringBijection< TrafficLightType > TrafficLightTypes
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
T get(const std::string &str) const
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
void deleteNode(const SUMOSAXAttributes &attrs)
void removeTrafficLights()
Removes all references to traffic lights that control this tls.
Definition: NBNode.cpp:331
SUMOReal x() const
Returns the x-position.
Definition: Position.h:63
static void processNodeType(const SUMOSAXAttributes &attrs, NBNode *node, const std::string &nodeID, const Position &position, bool updateEdgeGeometries, NBNodeCont &nc, NBTrafficLightLogicCont &tlc)
parses node attributes (not related to positioning)
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:55
OptionsCont & myOptions
A reference to the program&#39;s options.
static void processTrafficLightDefinitions(const SUMOSAXAttributes &attrs, NBNode *currentNode, NBTrafficLightLogicCont &tlc)
Builds the defined traffic light or adds a node to it.
void setKeepClear(bool keepClear)
set the keepClear flag
Definition: NBNode.h:515
void addJoinExclusion(const SUMOSAXAttributes &attrs)
std::vector< std::string > getVector()
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
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.
void addCluster2Join(std::set< std::string > cluster)
add ids of nodes which shall be joined into a single node
Definition: NBNodeCont.cpp:466
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
const std::set< NBTrafficLightDefinition * > & getControllingTLS() const
Returns the traffic lights that were assigned to this node.
Definition: NBNode.h:318
void addJoinCluster(const SUMOSAXAttributes &attrs)
A storage for options typed value containers)
Definition: OptionsCont.h:99
void set(SUMOReal x, SUMOReal y)
Definition: Position.h:78
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:265
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:81
void mul(SUMOReal val)
Multiplies both positions with the given value.
Definition: Position.h:99
const Position & getPosition() const
Returns the position of this node.
Definition: NBNode.h:228
Represents a single node (junction) during network building.
Definition: NBNode.h:74
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:213
void addNode(const SUMOSAXAttributes &attrs)
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:63
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:151
SUMOReal y() const
Returns the y-position.
Definition: Position.h:68
void closePolygon()
ensures that the last position equals the first
static bool isTrafficLight(SumoXMLNodeType type)
return whether the given type is a traffic light
Definition: NBNode.cpp:2654
TrafficLightType
NIXMLNodesHandler(NBNodeCont &nc, NBTrafficLightLogicCont &tlc, OptionsCont &options)
Constructor.