SUMO - Simulation of Urban MObility
RONetHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
11 // The handler for SUMO-Networks
12 /****************************************************************************/
13 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
14 // Copyright (C) 2002-2015 DLR (http://www.dlr.de/) and contributors
15 /****************************************************************************/
16 //
17 // This file is part of SUMO.
18 // SUMO is free software: you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation, either version 3 of the License, or
21 // (at your option) any later version.
22 //
23 /****************************************************************************/
24 
25 
26 // ===========================================================================
27 // included modules
28 // ===========================================================================
29 #ifdef _MSC_VER
30 #include <windows_config.h>
31 #else
32 #include <config.h>
33 #endif
34 
35 #include <string>
40 #include <utils/common/ToString.h>
44 #include "ROEdge.h"
45 #include "ROLane.h"
46 #include "RONode.h"
47 #include "RONet.h"
48 #include "RONetHandler.h"
49 #include "ROAbstractEdgeBuilder.h"
50 
51 #ifdef CHECK_MEMORY_LEAKS
52 #include <foreign/nvwa/debug_new.h>
53 #endif // CHECK_MEMORY_LEAKS
54 
55 
56 // ===========================================================================
57 // method definitions
58 // ===========================================================================
61  : SUMOSAXHandler("sumo-network"),
62  myNet(net), myCurrentName(),
63  myCurrentEdge(0), myProcess(true), myEdgeBuilder(eb) {}
64 
65 
67 
68 
69 void
71  const SUMOSAXAttributes& attrs) {
72  switch (element) {
73  case SUMO_TAG_EDGE:
74  // in the first step, we do need the name to allocate the edge
75  // in the second, we need it to know to which edge we have to add
76  // the following edges to
77  parseEdge(attrs);
78  break;
79  case SUMO_TAG_LANE:
80  if (myProcess) {
81  parseLane(attrs);
82  }
83  break;
84  case SUMO_TAG_JUNCTION:
85  parseJunction(attrs);
86  break;
88  parseConnection(attrs);
89  break;
90  case SUMO_TAG_BUS_STOP:
91  parseBusStop(attrs);
92  break;
94  parseContainerStop(attrs);
95  break;
96  case SUMO_TAG_TAZ:
97  parseDistrict(attrs);
98  break;
99  case SUMO_TAG_TAZSOURCE:
100  parseDistrictEdge(attrs, true);
101  break;
102  case SUMO_TAG_TAZSINK:
103  parseDistrictEdge(attrs, false);
104  break;
105  case SUMO_TAG_TYPE: {
106  bool ok = true;
107  myCurrentTypeID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
108  break;
109  }
110  case SUMO_TAG_RESTRICTION: {
111  bool ok = true;
112  const SUMOVehicleClass svc = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_VCLASS, myCurrentTypeID.c_str(), ok));
113  const SUMOReal speed = attrs.get<SUMOReal>(SUMO_ATTR_SPEED, myCurrentTypeID.c_str(), ok);
114  if (ok) {
115  myNet.addRestriction(myCurrentTypeID, svc, speed);
116  }
117  break;
118  }
119  default:
120  break;
121  }
122 }
123 
124 
125 void
127  switch (element) {
128  case SUMO_TAG_NET:
129  // build junction graph
130  for (JunctionGraph::iterator it = myJunctionGraph.begin(); it != myJunctionGraph.end(); ++it) {
131  ROEdge* edge = myNet.getEdge(it->first);
132  RONode* from = myNet.getNode(it->second.first);
133  RONode* to = myNet.getNode(it->second.second);
134  if (edge != 0 && from != 0 && to != 0) {
135  from->addOutgoing(edge);
136  to->addIncoming(edge);
137  }
138  }
139  break;
140  default:
141  break;
142  }
143 }
144 
145 
146 void
148  // get the id, report an error if not given or empty...
149  bool ok = true;
150  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
151  if (!ok) {
152  throw ProcessError();
153  }
154  const SumoXMLEdgeFunc func = attrs.getEdgeFunc(ok);
155  if (!ok) {
156  WRITE_ERROR("Edge '" + myCurrentName + "' has an unknown type.");
157  return;
158  }
159  // get the edge
160  std::string from;
161  std::string to;
162  RONode* fromNode;
163  RONode* toNode;
164  int priority;
165  myCurrentEdge = 0;
166  if (func == EDGEFUNC_INTERNAL || func == EDGEFUNC_CROSSING || func == EDGEFUNC_WALKINGAREA) {
167  assert(myCurrentName[0] == ':');
168  std::string junctionID = myCurrentName.substr(1, myCurrentName.rfind('_') - 1);
169  myJunctionGraph[myCurrentName] = std::make_pair(junctionID, junctionID);
170  from = junctionID;
171  to = junctionID;
172  priority = 0;
173  } else {
174  from = attrs.get<std::string>(SUMO_ATTR_FROM, myCurrentName.c_str(), ok);
175  to = attrs.get<std::string>(SUMO_ATTR_TO, myCurrentName.c_str(), ok);
176  priority = attrs.get<int>(SUMO_ATTR_PRIORITY, myCurrentName.c_str(), ok);
177  if (!ok) {
178  return;
179  }
180  }
181  myJunctionGraph[myCurrentName] = std::make_pair(from, to);
182  fromNode = myNet.getNode(from);
183  if (fromNode == 0) {
184  fromNode = new RONode(from);
185  myNet.addNode(fromNode);
186  }
187  toNode = myNet.getNode(to);
188  if (toNode == 0) {
189  toNode = new RONode(to);
190  myNet.addNode(toNode);
191  }
192  // build the edge
193  myCurrentEdge = myEdgeBuilder.buildEdge(myCurrentName, fromNode, toNode, priority);
194  // set the type
195  myCurrentEdge->setRestrictions(myNet.getRestrictions(attrs.getOpt<std::string>(SUMO_ATTR_TYPE, myCurrentName.c_str(), ok, "")));
196  myProcess = true;
197  switch (func) {
198  case EDGEFUNC_CONNECTOR:
199  case EDGEFUNC_NORMAL:
201  break;
202  case EDGEFUNC_SOURCE:
204  break;
205  case EDGEFUNC_SINK:
207  break;
210  break;
211  case EDGEFUNC_CROSSING:
213  break;
214  case EDGEFUNC_INTERNAL:
216  myProcess = false;
217  break;
218  default:
219  throw ProcessError("Unhandled EdgeFunc " + toString(func));
220  }
221 
222  if (!myNet.addEdge(myCurrentEdge)) {
223  myCurrentEdge = 0;
224  }
225 }
226 
227 
228 void
230  if (myCurrentEdge == 0) {
231  // was an internal edge to skip or an error occured
232  return;
233  }
234  bool ok = true;
235  // get the id, report an error if not given or empty...
236  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
237  if (!ok) {
238  return;
239  }
240  // get the speed
241  SUMOReal maxSpeed = attrs.get<SUMOReal>(SUMO_ATTR_SPEED, id.c_str(), ok);
242  SUMOReal length = attrs.get<SUMOReal>(SUMO_ATTR_LENGTH, id.c_str(), ok);
243  std::string allow = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, id.c_str(), ok, "");
244  std::string disallow = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, id.c_str(), ok, "");
245  if (!ok) {
246  return;
247  }
248  // get the length
249  // get the vehicle classes
250  SVCPermissions permissions = parseVehicleClasses(allow, disallow);
251  if (permissions != SVCAll) {
253  }
254  // add when both values are valid
255  if (maxSpeed > 0 && length > 0 && id.length() > 0) {
256  myCurrentEdge->addLane(new ROLane(id, myCurrentEdge, length, maxSpeed, permissions));
257  }
258 }
259 
260 
261 void
263  bool ok = true;
264  // get the id, report an error if not given or empty...
265  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
266  if (!ok) {
267  return;
268  }
269  // get the position of the node
270  SUMOReal x = attrs.get<SUMOReal>(SUMO_ATTR_X, id.c_str(), ok);
271  SUMOReal y = attrs.get<SUMOReal>(SUMO_ATTR_Y, id.c_str(), ok);
272  if (ok) {
273  RONode* n = myNet.getNode(id);
274  if (n == 0) {
275  n = new RONode(id);
276  myNet.addNode(n);
277  }
278  n->setPosition(Position(x, y));
279  } else {
280  throw ProcessError();
281  }
282 }
283 
284 
285 void
287  bool ok = true;
288  std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, 0, ok);
289  std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, 0, ok);
290  int fromLane = attrs.get<int>(SUMO_ATTR_FROM_LANE, 0, ok);
291  int toLane = attrs.get<int>(SUMO_ATTR_TO_LANE, 0, ok);
292  std::string dir = attrs.get<std::string>(SUMO_ATTR_DIR, 0, ok);
293  ROEdge* from = myNet.getEdge(fromID);
294  ROEdge* to = myNet.getEdge(toID);
295  if (from == 0) {
296  throw ProcessError("unknown from-edge '" + fromID + "' in connection");
297  }
298  if (to == 0) {
299  throw ProcessError("unknown to-edge '" + toID + "' in connection");
300  }
301  if (from->getFunc() == ROEdge::ET_INTERNAL) { // skip inner lane connections
302  return;
303  }
304  if (from->getLanes().size() <= (size_t)fromLane) {
305  throw ProcessError("invalid fromLane '" + toString(fromLane) + "' in connection from '" + fromID + "'.");
306  }
307  if (to->getLanes().size() <= (size_t)toLane) {
308  throw ProcessError("invalid toLane '" + toString(toLane) + "' in connection to '" + toID + "'.");
309  }
310  from->getLanes()[fromLane]->addOutgoingLane(to->getLanes()[toLane]);
311  from->addSuccessor(to, dir);
312 }
313 
314 
315 void
317  bool ok = true;
319  // get the id, throw if not given or empty...
320  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "busStop", ok);
321  // get the lane
322  stop->lane = attrs.get<std::string>(SUMO_ATTR_LANE, "busStop", ok);
323  if (!ok) {
324  throw ProcessError();
325  }
326  const ROEdge* edge = myNet.getEdge(stop->lane.substr(0, stop->lane.rfind("_")));
327  if (edge == 0) {
328  throw InvalidArgument("Unknown lane '" + stop->lane + "' for bus stop '" + id + "'.");
329  }
330  // get the positions
331  stop->startPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0);
332  stop->endPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_ENDPOS, id.c_str(), ok, edge->getLength());
333  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
334  if (!ok || !SUMORouteHandler::checkStopPos(stop->startPos, stop->endPos, edge->getLength(), POSITION_EPS, friendlyPos)) {
335  throw InvalidArgument("Invalid position for bus stop '" + id + "'.");
336  }
337  myNet.addBusStop(id, stop);
338 }
339 
340 
341 void
343  bool ok = true;
345  // get the id, throw if not given or empty...
346  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "containerStop", ok);
347  // get the lane
348  stop->lane = attrs.get<std::string>(SUMO_ATTR_LANE, "containerStop", ok);
349  if (!ok) {
350  throw ProcessError();
351  }
352  const ROEdge* edge = myNet.getEdge(stop->lane.substr(0, stop->lane.rfind("_")));
353  if (edge == 0) {
354  throw InvalidArgument("Unknown lane '" + stop->lane + "' for container stop '" + id + "'.");
355  }
356  // get the positions
357  stop->startPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0);
358  stop->endPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_ENDPOS, id.c_str(), ok, edge->getLength());
359  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
360  if (!ok || !SUMORouteHandler::checkStopPos(stop->startPos, stop->endPos, edge->getLength(), POSITION_EPS, friendlyPos)) {
361  throw InvalidArgument("Invalid position for container stop '" + id + "'.");
362  }
363  myNet.addContainerStop(id, stop);
364 }
365 
366 
367 void
369  myCurrentEdge = 0;
370  bool ok = true;
371  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
372  if (!ok) {
373  return;
374  }
376  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
377  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
378  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
380  myNet.addDistrictEdge(myCurrentName, *i, false);
381  }
382  }
383 }
384 
385 
386 void
388  bool ok = true;
389  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, myCurrentName.c_str(), ok);
390  myNet.addDistrictEdge(myCurrentName, id, isSource);
391 }
392 
393 
394 
395 /****************************************************************************/
396 
SUMOVehicleClass getVehicleClassID(const std::string &name)
Returns the class id of the abstract class given by its name.
bool addDistrictEdge(const std::string tazID, const std::string edgeID, const bool isSource)
Definition: RONet.cpp:148
std::string myCurrentName
The name of the edge/node that is currently processed.
Definition: RONetHandler.h:190
void addOutgoing(ROEdge *edge)
Definition: RONode.h:91
ROAbstractEdgeBuilder & myEdgeBuilder
The object used to build of edges of the desired type.
Definition: RONetHandler.h:202
void addRestriction(const std::string &id, const SUMOVehicleClass svc, const SUMOReal speed)
Adds a restriction for an edge type.
Definition: RONet.cpp:101
A single lane the router may use.
Definition: ROLane.h:52
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types...
void parseDistrict(const SUMOSAXAttributes &attrs)
void addNode(RONode *node)
Definition: RONet.cpp:170
int SVCPermissions
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:158
void parseJunction(const SUMOSAXAttributes &attrs)
Parses a junction&#39;s position.
static bool checkStopPos(SUMOReal &startPos, SUMOReal &endPos, const SUMOReal laneLength, const SUMOReal minLength, const bool friendlyPos)
check start and end position of a stop
virtual void addLane(ROLane *lane)
Adds a lane to the edge while loading.
Definition: ROEdge.cpp:97
An internal edge which models vehicles driving across a junction. This is currently not used for rout...
Definition: ROEdge.h:93
virtual void parseLane(const SUMOSAXAttributes &attrs)
Parses and builds a lane.
void setFunc(EdgeFunc func)
Sets the function of the edge.
Definition: ROEdge.h:137
const SVCPermissions SVCAll
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...
const std::vector< ROLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: ROEdge.h:434
virtual SumoXMLEdgeFunc getEdgeFunc(bool &ok) const =0
Returns the value of the named attribute.
JunctionGraph myJunctionGraph
Definition: RONetHandler.h:206
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
RONetHandler(RONet &net, ROAbstractEdgeBuilder &eb)
Constructor.
An internal edge which models walking areas for pedestrians.
Definition: ROEdge.h:89
SUMOReal startPos
The stopping position start.
the edges of a route
An edge where vehicles disappear (no vehicle may leave this edge)
Definition: ROEdge.h:87
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
An edge where vehicles are inserted at (no vehicle may come from back)
Definition: ROEdge.h:85
SUMOReal endPos
The stopping position end.
#define POSITION_EPS
Definition: config.h:188
SVCPermissions parseVehicleClasses(const std::string &allowedS)
Parses the given definition of allowed vehicle classes into the given containers Deprecated classes g...
void setPosition(const Position &p)
Sets the position of the node.
Definition: RONode.cpp:51
void parseBusStop(const SUMOSAXAttributes &attrs)
virtual ~RONetHandler()
Destructor.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
void addIncoming(ROEdge *edge)
Definition: RONode.h:87
A basic edge for routing applications.
Definition: ROEdge.h:73
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.
void setRestrictions(const std::map< SUMOVehicleClass, SUMOReal > *restrictions)
Sets the vehicle class specific speed limits of the edge.
Definition: ROEdge.h:145
#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.
The router&#39;s network representation.
Definition: RONet.h:72
bool addDistrict(const std::string id, ROEdge *source, ROEdge *sink)
Definition: RONet.cpp:131
An internal edge which models pedestrian crossings.
Definition: ROEdge.h:91
SUMOReal getLength() const
Returns the length of the edge.
Definition: ROEdge.h:194
Definition of vehicle stop (position and duration)
SumoXMLEdgeFunc
Numbers representing special SUMO-XML-attribute values for representing edge functions used in netbui...
void addBusStop(const std::string &id, SUMOVehicleParameter::Stop *stop)
Definition: RONet.cpp:179
RONode * getNode(const std::string &id) const
Retrieves an node from the network.
Definition: RONet.h:180
The abstract direction of a link.
virtual bool addEdge(ROEdge *edge)
Definition: RONet.cpp:117
RONet & myNet
The net to store the information into.
Definition: RONetHandler.h:187
void parseConnection(const SUMOSAXAttributes &attrs)
#define SUMOReal
Definition: config.h:214
virtual void myEndElement(int element)
Called when a closing tag occurs.
Base class for nodes used by the router.
Definition: RONode.h:53
std::string myCurrentTypeID
The id of the currently processed edge type.
Definition: RONetHandler.h:193
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.
bool myProcess
An indicator whether the next edge shall be read (internal edges are not read by now) ...
Definition: RONetHandler.h:199
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
A normal edge.
Definition: ROEdge.h:81
void addContainerStop(const std::string &id, SUMOVehicleParameter::Stop *stop)
Definition: RONet.cpp:190
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:642
const std::map< SUMOVehicleClass, SUMOReal > * getRestrictions(const std::string &id) const
Returns the restrictions for an edge type If no restrictions are present, 0 is returned.
Definition: RONet.cpp:107
void parseContainerStop(const SUMOSAXAttributes &attrs)