SUMO - Simulation of Urban MObility
RONetHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
21 // The handler for SUMO-Networks
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>
39 #include <utils/common/ToString.h>
43 #include "ROEdge.h"
44 #include "ROLane.h"
45 #include "RONode.h"
46 #include "RONet.h"
47 #include "RONetHandler.h"
48 #include "ROAbstractEdgeBuilder.h"
49 
50 
51 // ===========================================================================
52 // method definitions
53 // ===========================================================================
56  : SUMOSAXHandler("sumo-network"),
57  myNet(net), myCurrentName(),
58  myCurrentEdge(0), myCurrentStoppingPlace(0),
59  myEdgeBuilder(eb) {}
60 
61 
63 
64 
65 void
67  const SUMOSAXAttributes& attrs) {
68  switch (element) {
69  case SUMO_TAG_EDGE:
70  // in the first step, we do need the name to allocate the edge
71  // in the second, we need it to know to which edge we have to add
72  // the following edges to
73  parseEdge(attrs);
74  break;
75  case SUMO_TAG_LANE:
76  parseLane(attrs);
77  break;
78  case SUMO_TAG_JUNCTION:
79  parseJunction(attrs);
80  break;
82  parseConnection(attrs);
83  break;
84  case SUMO_TAG_BUS_STOP:
88  parseStoppingPlace(attrs, (SumoXMLTag)element);
89  break;
90  case SUMO_TAG_ACCESS:
91  parseAccess(attrs);
92  break;
93  case SUMO_TAG_TAZ:
94  parseDistrict(attrs);
95  break;
96  case SUMO_TAG_TAZSOURCE:
97  parseDistrictEdge(attrs, true);
98  break;
99  case SUMO_TAG_TAZSINK:
100  parseDistrictEdge(attrs, false);
101  break;
102  case SUMO_TAG_TYPE: {
103  bool ok = true;
104  myCurrentTypeID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
105  break;
106  }
107  case SUMO_TAG_RESTRICTION: {
108  bool ok = true;
109  const SUMOVehicleClass svc = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_VCLASS, myCurrentTypeID.c_str(), ok));
110  const double speed = attrs.get<double>(SUMO_ATTR_SPEED, myCurrentTypeID.c_str(), ok);
111  if (ok) {
112  myNet.addRestriction(myCurrentTypeID, svc, speed);
113  }
114  break;
115  }
116  default:
117  break;
118  }
119 }
120 
121 
122 void
124  switch (element) {
125  case SUMO_TAG_NET:
126  // build junction graph
127  for (std::set<std::string>::const_iterator it = myUnseenNodeIDs.begin(); it != myUnseenNodeIDs.end(); ++it) {
128  WRITE_ERROR("Unknown node '" + *it + "'.");
129  }
130  break;
131  default:
132  break;
133  }
134 }
135 
136 
137 void
139  // get the id, report an error if not given or empty...
140  bool ok = true;
141  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
142  if (!ok) {
143  throw ProcessError();
144  }
145  const SumoXMLEdgeFunc func = attrs.getEdgeFunc(ok);
146  if (!ok) {
147  WRITE_ERROR("Edge '" + myCurrentName + "' has an unknown type.");
148  return;
149  }
150  // get the edge
151  std::string from;
152  std::string to;
153  int priority;
154  myCurrentEdge = 0;
155  if (func == EDGEFUNC_INTERNAL || func == EDGEFUNC_CROSSING || func == EDGEFUNC_WALKINGAREA) {
156  assert(myCurrentName[0] == ':');
157  const std::string junctionID = myCurrentName.substr(1, myCurrentName.rfind('_') - 1);
158  from = junctionID;
159  to = junctionID;
160  priority = 0;
161  } else {
162  from = attrs.get<std::string>(SUMO_ATTR_FROM, myCurrentName.c_str(), ok);
163  to = attrs.get<std::string>(SUMO_ATTR_TO, myCurrentName.c_str(), ok);
164  priority = attrs.get<int>(SUMO_ATTR_PRIORITY, myCurrentName.c_str(), ok);
165  if (!ok) {
166  return;
167  }
168  }
169  RONode* fromNode = myNet.getNode(from);
170  if (fromNode == 0) {
171  myUnseenNodeIDs.insert(from);
172  fromNode = new RONode(from);
173  myNet.addNode(fromNode);
174  }
175  RONode* toNode = myNet.getNode(to);
176  if (toNode == 0) {
177  myUnseenNodeIDs.insert(to);
178  toNode = new RONode(to);
179  myNet.addNode(toNode);
180  }
181  // build the edge
182  myCurrentEdge = myEdgeBuilder.buildEdge(myCurrentName, fromNode, toNode, priority);
183  // set the type
184  myCurrentEdge->setRestrictions(myNet.getRestrictions(attrs.getOpt<std::string>(SUMO_ATTR_TYPE, myCurrentName.c_str(), ok, "")));
185  myCurrentEdge->setFunction(func);
186 
187  if (myNet.addEdge(myCurrentEdge)) {
188  fromNode->addOutgoing(myCurrentEdge);
189  toNode->addIncoming(myCurrentEdge);
190  } else {
191  myCurrentEdge = 0;
192  }
193 }
194 
195 
196 void
198  if (myCurrentEdge == 0) {
199  // was an internal edge to skip or an error occured
200  return;
201  }
202  bool ok = true;
203  // get the id, report an error if not given or empty...
204  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
205  if (!ok) {
206  return;
207  }
208  // get the speed
209  double maxSpeed = attrs.get<double>(SUMO_ATTR_SPEED, id.c_str(), ok);
210  double length = attrs.get<double>(SUMO_ATTR_LENGTH, id.c_str(), ok);
211  std::string allow = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, id.c_str(), ok, "");
212  std::string disallow = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, id.c_str(), ok, "");
213  if (!ok) {
214  return;
215  }
216  // get the length
217  // get the vehicle classes
218  SVCPermissions permissions = parseVehicleClasses(allow, disallow);
219  if (permissions != SVCAll) {
221  }
222  // add when both values are valid
223  if (maxSpeed > 0 && length > 0 && id.length() > 0) {
224  myCurrentEdge->addLane(new ROLane(id, myCurrentEdge, length, maxSpeed, permissions));
225  } else {
226  WRITE_WARNING("Ignoring lane '" + id + "' with speed " + toString(maxSpeed) + " and length " + toString(length));
227  }
228 }
229 
230 
231 void
233  bool ok = true;
234  // get the id, report an error if not given or empty...
235  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
236  if (attrs.getNodeType(ok) == NODETYPE_INTERNAL) {
237  return;
238  }
239  myUnseenNodeIDs.erase(id);
240  // get the position of the node
241  const double x = attrs.get<double>(SUMO_ATTR_X, id.c_str(), ok);
242  const double y = attrs.get<double>(SUMO_ATTR_Y, id.c_str(), ok);
243  const double z = attrs.getOpt<double>(SUMO_ATTR_Z, id.c_str(), ok, 0.);
244  if (!ok) {
245  return;
246  }
247  RONode* n = myNet.getNode(id);
248  if (n == 0) {
249  WRITE_WARNING("Skipping isolated junction '" + id + "'.");
250  } else {
251  n->setPosition(Position(x, y, z));
252  }
253 }
254 
255 
256 void
258  bool ok = true;
259  std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, 0, ok);
260  std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, 0, ok);
261  int fromLane = attrs.get<int>(SUMO_ATTR_FROM_LANE, 0, ok);
262  int toLane = attrs.get<int>(SUMO_ATTR_TO_LANE, 0, ok);
263  std::string dir = attrs.get<std::string>(SUMO_ATTR_DIR, 0, ok);
264  std::string viaID = attrs.getOpt<std::string>(SUMO_ATTR_VIA, 0, ok, "");
265  ROEdge* from = myNet.getEdge(fromID);
266  ROEdge* to = myNet.getEdge(toID);
267  if (from == 0) {
268  throw ProcessError("unknown from-edge '" + fromID + "' in connection");
269  }
270  if (to == 0) {
271  throw ProcessError("unknown to-edge '" + toID + "' in connection");
272  }
273  if ((int)from->getLanes().size() <= fromLane) {
274  throw ProcessError("invalid fromLane '" + toString(fromLane) + "' in connection from '" + fromID + "'.");
275  }
276  if ((int)to->getLanes().size() <= toLane) {
277  throw ProcessError("invalid toLane '" + toString(toLane) + "' in connection to '" + toID + "'.");
278  }
279  from->getLanes()[fromLane]->addOutgoingLane(to->getLanes()[toLane]);
280  from->addSuccessor(to, dir);
281  if (viaID != "") {
282  ROEdge* via = myNet.getEdge(viaID.substr(0, viaID.rfind('_')));
283  if (via == 0) {
284  throw ProcessError("unknown via-edge '" + viaID + "' in connection");
285  }
286  from->addSuccessor(via, dir);
287  return;
288  }
289 }
290 
291 
292 void
294  bool ok = true;
296  // get the id, throw if not given or empty...
297  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, toString(element).c_str(), ok);
298  // get the lane
299  myCurrentStoppingPlace->lane = attrs.get<std::string>(SUMO_ATTR_LANE, toString(element).c_str(), ok);
300  if (!ok) {
301  throw ProcessError();
302  }
304  if (edge == 0) {
305  throw InvalidArgument("Unknown lane '" + myCurrentStoppingPlace->lane + "' for " + toString(element) + " '" + id + "'.");
306  }
307  // get the positions
308  myCurrentStoppingPlace->startPos = attrs.getOpt<double>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0.);
309  myCurrentStoppingPlace->endPos = attrs.getOpt<double>(SUMO_ATTR_ENDPOS, id.c_str(), ok, edge->getLength());
310  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
312  throw InvalidArgument("Invalid position for " + toString(element) + " '" + id + "'.");
313  }
315 }
316 
317 
318 void
320  bool ok = true;
321  const std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, "access", ok);
322  const ROEdge* edge = myNet.getEdgeForLaneID(lane);
323  if (edge == 0) {
324  throw InvalidArgument("Unknown lane '" + lane + "' for access.");
325  }
326  double pos = attrs.getOpt<double>(SUMO_ATTR_POSITION, "access", ok, 0.);
327  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, "access", ok, false);
328  if (!ok || !SUMORouteHandler::checkStopPos(pos, pos, edge->getLength(), 0., friendlyPos)) {
329  throw InvalidArgument("Invalid position " + toString(pos) + " for access on lane '" + lane + "'.");
330  }
331  if (!ok) {
332  throw ProcessError();
333  }
334  myCurrentStoppingPlace->accessPos.insert(std::make_pair(lane, pos));
335 }
336 
337 
338 void
340  myCurrentEdge = 0;
341  bool ok = true;
342  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
343  if (!ok) {
344  return;
345  }
347  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
348  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
349  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
351  myNet.addDistrictEdge(myCurrentName, *i, false);
352  }
353  }
354 }
355 
356 
357 void
359  bool ok = true;
360  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, myCurrentName.c_str(), ok);
361  myNet.addDistrictEdge(myCurrentName, id, isSource);
362 }
363 
364 
365 
366 /****************************************************************************/
367 
SUMOVehicleClass getVehicleClassID(const std::string &name)
Returns the class id of the abstract class given by its name.
RONode * getNode(const std::string &id) const
Retrieves an node from the network.
Definition: RONet.h:195
bool addDistrictEdge(const std::string tazID, const std::string edgeID, const bool isSource)
Definition: RONet.cpp:168
std::string myCurrentName
The name of the edge/node that is currently processed.
Definition: RONetHandler.h:190
void addOutgoing(ROEdge *edge)
Definition: RONode.h:90
SumoXMLTag
Numbers representing SUMO-XML - element names.
ROAbstractEdgeBuilder & myEdgeBuilder
The object used to build of edges of the desired type.
Definition: RONetHandler.h:202
a source within a district (connection road)
ROEdge * getEdgeForLaneID(const std::string &laneID) const
Retrieves an edge from the network when the lane id is given.
Definition: RONet.h:173
A single lane the router may use.
Definition: ROLane.h:56
SUMOVehicleParameter::Stop * myCurrentStoppingPlace
The currently built stopping place.
Definition: RONetHandler.h:199
root element of a network file
begin/end of the description of a junction
begin/end of the description of a single lane
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
void parseDistrict(const SUMOSAXAttributes &attrs)
a traffic assignment zone
void addNode(RONode *node)
Definition: RONet.cpp:190
connectio between two lanes
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
void parseJunction(const SUMOSAXAttributes &attrs)
Parses a junction&#39;s position.
virtual void addLane(ROLane *lane)
Adds a lane to the edge while loading.
Definition: ROEdge.cpp:93
const std::map< SUMOVehicleClass, double > * getRestrictions(const std::string &id) const
Returns the restrictions for an edge type If no restrictions are present, 0 is returned.
Definition: RONet.cpp:127
virtual void parseLane(const SUMOSAXAttributes &attrs)
Parses and builds a lane.
double getLength() const
Returns the length of the edge.
Definition: ROEdge.h:204
const SVCPermissions SVCAll
all VClasses are allowed
begin/end of the description of an edge restriction
SAX-handler base for SUMO-files.
Interface for building instances of router-edges.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
std::set< std::string > myUnseenNodeIDs
temporary data for checking node initialisation after network parsing is finished ...
Definition: RONetHandler.h:205
virtual SumoXMLEdgeFunc getEdgeFunc(bool &ok) const =0
Returns the value of the named attribute.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
RONetHandler(RONet &net, ROAbstractEdgeBuilder &eb)
Constructor.
void addRestriction(const std::string &id, const SUMOVehicleClass svc, const double speed)
Adds a restriction for an edge type.
Definition: RONet.cpp:121
the edges of a route
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
double startPos
The stopping position start.
Encapsulated SAX-Attributes.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
std::multimap< std::string, double > accessPos
lanes and positions connected to this stop
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:45
#define POSITION_EPS
Definition: config.h:175
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers Deprecated classes g...
double endPos
The stopping position end.
void setPosition(const Position &p)
Sets the position of the node.
Definition: RONode.cpp:46
virtual ~RONetHandler()
Destructor.
virtual SumoXMLNodeType getNodeType(bool &ok) const =0
Returns the value of the named attribute.
void addIncoming(ROEdge *edge)
Definition: RONode.h:86
A basic edge for routing applications.
Definition: ROEdge.h:77
begin/end of the description of an edge
std::string lane
The lane to stop at.
virtual std::vector< std::string > getStringVector(int attr) const =0
Tries to read given attribute assuming it is a string vector.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:205
virtual ROEdge * buildEdge(const std::string &name, RONode *from, RONode *to, const int priority)=0
Builds an edge with the given name.
static bool checkStopPos(double &startPos, double &endPos, const double laneLength, const double minLength, const bool friendlyPos)
check start and end position of a stop
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.
The router&#39;s network representation.
Definition: RONet.h:74
bool addDistrict(const std::string id, ROEdge *source, ROEdge *sink)
Definition: RONet.cpp:151
A train stop (alias for bus stop)
void addStoppingPlace(const std::string &id, const SumoXMLTag category, SUMOVehicleParameter::Stop *stop)
Definition: RONet.cpp:199
a sink within a district (connection road)
Definition of vehicle stop (position and duration)
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
The abstract direction of a link.
const std::vector< ROLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: ROEdge.h:476
virtual bool addEdge(ROEdge *edge)
Definition: RONet.cpp:137
RONet & myNet
The net to store the information into.
Definition: RONetHandler.h:187
void parseConnection(const SUMOSAXAttributes &attrs)
virtual void myEndElement(int element)
Called when a closing tag occurs.
Base class for nodes used by the router.
Definition: RONode.h:52
std::string myCurrentTypeID
The id of the currently processed edge type.
Definition: RONetHandler.h:193
void parseAccess(const SUMOSAXAttributes &attrs)
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:163
void setRestrictions(const std::map< SUMOVehicleClass, double > *restrictions)
Sets the vehicle class specific speed limits of the edge.
Definition: ROEdge.h:143
An access point for a train stop.
ROEdge * myCurrentEdge
The currently built edge.
Definition: RONetHandler.h:196
void parseDistrictEdge(const SUMOSAXAttributes &attrs, bool isSource)
void parseEdge(const SUMOSAXAttributes &attrs)
Parses and builds an edge.
void setPermissionsFound()
Definition: RONet.cpp:694
void parseStoppingPlace(const SUMOSAXAttributes &attrs, const SumoXMLTag element)
void setFunction(SumoXMLEdgeFunc func)
Sets the function of the edge.
Definition: ROEdge.h:119