Eclipse SUMO - Simulation of Urban MObility
NIXMLNodesHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 // Copyright (C) 2001-2024 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
21 // Importer for network nodes stored in XML
22 /****************************************************************************/
23 #include <config.h>
24 
25 #include <string>
26 #include <iostream>
31 #include <utils/common/ToString.h>
35 #include <netbuild/NBNodeCont.h>
37 #include <netbuild/NBOwnTLDef.h>
38 #include <netbuild/NBNetBuilder.h>
39 #include "NIXMLNodesHandler.h"
40 #include "NIImporter_SUMO.h"
41 
42 
43 // ===========================================================================
44 // method definitions
45 // ===========================================================================
48  OptionsCont& options) :
49  SUMOSAXHandler("xml-nodes - file"),
50  myOptions(options),
51  myNodeCont(nc),
52  myEdgeCont(ec),
53  myTLLogicCont(tlc),
54  myLocation(nullptr),
55  myLastParameterised(nullptr) {
56 }
57 
58 
60  delete myLocation;
61 }
62 
63 
64 void
66  const SUMOSAXAttributes& attrs) {
67  switch (element) {
68  case SUMO_TAG_LOCATION:
70  if (myLocation) {
72  }
73  break;
74  case SUMO_TAG_NODE:
75  addNode(attrs);
76  break;
77  case SUMO_TAG_JOIN:
78  addJoinCluster(attrs);
79  break;
81  addJoinExclusion(attrs);
82  break;
83  case SUMO_TAG_DEL:
84  deleteNode(attrs);
85  break;
86  case SUMO_TAG_PARAM:
87  if (myLastParameterised != nullptr) {
88  bool ok = true;
89  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
90  // circumventing empty string test
91  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
93  }
94  break;
95  default:
96  break;
97  }
98 }
99 
100 
101 void
103  switch (element) {
104  case SUMO_TAG_NODE:
105  myLastParameterised = nullptr;
106  break;
107  default:
108  break;
109  }
110 }
111 
112 
113 void
115  bool ok = true;
116  // get the id, report a warning if not given or empty...
117  myID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
118  if (!ok) {
119  return;
120  }
121  NBNode* node = myNodeCont.retrieve(myID);
122  // retrieve the position of the node
123  bool xOk = false;
124  bool yOk = false;
125  bool needConversion = true;
126  if (node != nullptr) {
127  myPosition = node->getPosition();
128  xOk = yOk = true;
129  needConversion = false;
130  } else {
131  myPosition.set(0, 0, 0); // better to reset than to reuse the previous (z)-value
132  }
133  if (attrs.hasAttribute(SUMO_ATTR_X)) {
134  myPosition.set(attrs.get<double>(SUMO_ATTR_X, myID.c_str(), ok), myPosition.y());
135  xOk = true;
136  needConversion = true;
137  }
138  if (attrs.hasAttribute(SUMO_ATTR_Y)) {
139  myPosition.set(myPosition.x(), attrs.get<double>(SUMO_ATTR_Y, myID.c_str(), ok));
140  yOk = true;
141  needConversion = true;
142  }
143  if (attrs.hasAttribute(SUMO_ATTR_Z)) {
144  myPosition.set(myPosition.x(), myPosition.y(), attrs.get<double>(SUMO_ATTR_Z, myID.c_str(), ok));
145  }
146  if (xOk && yOk) {
147  if (needConversion && !NBNetBuilder::transformCoordinate(myPosition, true, myLocation)) {
148  WRITE_ERRORF(TL("Unable to project coordinates for node '%'."), myID);
149  }
150  } else {
151  WRITE_ERRORF(TL("Missing position (at node ID='%')."), myID);
152  }
153  bool updateEdgeGeometries = node != nullptr && myPosition != node->getPosition();
154  node = processNodeType(attrs, node, myID, myPosition, updateEdgeGeometries, myNodeCont, myEdgeCont, myTLLogicCont);
155  myLastParameterised = node;
156 }
157 
158 
159 NBNode*
160 NIXMLNodesHandler::processNodeType(const SUMOSAXAttributes& attrs, NBNode* node, const std::string& nodeID, const Position& position,
161  bool updateEdgeGeometries,
163  bool ok = true;
164  // get the type
166  if (node != nullptr) {
167  type = node->getType();
168  }
169  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, nodeID.c_str(), ok, "");
170  if (SUMOXMLDefinitions::NodeTypes.hasString(typeS)) {
171  type = SUMOXMLDefinitions::NodeTypes.get(typeS);
173  // dead end is a computed status. Reset this to unknown so it will
174  // be corrected if additional connections are loaded
176  }
177  }
178  std::set<NBTrafficLightDefinition*> oldTLS;
179  // check whether a prior node shall be modified
180  const bool isPatch = node != nullptr;
181  if (node == nullptr) {
182  node = new NBNode(nodeID, position, type);
183  if (!nc.insert(node)) {
184  throw ProcessError(TLF("Could not insert node though checked this before (id='%').", nodeID));
185  }
186  } else {
187  // patch information
188  oldTLS = node->getControllingTLS();
189  if (node->getType() == SumoXMLNodeType::PRIORITY
191  ec.removeRoundabout(node);
192  }
193  node->reinit(position, type, updateEdgeGeometries);
194  }
195  // process traffic light definition
196  if (NBNode::isTrafficLight(type)) {
197  processTrafficLightDefinitions(attrs, node, tlc);
198  } else if (isPatch && typeS != "") {
199  nc.markAsNotTLS(node);
200  }
201  // remove previously set tls if this node is not controlled by them
202  for (std::set<NBTrafficLightDefinition*>::iterator i = oldTLS.begin(); i != oldTLS.end(); ++i) {
203  if ((*i)->getNodes().size() == 0) {
204  tlc.removeFully((*i)->getID());
205  }
206  }
207 
208  // set optional shape
209  PositionVector shape;
210  if (attrs.hasAttribute(SUMO_ATTR_SHAPE)) {
211  shape = attrs.getOpt<PositionVector>(SUMO_ATTR_SHAPE, nodeID.c_str(), ok, PositionVector());
213  WRITE_ERRORF(TL("Unable to project node shape at node '%'."), node->getID());
214  }
215  if (shape.size() > 2) {
216  shape.closePolygon();
217  }
218  node->setCustomShape(shape);
219  }
220  // set optional radius
221  if (attrs.hasAttribute(SUMO_ATTR_RADIUS)) {
222  node->setRadius(attrs.get<double>(SUMO_ATTR_RADIUS, nodeID.c_str(), ok));
223  }
224  // set optional keepClear flag
225  if (attrs.hasAttribute(SUMO_ATTR_KEEP_CLEAR)) {
226  node->setKeepClear(attrs.get<bool>(SUMO_ATTR_KEEP_CLEAR, nodeID.c_str(), ok));
227  }
228  node->setRightOfWay(attrs.getOpt<RightOfWay>(SUMO_ATTR_RIGHT_OF_WAY, nodeID.c_str(), ok, node->getRightOfWay()));
229  node->setFringeType(attrs.getOpt<FringeType>(SUMO_ATTR_FRINGE, nodeID.c_str(), ok, node->getFringeType()));
230  // set optional name
231  if (attrs.hasAttribute(SUMO_ATTR_NAME)) {
232  node->setName(attrs.get<std::string>(SUMO_ATTR_NAME, nodeID.c_str(), ok));
233  }
234  return node;
235 }
236 
237 
238 void
240  bool ok = true;
241  // get the id, report a warning if not given or empty...
242  myID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
243  if (!ok) {
244  return;
245  }
246  NBNode* node = myNodeCont.retrieve(myID);
247  if (node == nullptr) {
248  WRITE_WARNING("Ignoring tag '" + toString(SUMO_TAG_DEL) + "' for unknown node '" +
249  myID + "'");
250  return;
251  } else {
252  myNodeCont.extract(node, true);
253  }
254 }
255 
256 
257 void
259  bool ok = true;
260  const std::string clusterString = attrs.get<std::string>(SUMO_ATTR_NODES, nullptr, ok);
261  const std::set<std::string>& cluster = StringTokenizer(clusterString).getSet();
262 
263  myID = attrs.getOpt<std::string>(SUMO_ATTR_ID, nullptr, ok, myNodeCont.createClusterId(cluster));
264 
266  if (attrs.hasAttribute(SUMO_ATTR_X)) {
267  pos.setx(attrs.get<double>(SUMO_ATTR_X, myID.c_str(), ok));
268  }
269  if (attrs.hasAttribute(SUMO_ATTR_Y)) {
270  pos.sety(attrs.get<double>(SUMO_ATTR_Y, myID.c_str(), ok));
271  }
272  if (attrs.hasAttribute(SUMO_ATTR_Z)) {
273  pos.setz(attrs.get<double>(SUMO_ATTR_Z, myID.c_str(), ok));
274  }
275 
276  NBNode* node = processNodeType(attrs, nullptr, myID, pos, false, myNodeCont, myEdgeCont, myTLLogicCont);
277  if (ok) {
278  myNodeCont.addCluster2Join(cluster, node);
279  }
280 }
281 
282 
283 void
285  bool ok = true;
286  const std::vector<std::string> ids = StringTokenizer(
287  attrs.get<std::string>(SUMO_ATTR_NODES, nullptr, ok)).getVector();
288  if (ok) {
290  }
291 }
292 
293 
294 void
296  NBNode* currentNode, NBTrafficLightLogicCont& tlc) {
297  // try to get the tl-id
298  // if a tl-id is given, we will look whether this tl already exists
299  // if so, we will add the node to it (and to all programs with this id), otherwise allocate a new one with this id
300  // if no tl-id exists, we will build a tl with the node's id
301  std::set<NBTrafficLightDefinition*> tlDefs;
302  bool ok = true;
303 
304  std::string oldTlID = "";
305  std::string oldTypeS = OptionsCont::getOptions().getString("tls.default-type");
306 
307  if (currentNode->isTLControlled()) {
308  NBTrafficLightDefinition* oldDef = *(currentNode->getControllingTLS().begin());
309  oldTlID = oldDef->getID();
310  oldTypeS = toString(oldDef->getType());
311  }
312  std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, nullptr, ok, oldTlID);
313  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TLTYPE, nullptr, ok, oldTypeS);
314  if (tlID != oldTlID || typeS != oldTypeS) {
315  currentNode->removeTrafficLights();
316  }
317  TrafficLightType type;
318  if (SUMOXMLDefinitions::TrafficLightTypes.hasString(typeS)) {
320  } else {
321  WRITE_ERRORF(TL("Unknown traffic light type '%' for node '%'."), typeS, currentNode->getID());
322  return;
323  }
325  if (attrs.hasAttribute(SUMO_ATTR_TLLAYOUT)) {
326  std::string layoutS = attrs.get<std::string>(SUMO_ATTR_TLLAYOUT, nullptr, ok);
327  if (SUMOXMLDefinitions::TrafficLightLayouts.hasString(layoutS)) {
329  } else {
330  WRITE_ERRORF(TL("Unknown traffic light layout '%' for node '%'."), typeS, currentNode->getID());
331  return;
332  }
333  }
334  if (tlID != "" && tlc.getPrograms(tlID).size() > 0) {
335  // we already have definitions for this tlID
336  for (auto item : tlc.getPrograms(tlID)) {
337  NBTrafficLightDefinition* def = item.second;
338  tlDefs.insert(def);
339  def->addNode(currentNode);
340  if (def->getType() != type && attrs.hasAttribute(SUMO_ATTR_TLTYPE)) {
341  WRITE_WARNINGF(TL("Changing traffic light type '%' to '%' for tl '%'."), toString(def->getType()), typeS, tlID);
342  def->setType(type);
343  if (type != TrafficLightType::STATIC && dynamic_cast<NBLoadedSUMOTLDef*>(def) != nullptr) {
344  dynamic_cast<NBLoadedSUMOTLDef*>(def)->guessMinMaxDuration();
345  }
346  }
347  if (layout != TrafficLightLayout::DEFAULT && dynamic_cast<NBOwnTLDef*>(def) != nullptr) {
348  dynamic_cast<NBOwnTLDef*>(def)->setLayout(layout);
349  }
350  }
351  } else {
352  // we need to add a new defition
353  tlID = (tlID == "" ? currentNode->getID() : tlID);
354  NBOwnTLDef* tlDef = new NBOwnTLDef(tlID, currentNode, 0, type);
355  if (!tlc.insert(tlDef)) {
356  // actually, nothing should fail here
357  delete tlDef;
358  throw ProcessError(TLF("Could not allocate tls '%'.", currentNode->getID()));
359  }
360  tlDef->setLayout(layout);
361  tlDefs.insert(tlDef);
362  }
363  // process inner edges which shall be controlled
364  const std::vector<std::string>& controlledInner = attrs.getOpt<std::vector<std::string> >(SUMO_ATTR_CONTROLLED_INNER, nullptr, ok);
365  if (controlledInner.size() != 0) {
366  for (std::set<NBTrafficLightDefinition*>::iterator it = tlDefs.begin(); it != tlDefs.end(); it++) {
367  (*it)->addControlledInnerEdges(controlledInner);
368  }
369  }
370 }
371 
372 
373 /****************************************************************************/
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:296
#define WRITE_ERRORF(...)
Definition: MsgHandler.h:305
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:295
#define TL(string)
Definition: MsgHandler.h:315
#define TLF(string,...)
Definition: MsgHandler.h:317
TrafficLightType
@ SUMO_TAG_JOINEXCLUDE
join exlude operation
@ SUMO_TAG_LOCATION
@ SUMO_TAG_JOIN
Join operation.
@ SUMO_TAG_NODE
alternative definition for junction
@ SUMO_TAG_PARAM
parameter associated to a certain key
@ SUMO_TAG_DEL
delete certain element (note: DELETE is a macro)
TrafficLightLayout
FringeType
classifying boundary nodes
SumoXMLNodeType
Numbers representing special SUMO-XML-attribute values for representing node- (junction-) types used ...
RightOfWay
algorithms for computing right of way
@ SUMO_ATTR_NODES
a list of node ids, used for controlling joining
@ SUMO_ATTR_VALUE
@ SUMO_ATTR_RADIUS
The turning radius at an intersection in m.
@ SUMO_ATTR_Y
@ SUMO_ATTR_Z
@ SUMO_ATTR_X
@ SUMO_ATTR_TLLAYOUT
node: the layout of the traffic light program
@ SUMO_ATTR_FRINGE
Fringe type of node.
@ SUMO_ATTR_SHAPE
edge: the shape in xml-definition
@ SUMO_ATTR_TLTYPE
node: the type of traffic light
@ SUMO_ATTR_NAME
@ SUMO_ATTR_TLID
link,node: the traffic light id responsible for this link
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_ID
@ SUMO_ATTR_RIGHT_OF_WAY
How to compute right of way.
@ SUMO_ATTR_CONTROLLED_INNER
@ SUMO_ATTR_KEY
@ SUMO_ATTR_KEEP_CLEAR
Whether vehicles must keep the junction clear.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
const std::string & getFileName() const
returns the current file name
static void setLoadedPlain(const std::string &nodFile, const GeoConvHelper &loaded)
registers the coordinate transformation as having been loaded from the given file
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:59
void removeRoundabout(const NBNode *node)
remove roundabout that contains the given node
A loaded (complete) traffic light logic.
static bool transformCoordinates(PositionVector &from, bool includeInBoundary=true, GeoConvHelper *from_srs=nullptr)
static bool transformCoordinate(Position &from, bool includeInBoundary=true, GeoConvHelper *from_srs=nullptr)
transforms loaded coordinates handles projections, offsets (using GeoConvHelper) and import of height...
Container for nodes during the netbuilding process.
Definition: NBNodeCont.h:57
void markAsNotTLS(const NBNode *node)
mark a node as explicitly not controlled by a TLS
Definition: NBNodeCont.h:368
std::string createClusterId(const NodeSet &cluster, const std::string &prefix="cluster_")
generate id from cluster node ids
Definition: NBNodeCont.h:136
void addJoinExclusion(const std::vector< std::string > &ids)
Definition: NBNodeCont.cpp:691
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:87
NBNode * retrieve(const std::string &id) const
Returns the node with the given name.
Definition: NBNodeCont.cpp:116
bool extract(NBNode *node, bool remember=false)
Removes the given node but does not delete it.
Definition: NBNodeCont.cpp:157
void addCluster2Join(const std::set< std::string > &cluster, NBNode *node)
add ids of nodes which shall be joined into a single node
Definition: NBNodeCont.cpp:724
Represents a single node (junction) during network building.
Definition: NBNode.h:66
RightOfWay getRightOfWay() const
Returns hint on how to compute right of way.
Definition: NBNode.h:300
void reinit(const Position &position, SumoXMLNodeType type, bool updateEdgeGeometries=false)
Resets initial values.
Definition: NBNode.cpp:338
FringeType getFringeType() const
Returns fringe type.
Definition: NBNode.h:305
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:285
bool isTrafficLight() const
Definition: NBNode.h:822
void setRightOfWay(RightOfWay rightOfWay)
set method for computing right-of-way
Definition: NBNode.h:569
void setCustomShape(const PositionVector &shape)
set the junction shape
Definition: NBNode.cpp:2611
void setKeepClear(bool keepClear)
set the keepClear flag
Definition: NBNode.h:564
void removeTrafficLights(bool setAsPriority=false)
Removes all references to traffic lights that control this tls.
Definition: NBNode.cpp:413
void setRadius(double radius)
set the turning radius
Definition: NBNode.h:559
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:336
void setName(const std::string &name)
set intersection name
Definition: NBNode.h:579
const Position & getPosition() const
Definition: NBNode.h:260
void setFringeType(FringeType fringeType)
set method for computing right-of-way
Definition: NBNode.h:574
bool isTLControlled() const
Returns whether this node is controlled by any tls.
Definition: NBNode.h:331
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:44
void setLayout(TrafficLightLayout layout)
sets the layout for the generated signal plan
Definition: NBOwnTLDef.h:143
The base class for traffic light logic definitions.
TrafficLightType getType() const
get the algorithm type (static etc..)
virtual void addNode(NBNode *node)
Adds a node to the traffic light logic.
virtual void setType(TrafficLightType type)
set the algorithm type (static etc..)
A container for traffic light definitions and built programs.
const std::map< std::string, NBTrafficLightDefinition * > & getPrograms(const std::string &id) const
Returns all programs for the given tl-id.
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.
static GeoConvHelper * loadLocation(const SUMOSAXAttributes &attrs, bool setLoaded=true)
Parses network location description and registers it with GeoConveHelper::setLoaded.
~NIXMLNodesHandler()
Destructor.
std::string myID
The id of the currently parsed node.
void addJoinCluster(const SUMOSAXAttributes &attrs)
GeoConvHelper * myLocation
The coordinate transformation which was used compute the node coordinates.
Position myPosition
The position of the currently parsed node.
NBNodeCont & myNodeCont
The node container to add built nodes to.
void deleteNode(const SUMOSAXAttributes &attrs)
NBTrafficLightLogicCont & myTLLogicCont
The traffic lights container to add built tls to.
static void processTrafficLightDefinitions(const SUMOSAXAttributes &attrs, NBNode *currentNode, NBTrafficLightLogicCont &tlc)
Builds the defined traffic light or adds a node to it.
NBEdgeCont & myEdgeCont
The node container to add built nodes to.
Parameterised * myLastParameterised
last item the could receive parameters
void myEndElement(int element)
Called when a closing tag occurs.
void addNode(const SUMOSAXAttributes &attrs)
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
NIXMLNodesHandler(NBNodeCont &nc, NBEdgeCont &ec, NBTrafficLightLogicCont &tlc, OptionsCont &options)
Constructor.
static NBNode * processNodeType(const SUMOSAXAttributes &attrs, NBNode *node, const std::string &nodeID, const Position &position, bool updateEdgeGeometries, NBNodeCont &nc, NBEdgeCont &ec, NBTrafficLightLogicCont &tlc)
parses node attributes (not related to positioning)
void addJoinExclusion(const SUMOSAXAttributes &attrs)
const std::string & getID() const
Returns the id.
Definition: Named.h:74
A storage for options typed value containers)
Definition: OptionsCont.h:89
std::string getString(const std::string &name) const
Returns the string-value of the named option (only for Option_String)
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:60
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
void setx(double x)
set position x
Definition: Position.h:70
void set(double x, double y)
set positions x and y
Definition: Position.h:85
static const Position INVALID
used to indicate that a position is valid
Definition: Position.h:317
double x() const
Returns the x-position.
Definition: Position.h:55
double y() const
Returns the y-position.
Definition: Position.h:60
A list of positions.
void closePolygon()
ensures that the last position equals the first
Encapsulated SAX-Attributes.
virtual std::string getString(int id, bool *isPresent=nullptr) const =0
Returns the string-value of the named (by its enum-value) attribute.
T getOpt(int attr, const char *objectid, bool &ok, T defaultValue=T(), bool report=true) const
Tries to read given attribute assuming it is an int.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
SAX-handler base for SUMO-files.
static StringBijection< SumoXMLNodeType > NodeTypes
node types
static StringBijection< TrafficLightType > TrafficLightTypes
traffic light types
static StringBijection< TrafficLightLayout > TrafficLightLayouts
traffic light layouts
T get(const std::string &str) const
std::set< std::string > getSet()
return set of strings
std::vector< std::string > getVector()
return vector of strings