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.dlr.de/
13 // Copyright (C) 2001-2015 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  // check whether a prior node shall be modified
173  if (node == 0) {
174  node = new NBNode(nodeID, position, type);
175  if (!nc.insert(node)) {
176  throw ProcessError("Could not insert node though checked this before (id='" + nodeID + "').");
177  }
178  } else {
179  // remove previously set tls if this node is not controlled by a tls
180  std::set<NBTrafficLightDefinition*> tls = node->getControllingTLS();
181  node->removeTrafficLights();
182  for (std::set<NBTrafficLightDefinition*>::iterator i = tls.begin(); i != tls.end(); ++i) {
183  if ((*i)->getNodes().size() == 0) {
184  tlc.removeFully((*i)->getID());
185  }
186  }
187  // patch information
188  node->reinit(position, type, updateEdgeGeometries);
189  }
190  // process traffic light definition
192  processTrafficLightDefinitions(attrs, node, tlc);
193  }
194  // set optional shape
195  PositionVector shape;
196  if (attrs.hasAttribute(SUMO_ATTR_SHAPE)) {
197  shape = attrs.getOpt<PositionVector>(SUMO_ATTR_SHAPE, nodeID.c_str(), ok, PositionVector());
198  if (shape.size() > 2) {
199  shape.closePolygon();
200  }
201  node->setCustomShape(shape);
202  }
203  // set optional radius
204  if (attrs.hasAttribute(SUMO_ATTR_RADIUS)) {
205  node->setRadius(attrs.get<SUMOReal>(SUMO_ATTR_RADIUS, nodeID.c_str(), ok));
206  }
207  // set optional keepClear flag
208  if (attrs.hasAttribute(SUMO_ATTR_KEEP_CLEAR)) {
209  node->setKeepClear(attrs.get<bool>(SUMO_ATTR_KEEP_CLEAR, nodeID.c_str(), ok));
210  }
211 }
212 
213 
214 void
216  bool ok = true;
217  // get the id, report a warning if not given or empty...
218  myID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
219  if (!ok) {
220  return;
221  }
222  NBNode* node = myNodeCont.retrieve(myID);
223  if (node == 0) {
224  WRITE_WARNING("Ignoring tag '" + toString(SUMO_TAG_DELETE) + "' for unknown node '" +
225  myID + "'");
226  return;
227  } else {
228  myNodeCont.extract(node, true);
229  }
230 }
231 
232 
233 void
235  bool ok = true;
236  const std::string clusterString = attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok);
237  const std::vector<std::string> ids = StringTokenizer(clusterString).getVector();
238  if (ok) {
239  myNodeCont.addCluster2Join(std::set<std::string>(ids.begin(), ids.end()));
240  }
241 }
242 
243 
244 void
246  bool ok = true;
247  const std::vector<std::string> ids = StringTokenizer(
248  attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok)).getVector();
249  if (ok) {
251  }
252 }
253 
254 
255 void
257  NBNode* currentNode, NBTrafficLightLogicCont& tlc) {
258  // try to get the tl-id
259  // if a tl-id is given, we will look whether this tl already exists
260  // if so, we will add the node to it (and to all programs with this id), otherwise allocate a new one with this id
261  // if no tl-id exists, we will build a tl with the node's id
262  std::set<NBTrafficLightDefinition*> tlDefs;
263  bool ok = true;
264  std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, 0, ok, "");
265  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TLTYPE, 0, ok,
266  OptionsCont::getOptions().getString("tls.default-type"));
267  TrafficLightType type;
268  if (SUMOXMLDefinitions::TrafficLightTypes.hasString(typeS)) {
270  } else {
271  WRITE_ERROR("Unknown traffic light type '" + typeS + "' for node '" + currentNode->getID() + "'.");
272  return;
273  }
274  if (tlID != "" && tlc.getPrograms(tlID).size() > 0) {
275  // we already have definitions for this tlID
276  const std::map<std::string, NBTrafficLightDefinition*>& programs = tlc.getPrograms(tlID);
277  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
278  for (it = programs.begin(); it != programs.end(); it++) {
279  if (it->second->getType() != type) {
280  WRITE_ERROR("Mismatched traffic light type '" + typeS + "' for tl '" + tlID + "'.");
281  ok = false;
282  } else {
283  tlDefs.insert(it->second);
284  it->second->addNode(currentNode);
285  }
286  }
287  } else {
288  // we need to add a new defition
289  tlID = (tlID == "" ? currentNode->getID() : tlID);
290  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(tlID, currentNode, 0, type);
291  if (!tlc.insert(tlDef)) {
292  // actually, nothing should fail here
293  delete tlDef;
294  throw ProcessError("Could not allocate tls '" + currentNode->getID() + "'.");
295  }
296  tlDefs.insert(tlDef);
297  }
298  // process inner edges which shall be controlled
299  std::vector<std::string> controlledInner;
300  SUMOSAXAttributes::parseStringVector(attrs.getOpt<std::string>(SUMO_ATTR_CONTROLLED_INNER, 0, ok, ""), controlledInner);
301  if (controlledInner.size() != 0) {
302  for (std::set<NBTrafficLightDefinition*>::iterator it = tlDefs.begin(); it != tlDefs.end(); it++) {
303  (*it)->addControlledInnerEdges(controlledInner);
304  }
305  }
306 }
307 
308 
309 
310 /****************************************************************************/
311 
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: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:266
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.
void setCustomShape(const PositionVector &shape)
set the junction shape
Definition: NBNode.cpp:1532
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:200
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:69
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)
const std::string & getID() const
Returns the id.
Definition: Named.h:60
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:235
The turning radius at an intersection in m.
const std::set< NBTrafficLightDefinition * > & getControllingTLS() const
Returns the traffic lights that were assigned to this node.
Definition: NBNode.h:325
Encapsulated SAX-Attributes.
void setRadius(SUMOReal radius)
set the turning radius
Definition: NBNode.h:516
static GeoConvHelper * loadLocation(const SUMOSAXAttributes &attrs)
Parses network location description and registers it with GeoConveHelper::setLoaded.
static StringBijection< TrafficLightType > TrafficLightTypes
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
A list of positions.
void deleteNode(const SUMOSAXAttributes &attrs)
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:272
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:323
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:53
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:521
void addJoinExclusion(const SUMOSAXAttributes &attrs)
std::vector< std::string > getVector()
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
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:75
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:108
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:149
void closePolygon()
ensures that the last position equals the first
TrafficLightType
NIXMLNodesHandler(NBNodeCont &nc, NBTrafficLightLogicCont &tlc, OptionsCont &options)
Constructor.