SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules 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  // get the type
151  if (node != 0) {
152  type = node->getType();
153  }
154  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, myID.c_str(), ok, "");
155  if (SUMOXMLDefinitions::NodeTypes.hasString(typeS)) {
156  type = SUMOXMLDefinitions::NodeTypes.get(typeS);
157  if (type == NODETYPE_DEAD_END_DEPRECATED || type == NODETYPE_DEAD_END) {
158  // dead end is a computed status. Reset this to unknown so it will
159  // be corrected if additional connections are loaded
160  type = NODETYPE_UNKNOWN;
161  }
162  }
163  // check whether a prior node shall be modified
164  if (node == 0) {
165  node = new NBNode(myID, myPosition, type);
166  if (!myNodeCont.insert(node)) {
167  throw ProcessError("Could not insert node though checked this before (id='" + myID + "').");
168  }
169  } else {
170  // remove previously set tls if this node is not controlled by a tls
171  std::set<NBTrafficLightDefinition*> tls = node->getControllingTLS();
172  node->removeTrafficLights();
173  for (std::set<NBTrafficLightDefinition*>::iterator i = tls.begin(); i != tls.end(); ++i) {
174  if ((*i)->getNodes().size() == 0) {
175  myTLLogicCont.removeFully((*i)->getID());
176  }
177  }
178  // patch information
179  node->reinit(myPosition, type, updateEdgeGeometries);
180  }
181  // process traffic light definition
183  processTrafficLightDefinitions(attrs, node);
184  }
185  // set optional shape
186  PositionVector shape;
187  if (attrs.hasAttribute(SUMO_ATTR_SHAPE)) {
188  shape = attrs.getOpt<PositionVector>(SUMO_ATTR_SHAPE, myID.c_str(), ok, PositionVector());
189  if (shape.size() > 2) {
190  shape.closePolygon();
191  }
192  node->setCustomShape(shape);
193  }
194  // set optional radius
195  if (attrs.hasAttribute(SUMO_ATTR_RADIUS)) {
196  node->setRadius(attrs.get<SUMOReal>(SUMO_ATTR_RADIUS, myID.c_str(), ok));
197  }
198 }
199 
200 
201 void
203  bool ok = true;
204  // get the id, report a warning if not given or empty...
205  myID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
206  if (!ok) {
207  return;
208  }
209  NBNode* node = myNodeCont.retrieve(myID);
210  if (node == 0) {
211  WRITE_WARNING("Ignoring tag '" + toString(SUMO_TAG_DELETE) + "' for unknown node '" +
212  myID + "'");
213  return;
214  } else {
215  myNodeCont.extract(node, true);
216  }
217 }
218 
219 
220 void
222  bool ok = true;
223  const std::string clusterString = attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok);
224  const std::vector<std::string> ids = StringTokenizer(clusterString).getVector();
225  if (ok) {
226  myNodeCont.addCluster2Join(std::set<std::string>(ids.begin(), ids.end()));
227  }
228 }
229 
230 
231 void
233  bool ok = true;
234  const std::vector<std::string> ids = StringTokenizer(
235  attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok)).getVector();
236  if (ok) {
238  }
239 }
240 
241 
242 void
244  NBNode* currentNode) {
245  // try to get the tl-id
246  // if a tl-id is given, we will look whether this tl already exists
247  // if so, we will add the node to it (and to all programs with this id), otherwise allocate a new one with this id
248  // if no tl-id exists, we will build a tl with the node's id
249  std::set<NBTrafficLightDefinition*> tlDefs;
250  bool ok = true;
251  std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, 0, ok, "");
252  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TLTYPE, 0, ok,
253  OptionsCont::getOptions().getString("tls.default-type"));
254  TrafficLightType type;
255  if (SUMOXMLDefinitions::TrafficLightTypes.hasString(typeS)) {
257  } else {
258  WRITE_ERROR("Unknown traffic light type '" + typeS + "' for node '" + myID + "'.");
259  return;
260  }
261  if (tlID != "" && myTLLogicCont.getPrograms(tlID).size() > 0) {
262  // we already have definitions for this tlID
263  const std::map<std::string, NBTrafficLightDefinition*>& programs = myTLLogicCont.getPrograms(tlID);
264  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
265  for (it = programs.begin(); it != programs.end(); it++) {
266  if (it->second->getType() != type) {
267  WRITE_ERROR("Mismatched traffic light type '" + typeS + "' for tl '" + tlID + "'.");
268  ok = false;
269  } else {
270  tlDefs.insert(it->second);
271  it->second->addNode(currentNode);
272  }
273  }
274  } else {
275  // we need to add a new defition
276  tlID = (tlID == "" ? myID : tlID);
277  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(tlID, currentNode, 0, type);
278  if (!myTLLogicCont.insert(tlDef)) {
279  // actually, nothing should fail here
280  delete tlDef;
281  throw ProcessError("Could not allocate tls '" + myID + "'.");
282  }
283  tlDefs.insert(tlDef);
284  }
285  // process inner edges which shall be controlled
286  std::vector<std::string> controlledInner;
287  SUMOSAXAttributes::parseStringVector(attrs.getOpt<std::string>(SUMO_ATTR_CONTROLLED_INNER, 0, ok, ""), controlledInner);
288  if (controlledInner.size() != 0) {
289  for (std::set<NBTrafficLightDefinition*>::iterator it = tlDefs.begin(); it != tlDefs.end(); it++) {
290  (*it)->addControlledInnerEdges(controlledInner);
291  }
292  }
293 }
294 
295 
296 
297 /****************************************************************************/
298 
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:426
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:261
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:1467
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: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:239
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:323
Encapsulated SAX-Attributes.
void setRadius(SUMOReal radius)
set the turning radius
Definition: NBNode.h:511
static GeoConvHelper * loadLocation(const SUMOSAXAttributes &attrs)
Parses network location description and registers it with GeoConveHelper::setLoaded.
static StringBijection< TrafficLightType > TrafficLightTypes
A list of positions.
void deleteNode(const SUMOSAXAttributes &attrs)
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:276
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:311
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's options.
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:442
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:218
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
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.