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-2017 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 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
61  OptionsCont& options) :
62  SUMOSAXHandler("xml-nodes - file"),
63  myOptions(options),
64  myNodeCont(nc),
65  myTLLogicCont(tlc),
66  myLocation(0) {
67 }
68 
69 
71  delete myLocation;
72 }
73 
74 
75 void
77  const SUMOSAXAttributes& attrs) {
78  switch (element) {
79  case SUMO_TAG_LOCATION:
81  break;
82  case SUMO_TAG_NODE:
83  addNode(attrs);
84  break;
85  case SUMO_TAG_JOIN:
86  addJoinCluster(attrs);
87  break;
89  addJoinExclusion(attrs);
90  break;
91  case SUMO_TAG_DELETE:
92  deleteNode(attrs);
93  break;
94  default:
95  break;
96  }
97 }
98 
99 
100 void
102  bool ok = true;
103  // get the id, report a warning if not given or empty...
104  myID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
105  if (!ok) {
106  return;
107  }
108  NBNode* node = myNodeCont.retrieve(myID);
109  // retrieve the position of the node
110  bool xOk = false;
111  bool yOk = false;
112  bool needConversion = true;
113  if (node != 0) {
114  myPosition = node->getPosition();
115  xOk = yOk = true;
116  needConversion = false;
117  } else {
118  myPosition.set(0, 0, 0); // better to reset than to reuse the previous (z)-value
119  }
120  if (attrs.hasAttribute(SUMO_ATTR_X)) {
121  myPosition.set(attrs.get<double>(SUMO_ATTR_X, myID.c_str(), ok), myPosition.y());
122  xOk = true;
123  needConversion = true;
124  }
125  if (attrs.hasAttribute(SUMO_ATTR_Y)) {
126  myPosition.set(myPosition.x(), attrs.get<double>(SUMO_ATTR_Y, myID.c_str(), ok));
127  yOk = true;
128  needConversion = true;
129  }
130  if (attrs.hasAttribute(SUMO_ATTR_Z)) {
131  myPosition.set(myPosition.x(), myPosition.y(), attrs.get<double>(SUMO_ATTR_Z, myID.c_str(), ok));
132  }
133  if (xOk && yOk) {
134  if (needConversion && !NBNetBuilder::transformCoordinate(myPosition, true, myLocation)) {
135  WRITE_ERROR("Unable to project coordinates for node '" + myID + "'.");
136  }
137  } else {
138  WRITE_ERROR("Missing position (at node ID='" + myID + "').");
139  }
140  bool updateEdgeGeometries = node != 0 && myPosition != node->getPosition();
141  // check whether the y-axis shall be flipped
142  if (myOptions.getBool("flip-y-axis")) {
143  myPosition.mul(1.0, -1.0);
144  }
145  processNodeType(attrs, node, myID, myPosition, updateEdgeGeometries, myNodeCont, myTLLogicCont);
146 }
147 
148 
149 void
150 NIXMLNodesHandler::processNodeType(const SUMOSAXAttributes& attrs, NBNode* node, const std::string& nodeID, const Position& position,
151  bool updateEdgeGeometries,
153  bool ok = true;
154  // get the type
156  if (node != 0) {
157  type = node->getType();
158  }
159  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, nodeID.c_str(), ok, "");
160  if (SUMOXMLDefinitions::NodeTypes.hasString(typeS)) {
161  type = SUMOXMLDefinitions::NodeTypes.get(typeS);
162  if (type == NODETYPE_DEAD_END_DEPRECATED || type == NODETYPE_DEAD_END) {
163  // dead end is a computed status. Reset this to unknown so it will
164  // be corrected if additional connections are loaded
165  type = NODETYPE_UNKNOWN;
166  }
167  }
168  std::set<NBTrafficLightDefinition*> oldTLS;
169  // check whether a prior node shall be modified
170  if (node == 0) {
171  node = new NBNode(nodeID, position, type);
172  if (!nc.insert(node)) {
173  throw ProcessError("Could not insert node though checked this before (id='" + nodeID + "').");
174  }
175  } else {
176  // patch information
177  oldTLS = node->getControllingTLS();
178  node->reinit(position, type, updateEdgeGeometries);
179  }
180  // process traffic light definition
181  if (NBNode::isTrafficLight(type)) {
182  processTrafficLightDefinitions(attrs, node, tlc);
183  }
184  // remove previously set tls if this node is not controlled by them
185  for (std::set<NBTrafficLightDefinition*>::iterator i = oldTLS.begin(); i != oldTLS.end(); ++i) {
186  if ((*i)->getNodes().size() == 0) {
187  tlc.removeFully((*i)->getID());
188  }
189  }
190 
191  // set optional shape
192  PositionVector shape;
193  if (attrs.hasAttribute(SUMO_ATTR_SHAPE)) {
194  shape = attrs.getOpt<PositionVector>(SUMO_ATTR_SHAPE, nodeID.c_str(), ok, PositionVector());
195  if (shape.size() > 2) {
196  shape.closePolygon();
197  }
198  node->setCustomShape(shape);
199  }
200  // set optional radius
201  if (attrs.hasAttribute(SUMO_ATTR_RADIUS)) {
202  node->setRadius(attrs.get<double>(SUMO_ATTR_RADIUS, nodeID.c_str(), ok));
203  }
204  // set optional keepClear flag
205  if (attrs.hasAttribute(SUMO_ATTR_KEEP_CLEAR)) {
206  node->setKeepClear(attrs.get<bool>(SUMO_ATTR_KEEP_CLEAR, nodeID.c_str(), ok));
207  }
208 }
209 
210 
211 void
213  bool ok = true;
214  // get the id, report a warning if not given or empty...
215  myID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
216  if (!ok) {
217  return;
218  }
219  NBNode* node = myNodeCont.retrieve(myID);
220  if (node == 0) {
221  WRITE_WARNING("Ignoring tag '" + toString(SUMO_TAG_DELETE) + "' for unknown node '" +
222  myID + "'");
223  return;
224  } else {
225  myNodeCont.extract(node, true);
226  }
227 }
228 
229 
230 void
232  bool ok = true;
233  const std::string clusterString = attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok);
234  const std::vector<std::string> ids = StringTokenizer(clusterString).getVector();
235  if (ok) {
236  myNodeCont.addCluster2Join(std::set<std::string>(ids.begin(), ids.end()));
237  }
238 }
239 
240 
241 void
243  bool ok = true;
244  const std::vector<std::string> ids = StringTokenizer(
245  attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok)).getVector();
246  if (ok) {
248  }
249 }
250 
251 
252 void
254  NBNode* currentNode, NBTrafficLightLogicCont& tlc) {
255  // try to get the tl-id
256  // if a tl-id is given, we will look whether this tl already exists
257  // if so, we will add the node to it (and to all programs with this id), otherwise allocate a new one with this id
258  // if no tl-id exists, we will build a tl with the node's id
259  std::set<NBTrafficLightDefinition*> tlDefs;
260  bool ok = true;
261 
262  std::string oldTlID = "";
263  std::string oldTypeS = OptionsCont::getOptions().getString("tls.default-type");
264 
265  if (currentNode->isTLControlled()) {
266  NBTrafficLightDefinition* oldDef = *(currentNode->getControllingTLS().begin());
267  oldTlID = oldDef->getID();
268  oldTypeS = toString(oldDef->getType());
269  }
270  std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, 0, ok, oldTlID);
271  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TLTYPE, 0, ok, oldTypeS);
272  if (tlID != oldTlID || typeS != oldTypeS) {
273  currentNode->removeTrafficLights();
274  }
275  TrafficLightType type;
276  if (SUMOXMLDefinitions::TrafficLightTypes.hasString(typeS)) {
278  } else {
279  WRITE_ERROR("Unknown traffic light type '" + typeS + "' for node '" + currentNode->getID() + "'.");
280  return;
281  }
282  if (tlID != "" && tlc.getPrograms(tlID).size() > 0) {
283  // we already have definitions for this tlID
284  const std::map<std::string, NBTrafficLightDefinition*>& programs = tlc.getPrograms(tlID);
285  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
286  for (it = programs.begin(); it != programs.end(); it++) {
287  if (it->second->getType() != type) {
288  WRITE_ERROR("Mismatched traffic light type '" + typeS + "' for tl '" + tlID + "'.");
289  ok = false;
290  } else {
291  tlDefs.insert(it->second);
292  it->second->addNode(currentNode);
293  }
294  }
295  } else {
296  // we need to add a new defition
297  tlID = (tlID == "" ? currentNode->getID() : tlID);
298  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(tlID, currentNode, 0, type);
299  if (!tlc.insert(tlDef)) {
300  // actually, nothing should fail here
301  delete tlDef;
302  throw ProcessError("Could not allocate tls '" + currentNode->getID() + "'.");
303  }
304  tlDefs.insert(tlDef);
305  }
306  // process inner edges which shall be controlled
307  std::vector<std::string> controlledInner;
308  SUMOSAXAttributes::parseStringVector(attrs.getOpt<std::string>(SUMO_ATTR_CONTROLLED_INNER, 0, ok, ""), controlledInner);
309  if (controlledInner.size() != 0) {
310  for (std::set<NBTrafficLightDefinition*>::iterator it = tlDefs.begin(); it != tlDefs.end(); it++) {
311  (*it)->addControlledInnerEdges(controlledInner);
312  }
313  }
314 }
315 
316 
317 
318 /****************************************************************************/
319 
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:106
static StringBijection< SumoXMLNodeType > NodeTypes
node types
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 transformCoordinate(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:446
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:262
double y() const
Returns the y-position.
Definition: Position.h:68
TrafficLightType getType() const
get the algorithm type (static etc..)
double x() const
Returns the x-position.
Definition: Position.h:63
Position myPosition
The position of the currently parsed node.
The base class for traffic light logic definitions.
link,node: the traffic light id responsible for this link
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 set(double x, double y)
set positions x and y
Definition: Position.h:93
void setCustomShape(const PositionVector &shape)
set the junction shape
Definition: NBNode.cpp:1722
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:65
NBNodeCont & myNodeCont
The node container to add built nodes to.
void setRadius(double radius)
set the turning radius
Definition: NBNode.h:483
bool isTLControlled() const
Returns whether this node is controlled by any tls.
Definition: NBNode.h:288
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
The turning radius at an intersection in m.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
Encapsulated SAX-Attributes.
static GeoConvHelper * loadLocation(const SUMOSAXAttributes &attrs)
Parses network location description and registers it with GeoConveHelper::setLoaded.
static StringBijection< TrafficLightType > TrafficLightTypes
traffic light types
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:327
node: the type of traffic light
edge: the shape in xml-definition
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;.
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:488
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:462
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 (The set of tls that control this node) ...
Definition: NBNode.h:298
void addJoinCluster(const SUMOSAXAttributes &attrs)
Join operation.
alternative definition for junction
A storage for options typed value containers)
Definition: OptionsCont.h:99
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:257
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:77
const Position & getPosition() const
Definition: NBNode.h:232
Represents a single node (junction) during network building.
Definition: NBNode.h:75
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.
void addNode(const SUMOSAXAttributes &attrs)
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:63
void mul(double val)
Multiplies both positions with the given value.
Definition: Position.h:113
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:54
delete certain element
bool extract(NBNode *node, bool remember=false)
Removes the given node but does not delete it.
Definition: NBNodeCont.cpp:147
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:2698
join exlude operation
TrafficLightType
NIXMLNodesHandler(NBNodeCont &nc, NBTrafficLightLogicCont &tlc, OptionsCont &options)
Constructor.