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.org/sumo
3 // Copyright (C) 2001-2018 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
18 // Importer for network nodes stored in XML
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #include <config.h>
26 
27 #include <string>
28 #include <iostream>
29 #include <xercesc/sax/HandlerBase.hpp>
30 #include <xercesc/sax/AttributeList.hpp>
31 #include <xercesc/sax/SAXParseException.hpp>
32 #include <xercesc/sax/SAXException.hpp>
37 #include <utils/common/ToString.h>
41 #include <netbuild/NBNodeCont.h>
43 #include <netbuild/NBOwnTLDef.h>
44 #include <netbuild/NBNetBuilder.h>
45 #include "NIXMLNodesHandler.h"
46 #include "NIImporter_SUMO.h"
47 
48 
49 // ===========================================================================
50 // method definitions
51 // ===========================================================================
54  OptionsCont& options) :
55  SUMOSAXHandler("xml-nodes - file"),
56  myOptions(options),
57  myNodeCont(nc),
58  myTLLogicCont(tlc),
59  myLocation(nullptr),
60  myLastParameterised(nullptr) {
61 }
62 
63 
65  delete myLocation;
66 }
67 
68 
69 void
71  const SUMOSAXAttributes& attrs) {
72  switch (element) {
73  case SUMO_TAG_LOCATION:
75  break;
76  case SUMO_TAG_NODE:
77  addNode(attrs);
78  break;
79  case SUMO_TAG_JOIN:
80  addJoinCluster(attrs);
81  break;
83  addJoinExclusion(attrs);
84  break;
85  case SUMO_TAG_DELETE:
86  deleteNode(attrs);
87  break;
88  case SUMO_TAG_PARAM:
89  if (myLastParameterised != nullptr) {
90  bool ok = true;
91  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
92  // circumventing empty string test
93  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
95  }
96  default:
97  break;
98  }
99 }
100 
101 
102 void
104  switch (element) {
105  case SUMO_TAG_NODE:
106  myLastParameterised = nullptr;
107  break;
108  default:
109  break;
110  }
111 }
112 
113 
114 void
116  bool ok = true;
117  // get the id, report a warning if not given or empty...
118  myID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
119  if (!ok) {
120  return;
121  }
122  NBNode* node = myNodeCont.retrieve(myID);
123  // retrieve the position of the node
124  bool xOk = false;
125  bool yOk = false;
126  bool needConversion = true;
127  if (node != nullptr) {
128  myPosition = node->getPosition();
129  xOk = yOk = true;
130  needConversion = false;
131  } else {
132  myPosition.set(0, 0, 0); // better to reset than to reuse the previous (z)-value
133  }
134  if (attrs.hasAttribute(SUMO_ATTR_X)) {
135  myPosition.set(attrs.get<double>(SUMO_ATTR_X, myID.c_str(), ok), myPosition.y());
136  xOk = true;
137  needConversion = true;
138  }
139  if (attrs.hasAttribute(SUMO_ATTR_Y)) {
140  myPosition.set(myPosition.x(), attrs.get<double>(SUMO_ATTR_Y, myID.c_str(), ok));
141  yOk = true;
142  needConversion = true;
143  }
144  if (attrs.hasAttribute(SUMO_ATTR_Z)) {
145  myPosition.set(myPosition.x(), myPosition.y(), attrs.get<double>(SUMO_ATTR_Z, myID.c_str(), ok));
146  }
147  if (xOk && yOk) {
148  if (needConversion && !NBNetBuilder::transformCoordinate(myPosition, true, myLocation)) {
149  WRITE_ERROR("Unable to project coordinates for node '" + myID + "'.");
150  }
151  } else {
152  WRITE_ERROR("Missing position (at node ID='" + myID + "').");
153  }
154  bool updateEdgeGeometries = node != nullptr && myPosition != node->getPosition();
155  // check whether the y-axis shall be flipped
156  if (myOptions.getBool("flip-y-axis")) {
157  myPosition.mul(1.0, -1.0);
158  }
159  node = processNodeType(attrs, node, myID, myPosition, updateEdgeGeometries, myNodeCont, myTLLogicCont);
160  myLastParameterised = node;
161 }
162 
163 
164 NBNode*
165 NIXMLNodesHandler::processNodeType(const SUMOSAXAttributes& attrs, NBNode* node, const std::string& nodeID, const Position& position,
166  bool updateEdgeGeometries,
168  bool ok = true;
169  // get the type
171  if (node != nullptr) {
172  type = node->getType();
173  }
174  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TYPE, nodeID.c_str(), ok, "");
175  if (SUMOXMLDefinitions::NodeTypes.hasString(typeS)) {
176  type = SUMOXMLDefinitions::NodeTypes.get(typeS);
177  if (type == NODETYPE_DEAD_END_DEPRECATED || type == NODETYPE_DEAD_END) {
178  // dead end is a computed status. Reset this to unknown so it will
179  // be corrected if additional connections are loaded
180  type = NODETYPE_UNKNOWN;
181  }
182  }
183  std::set<NBTrafficLightDefinition*> oldTLS;
184  // check whether a prior node shall be modified
185  if (node == nullptr) {
186  node = new NBNode(nodeID, position, type);
187  if (!nc.insert(node)) {
188  throw ProcessError("Could not insert node though checked this before (id='" + nodeID + "').");
189  }
190  } else {
191  // patch information
192  oldTLS = node->getControllingTLS();
193  node->reinit(position, type, updateEdgeGeometries);
194  }
195  // process traffic light definition
196  if (NBNode::isTrafficLight(type)) {
197  processTrafficLightDefinitions(attrs, node, tlc);
198  }
199  // remove previously set tls if this node is not controlled by them
200  for (std::set<NBTrafficLightDefinition*>::iterator i = oldTLS.begin(); i != oldTLS.end(); ++i) {
201  if ((*i)->getNodes().size() == 0) {
202  tlc.removeFully((*i)->getID());
203  }
204  }
205 
206  // set optional shape
207  PositionVector shape;
208  if (attrs.hasAttribute(SUMO_ATTR_SHAPE)) {
209  shape = attrs.getOpt<PositionVector>(SUMO_ATTR_SHAPE, nodeID.c_str(), ok, PositionVector());
210  if (shape.size() > 2) {
211  shape.closePolygon();
212  }
213  node->setCustomShape(shape);
214  }
215  // set optional radius
216  if (attrs.hasAttribute(SUMO_ATTR_RADIUS)) {
217  node->setRadius(attrs.get<double>(SUMO_ATTR_RADIUS, nodeID.c_str(), ok));
218  }
219  // set optional keepClear flag
220  if (attrs.hasAttribute(SUMO_ATTR_KEEP_CLEAR)) {
221  node->setKeepClear(attrs.get<bool>(SUMO_ATTR_KEEP_CLEAR, nodeID.c_str(), ok));
222  }
223 
224  // set optional right-of-way hint
226  node->setRightOfWay(attrs.getRightOfWay(ok));
227  }
228  return node;
229 }
230 
231 
232 void
234  bool ok = true;
235  // get the id, report a warning if not given or empty...
236  myID = attrs.get<std::string>(SUMO_ATTR_ID, nullptr, ok);
237  if (!ok) {
238  return;
239  }
240  NBNode* node = myNodeCont.retrieve(myID);
241  if (node == nullptr) {
242  WRITE_WARNING("Ignoring tag '" + toString(SUMO_TAG_DELETE) + "' for unknown node '" +
243  myID + "'");
244  return;
245  } else {
246  myNodeCont.extract(node, true);
247  }
248 }
249 
250 
251 void
253  bool ok = true;
254  const std::string clusterString = attrs.get<std::string>(SUMO_ATTR_NODES, nullptr, ok);
255  const std::vector<std::string> ids = StringTokenizer(clusterString).getVector();
256  if (ok) {
257  myNodeCont.addCluster2Join(std::set<std::string>(ids.begin(), ids.end()));
258  }
259 }
260 
261 
262 void
264  bool ok = true;
265  const std::vector<std::string> ids = StringTokenizer(
266  attrs.get<std::string>(SUMO_ATTR_NODES, nullptr, ok)).getVector();
267  if (ok) {
269  }
270 }
271 
272 
273 void
275  NBNode* currentNode, NBTrafficLightLogicCont& tlc) {
276  // try to get the tl-id
277  // if a tl-id is given, we will look whether this tl already exists
278  // if so, we will add the node to it (and to all programs with this id), otherwise allocate a new one with this id
279  // if no tl-id exists, we will build a tl with the node's id
280  std::set<NBTrafficLightDefinition*> tlDefs;
281  bool ok = true;
282 
283  std::string oldTlID = "";
284  std::string oldTypeS = OptionsCont::getOptions().getString("tls.default-type");
285 
286  if (currentNode->isTLControlled()) {
287  NBTrafficLightDefinition* oldDef = *(currentNode->getControllingTLS().begin());
288  oldTlID = oldDef->getID();
289  oldTypeS = toString(oldDef->getType());
290  }
291  std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, nullptr, ok, oldTlID);
292  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TLTYPE, nullptr, ok, oldTypeS);
293  if (tlID != oldTlID || typeS != oldTypeS) {
294  currentNode->removeTrafficLights();
295  }
296  TrafficLightType type;
297  if (SUMOXMLDefinitions::TrafficLightTypes.hasString(typeS)) {
299  } else {
300  WRITE_ERROR("Unknown traffic light type '" + typeS + "' for node '" + currentNode->getID() + "'.");
301  return;
302  }
303  if (tlID != "" && tlc.getPrograms(tlID).size() > 0) {
304  // we already have definitions for this tlID
305  const std::map<std::string, NBTrafficLightDefinition*>& programs = tlc.getPrograms(tlID);
306  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
307  for (it = programs.begin(); it != programs.end(); it++) {
308  if (it->second->getType() != type) {
309  WRITE_ERROR("Mismatched traffic light type '" + typeS + "' for tl '" + tlID + "'.");
310  ok = false;
311  } else {
312  tlDefs.insert(it->second);
313  it->second->addNode(currentNode);
314  }
315  }
316  } else {
317  // we need to add a new defition
318  tlID = (tlID == "" ? currentNode->getID() : tlID);
319  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(tlID, currentNode, 0, type);
320  if (!tlc.insert(tlDef)) {
321  // actually, nothing should fail here
322  delete tlDef;
323  throw ProcessError("Could not allocate tls '" + currentNode->getID() + "'.");
324  }
325  tlDefs.insert(tlDef);
326  }
327  // process inner edges which shall be controlled
328  std::vector<std::string> controlledInner;
329  SUMOSAXAttributes::parseStringVector(attrs.getOpt<std::string>(SUMO_ATTR_CONTROLLED_INNER, nullptr, ok, ""), controlledInner);
330  if (controlledInner.size() != 0) {
331  for (std::set<NBTrafficLightDefinition*>::iterator it = tlDefs.begin(); it != tlDefs.end(); it++) {
332  (*it)->addControlledInnerEdges(controlledInner);
333  }
334  }
335 }
336 
337 
338 
339 /****************************************************************************/
340 
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:108
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:565
void setRightOfWay(RightOfWay rightOfWay)
set method for computing right-of-way
Definition: NBNode.h:506
static NBNode * 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)
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:286
double y() const
Returns the y-position.
Definition: Position.h:62
TrafficLightType getType() const
get the algorithm type (static etc..)
double x() const
Returns the x-position.
Definition: Position.h:57
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:78
void set(double x, double y)
set positions x and y
Definition: Position.h:87
void setCustomShape(const PositionVector &shape)
set the junction shape
Definition: NBNode.cpp:1985
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:241
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
NBNodeCont & myNodeCont
The node container to add built nodes to.
virtual RightOfWay getRightOfWay(bool &ok) const =0
Returns the right-of-way method.
void setRadius(double radius)
set the turning radius
Definition: NBNode.h:496
bool isTLControlled() const
Returns whether this node is controlled by any tls.
Definition: NBNode.h:303
static void parseStringVector(const std::string &def, std::vector< std::string > &into)
Splits the given string.
How to compute right of way.
The turning radius at an intersection in m.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
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:39
parameter associated to a certain key
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:354
Parameterised * myLastParameterised
last item the could receive parameters
void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
node: the type of traffic light
edge: the shape in xml-definition
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:501
void addJoinExclusion(const SUMOSAXAttributes &attrs)
std::vector< std::string > getVector()
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:247
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:581
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:308
void addJoinCluster(const SUMOSAXAttributes &attrs)
Join operation.
alternative definition for junction
A storage for options typed value containers)
Definition: OptionsCont.h:92
SumoXMLNodeType getType() const
Returns the type of this node.
Definition: NBNode.h:267
bool insert(const std::string &id, const Position &position, NBDistrict *district=0)
Inserts a node into the map.
Definition: NBNodeCont.cpp:79
const Position & getPosition() const
Definition: NBNode.h:242
Represents a single node (junction) during network building.
Definition: NBNode.h:68
bool removeFully(const std::string id)
Removes a logic definition (and all programs) from the dictionary.
void myEndElement(int element)
Called when a closing tag occurs.
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:60
void mul(double val)
Multiplies both positions with the given value.
Definition: Position.h:107
A traffic light logics which must be computed (only nodes/edges are given)
Definition: NBOwnTLDef.h:47
delete certain element
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
static bool isTrafficLight(SumoXMLNodeType type)
return whether the given type is a traffic light
Definition: NBNode.cpp:3102
join exlude operation
TrafficLightType
NIXMLNodesHandler(NBNodeCont &nc, NBTrafficLightLogicCont &tlc, OptionsCont &options)
Constructor.