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(0),
60  myLastParameterised(0) {
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 != 0) {
90  bool ok = true;
91  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, 0, 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:
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, 0, 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 != 0) {
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 != 0 && 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 != 0) {
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 == 0) {
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  return node;
224 }
225 
226 
227 void
229  bool ok = true;
230  // get the id, report a warning if not given or empty...
231  myID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
232  if (!ok) {
233  return;
234  }
235  NBNode* node = myNodeCont.retrieve(myID);
236  if (node == 0) {
237  WRITE_WARNING("Ignoring tag '" + toString(SUMO_TAG_DELETE) + "' for unknown node '" +
238  myID + "'");
239  return;
240  } else {
241  myNodeCont.extract(node, true);
242  }
243 }
244 
245 
246 void
248  bool ok = true;
249  const std::string clusterString = attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok);
250  const std::vector<std::string> ids = StringTokenizer(clusterString).getVector();
251  if (ok) {
252  myNodeCont.addCluster2Join(std::set<std::string>(ids.begin(), ids.end()));
253  }
254 }
255 
256 
257 void
259  bool ok = true;
260  const std::vector<std::string> ids = StringTokenizer(
261  attrs.get<std::string>(SUMO_ATTR_NODES, 0, ok)).getVector();
262  if (ok) {
264  }
265 }
266 
267 
268 void
270  NBNode* currentNode, NBTrafficLightLogicCont& tlc) {
271  // try to get the tl-id
272  // if a tl-id is given, we will look whether this tl already exists
273  // if so, we will add the node to it (and to all programs with this id), otherwise allocate a new one with this id
274  // if no tl-id exists, we will build a tl with the node's id
275  std::set<NBTrafficLightDefinition*> tlDefs;
276  bool ok = true;
277 
278  std::string oldTlID = "";
279  std::string oldTypeS = OptionsCont::getOptions().getString("tls.default-type");
280 
281  if (currentNode->isTLControlled()) {
282  NBTrafficLightDefinition* oldDef = *(currentNode->getControllingTLS().begin());
283  oldTlID = oldDef->getID();
284  oldTypeS = toString(oldDef->getType());
285  }
286  std::string tlID = attrs.getOpt<std::string>(SUMO_ATTR_TLID, 0, ok, oldTlID);
287  std::string typeS = attrs.getOpt<std::string>(SUMO_ATTR_TLTYPE, 0, ok, oldTypeS);
288  if (tlID != oldTlID || typeS != oldTypeS) {
289  currentNode->removeTrafficLights();
290  }
291  TrafficLightType type;
292  if (SUMOXMLDefinitions::TrafficLightTypes.hasString(typeS)) {
294  } else {
295  WRITE_ERROR("Unknown traffic light type '" + typeS + "' for node '" + currentNode->getID() + "'.");
296  return;
297  }
298  if (tlID != "" && tlc.getPrograms(tlID).size() > 0) {
299  // we already have definitions for this tlID
300  const std::map<std::string, NBTrafficLightDefinition*>& programs = tlc.getPrograms(tlID);
301  std::map<std::string, NBTrafficLightDefinition*>::const_iterator it;
302  for (it = programs.begin(); it != programs.end(); it++) {
303  if (it->second->getType() != type) {
304  WRITE_ERROR("Mismatched traffic light type '" + typeS + "' for tl '" + tlID + "'.");
305  ok = false;
306  } else {
307  tlDefs.insert(it->second);
308  it->second->addNode(currentNode);
309  }
310  }
311  } else {
312  // we need to add a new defition
313  tlID = (tlID == "" ? currentNode->getID() : tlID);
314  NBTrafficLightDefinition* tlDef = new NBOwnTLDef(tlID, currentNode, 0, type);
315  if (!tlc.insert(tlDef)) {
316  // actually, nothing should fail here
317  delete tlDef;
318  throw ProcessError("Could not allocate tls '" + currentNode->getID() + "'.");
319  }
320  tlDefs.insert(tlDef);
321  }
322  // process inner edges which shall be controlled
323  std::vector<std::string> controlledInner;
324  SUMOSAXAttributes::parseStringVector(attrs.getOpt<std::string>(SUMO_ATTR_CONTROLLED_INNER, 0, ok, ""), controlledInner);
325  if (controlledInner.size() != 0) {
326  for (std::set<NBTrafficLightDefinition*>::iterator it = tlDefs.begin(); it != tlDefs.end(); it++) {
327  (*it)->addControlledInnerEdges(controlledInner);
328  }
329  }
330 }
331 
332 
333 
334 /****************************************************************************/
335 
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:570
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:284
double y() const
Returns the y-position.
Definition: Position.h:61
TrafficLightType getType() const
get the algorithm type (static etc..)
double x() const
Returns the x-position.
Definition: Position.h:56
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:69
void set(double x, double y)
set positions x and y
Definition: Position.h:86
void setCustomShape(const PositionVector &shape)
set the junction shape
Definition: NBNode.cpp:1917
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:242
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.
void setRadius(double radius)
set the turning radius
Definition: NBNode.h:484
bool isTLControlled() const
Returns whether this node is controlled by any tls.
Definition: NBNode.h:297
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: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:352
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:489
void addJoinExclusion(const SUMOSAXAttributes &attrs)
std::vector< std::string > getVector()
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:248
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:586
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:302
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:266
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:241
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:106
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:3017
join exlude operation
TrafficLightType
NIXMLNodesHandler(NBNodeCont &nc, NBTrafficLightLogicCont &tlc, OptionsCont &options)
Constructor.