SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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-2016 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), myCurrentStoppingPlace(0),
64  myProcess(true), myEdgeBuilder(eb) {}
65 
66 
68 
69 
70 void
72  const SUMOSAXAttributes& attrs) {
73  switch (element) {
74  case SUMO_TAG_EDGE:
75  // in the first step, we do need the name to allocate the edge
76  // in the second, we need it to know to which edge we have to add
77  // the following edges to
78  parseEdge(attrs);
79  break;
80  case SUMO_TAG_LANE:
81  if (myProcess) {
82  parseLane(attrs);
83  }
84  break;
85  case SUMO_TAG_JUNCTION:
86  parseJunction(attrs);
87  break;
89  parseConnection(attrs);
90  break;
91  case SUMO_TAG_BUS_STOP:
94  parseStoppingPlace(attrs, (SumoXMLTag)element);
95  break;
96  case SUMO_TAG_ACCESS:
97  parseAccess(attrs);
98  break;
99  case SUMO_TAG_TAZ:
100  parseDistrict(attrs);
101  break;
102  case SUMO_TAG_TAZSOURCE:
103  parseDistrictEdge(attrs, true);
104  break;
105  case SUMO_TAG_TAZSINK:
106  parseDistrictEdge(attrs, false);
107  break;
108  case SUMO_TAG_TYPE: {
109  bool ok = true;
110  myCurrentTypeID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
111  break;
112  }
113  case SUMO_TAG_RESTRICTION: {
114  bool ok = true;
115  const SUMOVehicleClass svc = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_VCLASS, myCurrentTypeID.c_str(), ok));
116  const SUMOReal speed = attrs.get<SUMOReal>(SUMO_ATTR_SPEED, myCurrentTypeID.c_str(), ok);
117  if (ok) {
118  myNet.addRestriction(myCurrentTypeID, svc, speed);
119  }
120  break;
121  }
122  default:
123  break;
124  }
125 }
126 
127 
128 void
130  switch (element) {
131  case SUMO_TAG_NET:
132  // build junction graph
133  for (JunctionGraph::iterator it = myJunctionGraph.begin(); it != myJunctionGraph.end(); ++it) {
134  ROEdge* edge = myNet.getEdge(it->first);
135  RONode* from = myNet.getNode(it->second.first);
136  RONode* to = myNet.getNode(it->second.second);
137  if (edge != 0 && from != 0 && to != 0) {
138  from->addOutgoing(edge);
139  to->addIncoming(edge);
140  }
141  }
142  break;
143  default:
144  break;
145  }
146 }
147 
148 
149 void
151  // get the id, report an error if not given or empty...
152  bool ok = true;
153  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
154  if (!ok) {
155  throw ProcessError();
156  }
157  const SumoXMLEdgeFunc func = attrs.getEdgeFunc(ok);
158  if (!ok) {
159  WRITE_ERROR("Edge '" + myCurrentName + "' has an unknown type.");
160  return;
161  }
162  // get the edge
163  std::string from;
164  std::string to;
165  RONode* fromNode;
166  RONode* toNode;
167  int priority;
168  myCurrentEdge = 0;
169  if (func == EDGEFUNC_INTERNAL || func == EDGEFUNC_CROSSING || func == EDGEFUNC_WALKINGAREA) {
170  assert(myCurrentName[0] == ':');
171  std::string junctionID = myCurrentName.substr(1, myCurrentName.rfind('_') - 1);
172  myJunctionGraph[myCurrentName] = std::make_pair(junctionID, junctionID);
173  from = junctionID;
174  to = junctionID;
175  priority = 0;
176  } else {
177  from = attrs.get<std::string>(SUMO_ATTR_FROM, myCurrentName.c_str(), ok);
178  to = attrs.get<std::string>(SUMO_ATTR_TO, myCurrentName.c_str(), ok);
179  priority = attrs.get<int>(SUMO_ATTR_PRIORITY, myCurrentName.c_str(), ok);
180  if (!ok) {
181  return;
182  }
183  }
184  myJunctionGraph[myCurrentName] = std::make_pair(from, to);
185  fromNode = myNet.getNode(from);
186  if (fromNode == 0) {
187  fromNode = new RONode(from);
188  myNet.addNode(fromNode);
189  }
190  toNode = myNet.getNode(to);
191  if (toNode == 0) {
192  toNode = new RONode(to);
193  myNet.addNode(toNode);
194  }
195  // build the edge
196  myCurrentEdge = myEdgeBuilder.buildEdge(myCurrentName, fromNode, toNode, priority);
197  // set the type
198  myCurrentEdge->setRestrictions(myNet.getRestrictions(attrs.getOpt<std::string>(SUMO_ATTR_TYPE, myCurrentName.c_str(), ok, "")));
199  myProcess = true;
200  switch (func) {
201  case EDGEFUNC_CONNECTOR:
202  case EDGEFUNC_NORMAL:
204  break;
205  case EDGEFUNC_SOURCE:
207  break;
208  case EDGEFUNC_SINK:
210  break;
213  break;
214  case EDGEFUNC_CROSSING:
216  break;
217  case EDGEFUNC_INTERNAL:
219  myProcess = false;
220  break;
221  default:
222  throw ProcessError("Unhandled EdgeFunc " + toString(func));
223  }
224 
225  if (!myNet.addEdge(myCurrentEdge)) {
226  myCurrentEdge = 0;
227  }
228 }
229 
230 
231 void
233  if (myCurrentEdge == 0) {
234  // was an internal edge to skip or an error occured
235  return;
236  }
237  bool ok = true;
238  // get the id, report an error if not given or empty...
239  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
240  if (!ok) {
241  return;
242  }
243  // get the speed
244  SUMOReal maxSpeed = attrs.get<SUMOReal>(SUMO_ATTR_SPEED, id.c_str(), ok);
245  SUMOReal length = attrs.get<SUMOReal>(SUMO_ATTR_LENGTH, id.c_str(), ok);
246  std::string allow = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, id.c_str(), ok, "");
247  std::string disallow = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, id.c_str(), ok, "");
248  if (!ok) {
249  return;
250  }
251  // get the length
252  // get the vehicle classes
253  SVCPermissions permissions = parseVehicleClasses(allow, disallow);
254  if (permissions != SVCAll) {
256  }
257  // add when both values are valid
258  if (maxSpeed > 0 && length > 0 && id.length() > 0) {
259  myCurrentEdge->addLane(new ROLane(id, myCurrentEdge, length, maxSpeed, permissions));
260  }
261 }
262 
263 
264 void
266  bool ok = true;
267  // get the id, report an error if not given or empty...
268  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
269  if (!ok) {
270  return;
271  }
272  // get the position of the node
273  SUMOReal x = attrs.get<SUMOReal>(SUMO_ATTR_X, id.c_str(), ok);
274  SUMOReal y = attrs.get<SUMOReal>(SUMO_ATTR_Y, id.c_str(), ok);
275  if (ok) {
276  RONode* n = myNet.getNode(id);
277  if (n == 0) {
278  n = new RONode(id);
279  myNet.addNode(n);
280  }
281  n->setPosition(Position(x, y));
282  } else {
283  throw ProcessError();
284  }
285 }
286 
287 
288 void
290  bool ok = true;
291  std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, 0, ok);
292  std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, 0, ok);
293  int fromLane = attrs.get<int>(SUMO_ATTR_FROM_LANE, 0, ok);
294  int toLane = attrs.get<int>(SUMO_ATTR_TO_LANE, 0, ok);
295  std::string dir = attrs.get<std::string>(SUMO_ATTR_DIR, 0, ok);
296  ROEdge* from = myNet.getEdge(fromID);
297  ROEdge* to = myNet.getEdge(toID);
298  if (from == 0) {
299  throw ProcessError("unknown from-edge '" + fromID + "' in connection");
300  }
301  if (to == 0) {
302  throw ProcessError("unknown to-edge '" + toID + "' in connection");
303  }
304  if (from->getFunc() == ROEdge::ET_INTERNAL) { // skip inner lane connections
305  return;
306  }
307  if ((int)from->getLanes().size() <= fromLane) {
308  throw ProcessError("invalid fromLane '" + toString(fromLane) + "' in connection from '" + fromID + "'.");
309  }
310  if ((int)to->getLanes().size() <= toLane) {
311  throw ProcessError("invalid toLane '" + toString(toLane) + "' in connection to '" + toID + "'.");
312  }
313  from->getLanes()[fromLane]->addOutgoingLane(to->getLanes()[toLane]);
314  from->addSuccessor(to, dir);
315 }
316 
317 
318 void
320  bool ok = true;
322  // get the id, throw if not given or empty...
323  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, toString(element).c_str(), ok);
324  // get the lane
325  myCurrentStoppingPlace->lane = attrs.get<std::string>(SUMO_ATTR_LANE, toString(element).c_str(), ok);
326  if (!ok) {
327  throw ProcessError();
328  }
330  if (edge == 0) {
331  throw InvalidArgument("Unknown lane '" + myCurrentStoppingPlace->lane + "' for " + toString(element) + " '" + id + "'.");
332  }
333  // get the positions
334  myCurrentStoppingPlace->startPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0);
335  myCurrentStoppingPlace->endPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_ENDPOS, id.c_str(), ok, edge->getLength());
336  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
338  throw InvalidArgument("Invalid position for " + toString(element) + " '" + id + "'.");
339  }
340  if (element == SUMO_TAG_CONTAINER_STOP) {
342  } else {
344  }
345 }
346 
347 
348 void
350  bool ok = true;
351  const std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, "access", ok);
352  const ROEdge* edge = myNet.getEdgeForLaneID(lane);
353  if (edge == 0) {
354  throw InvalidArgument("Unknown lane '" + lane + "' for access.");
355  }
356  const SUMOReal pos = attrs.getOpt<SUMOReal>(SUMO_ATTR_POSITION, "access", ok, 0);
357  if (!ok) {
358  throw ProcessError();
359  }
360  myCurrentStoppingPlace->accessPos.insert(std::make_pair(lane, pos));
361 }
362 
363 
364 void
366  myCurrentEdge = 0;
367  bool ok = true;
368  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
369  if (!ok) {
370  return;
371  }
373  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
374  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
375  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
377  myNet.addDistrictEdge(myCurrentName, *i, false);
378  }
379  }
380 }
381 
382 
383 void
385  bool ok = true;
386  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, myCurrentName.c_str(), ok);
387  myNet.addDistrictEdge(myCurrentName, id, isSource);
388 }
389 
390 
391 
392 /****************************************************************************/
393 
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:171
std::string myCurrentName
The name of the edge/node that is currently processed.
Definition: RONetHandler.h:191
void addOutgoing(ROEdge *edge)
Definition: RONode.h:91
SumoXMLTag
Numbers representing SUMO-XML - element names.
ROAbstractEdgeBuilder & myEdgeBuilder
The object used to build of edges of the desired type.
Definition: RONetHandler.h:206
void addRestriction(const std::string &id, const SUMOVehicleClass svc, const SUMOReal speed)
Adds a restriction for an edge type.
Definition: RONet.cpp:124
A single lane the router may use.
Definition: ROLane.h:57
SUMOVehicleParameter::Stop * myCurrentStoppingPlace
The currently built stopping place.
Definition: RONetHandler.h:200
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:193
int SVCPermissions
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:165
void parseJunction(const SUMOSAXAttributes &attrs)
Parses a junction'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:96
An internal edge which models vehicles driving across a junction. This is currently not used for rout...
Definition: ROEdge.h:97
virtual void parseLane(const SUMOSAXAttributes &attrs)
Parses and builds a lane.
void setFunc(EdgeFunc func)
Sets the function of the edge.
Definition: ROEdge.h:141
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's lanes.
Definition: ROEdge.h:449
virtual SumoXMLEdgeFunc getEdgeFunc(bool &ok) const =0
Returns the value of the named attribute.
JunctionGraph myJunctionGraph
Definition: RONetHandler.h:210
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:93
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:91
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:89
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
virtual ~RONetHandler()
Destructor.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:55
void addIncoming(ROEdge *edge)
Definition: RONode.h:87
A basic edge for routing applications.
Definition: ROEdge.h:77
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:149
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
ROEdge * getEdgeForLaneID(const std::string &laneID) const
Retrieves an edge from the network when the lane id is given.
Definition: RONet.h:175
virtual ROEdge * buildEdge(const std::string &name, RONode *from, RONode *to, const int priority)=0
Builds an edge with the given name.
The router's network representation.
Definition: RONet.h:76
bool addDistrict(const std::string id, ROEdge *source, ROEdge *sink)
Definition: RONet.cpp:154
A train stop (alias for bus stop)
An internal edge which models pedestrian crossings.
Definition: ROEdge.h:95
SUMOReal getLength() const
Returns the length of the edge.
Definition: ROEdge.h:198
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:202
RONode * getNode(const std::string &id) const
Retrieves an node from the network.
Definition: RONet.h:197
The abstract direction of a link.
virtual bool addEdge(ROEdge *edge)
Definition: RONet.cpp:140
RONet & myNet
The net to store the information into.
Definition: RONetHandler.h:188
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:194
void parseAccess(const SUMOSAXAttributes &attrs)
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:203
std::multimap< std::string, SUMOReal > accessPos
lanes and positions connected to this stop
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:85
An access point for a train stop.
void addContainerStop(const std::string &id, SUMOVehicleParameter::Stop *stop)
Definition: RONet.cpp:213
ROEdge * myCurrentEdge
The currently built edge.
Definition: RONetHandler.h:197
void parseDistrictEdge(const SUMOSAXAttributes &attrs, bool isSource)
void parseEdge(const SUMOSAXAttributes &attrs)
Parses and builds an edge.
void setPermissionsFound()
Definition: RONet.cpp:682
void parseStoppingPlace(const SUMOSAXAttributes &attrs, const SumoXMLTag element)
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:130