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  default:
106  break;
107  }
108 }
109 
110 
111 void
113  switch (element) {
114  case SUMO_TAG_NET:
115  // build junction graph
116  for (JunctionGraph::iterator it = myJunctionGraph.begin(); it != myJunctionGraph.end(); ++it) {
117  ROEdge* edge = myNet.getEdge(it->first);
118  RONode* from = myNet.getNode(it->second.first);
119  RONode* to = myNet.getNode(it->second.second);
120  if (edge != 0 && from != 0 && to != 0) {
121  edge->setJunctions(from, to);
122  from->addOutgoing(edge);
123  to->addIncoming(edge);
124  }
125  }
126  break;
127  default:
128  break;
129  }
130 }
131 
132 
133 void
135  // get the id, report an error if not given or empty...
136  bool ok = true;
137  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
138  if (!ok) {
139  throw ProcessError();
140  }
141  const SumoXMLEdgeFunc type = attrs.getEdgeFunc(ok);
142  if (!ok) {
143  WRITE_ERROR("Edge '" + myCurrentName + "' has an unknown type.");
144  return;
145  }
146  // get the edge
147  std::string from;
148  std::string to;
149  RONode* fromNode;
150  RONode* toNode;
151  int priority;
152  myCurrentEdge = 0;
153  if (type == EDGEFUNC_INTERNAL || type == EDGEFUNC_CROSSING || type == EDGEFUNC_WALKINGAREA) {
154  assert(myCurrentName[0] == ':');
155  std::string junctionID = myCurrentName.substr(1, myCurrentName.rfind('_') - 1);
156  myJunctionGraph[myCurrentName] = std::make_pair(junctionID, junctionID);
157  from = junctionID;
158  to = junctionID;
159  priority = 0;
160  } else {
161  from = attrs.get<std::string>(SUMO_ATTR_FROM, myCurrentName.c_str(), ok);
162  to = attrs.get<std::string>(SUMO_ATTR_TO, myCurrentName.c_str(), ok);
163  priority = attrs.get<int>(SUMO_ATTR_PRIORITY, myCurrentName.c_str(), ok);
164  if (!ok) {
165  return;
166  }
167  }
168  myJunctionGraph[myCurrentName] = std::make_pair(from, to);
169  fromNode = myNet.getNode(from);
170  if (fromNode == 0) {
171  fromNode = new RONode(from);
172  myNet.addNode(fromNode);
173  }
174  toNode = myNet.getNode(to);
175  if (toNode == 0) {
176  toNode = new RONode(to);
177  myNet.addNode(toNode);
178  }
179  // build the edge
180  myCurrentEdge = myEdgeBuilder.buildEdge(myCurrentName, fromNode, toNode, priority);
181  // set the type
182  myProcess = true;
183  switch (type) {
184  case EDGEFUNC_CONNECTOR:
185  case EDGEFUNC_NORMAL:
187  break;
188  case EDGEFUNC_SOURCE:
190  break;
191  case EDGEFUNC_SINK:
193  break;
196  break;
197  case EDGEFUNC_CROSSING:
199  break;
200  case EDGEFUNC_INTERNAL:
202  myProcess = false;
203  break;
204  default:
205  throw ProcessError("Unhandled EdgeFunk " + toString(type));
206  }
207 
208  if (!myNet.addEdge(myCurrentEdge)) {
209  myCurrentEdge = 0;
210  }
211 }
212 
213 
214 void
216  if (myCurrentEdge == 0) {
217  // was an internal edge to skip or an error occured
218  return;
219  }
220  bool ok = true;
221  // get the id, report an error if not given or empty...
222  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
223  if (!ok) {
224  return;
225  }
226  // get the speed
227  SUMOReal maxSpeed = attrs.get<SUMOReal>(SUMO_ATTR_SPEED, id.c_str(), ok);
228  SUMOReal length = attrs.get<SUMOReal>(SUMO_ATTR_LENGTH, id.c_str(), ok);
229  std::string allow = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, id.c_str(), ok, "");
230  std::string disallow = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, id.c_str(), ok, "");
231  if (!ok) {
232  return;
233  }
234  // get the length
235  // get the vehicle classes
236  SVCPermissions permissions = parseVehicleClasses(allow, disallow);
237  if (permissions != SVCAll) {
239  }
240  // add when both values are valid
241  if (maxSpeed > 0 && length > 0 && id.length() > 0) {
242  myCurrentEdge->addLane(new ROLane(id, myCurrentEdge, length, maxSpeed, permissions));
243  }
244 }
245 
246 
247 void
249  bool ok = true;
250  // get the id, report an error if not given or empty...
251  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
252  if (!ok) {
253  return;
254  }
255  // get the position of the node
256  SUMOReal x = attrs.get<SUMOReal>(SUMO_ATTR_X, id.c_str(), ok);
257  SUMOReal y = attrs.get<SUMOReal>(SUMO_ATTR_Y, id.c_str(), ok);
258  if (ok) {
259  RONode* n = myNet.getNode(id);
260  if (n == 0) {
261  n = new RONode(id);
262  myNet.addNode(n);
263  }
264  n->setPosition(Position(x, y));
265  } else {
266  throw ProcessError();
267  }
268 }
269 
270 
271 void
273  bool ok = true;
274  std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, 0, ok);
275  std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, 0, ok);
276  int fromLane = attrs.get<int>(SUMO_ATTR_FROM_LANE, 0, ok);
277  int toLane = attrs.get<int>(SUMO_ATTR_TO_LANE, 0, ok);
278  std::string dir = attrs.get<std::string>(SUMO_ATTR_DIR, 0, ok);
279  ROEdge* from = myNet.getEdge(fromID);
280  ROEdge* to = myNet.getEdge(toID);
281  if (from == 0) {
282  throw ProcessError("unknown from-edge '" + fromID + "' in connection");
283  }
284  if (to == 0) {
285  throw ProcessError("unknown to-edge '" + toID + "' in connection");
286  }
287  if (from->getType() == ROEdge::ET_INTERNAL) { // skip inner lane connections
288  return;
289  }
290  if (from->getLanes().size() <= (size_t)fromLane) {
291  throw ProcessError("invalid fromLane '" + toString(fromLane) + "' in connection from '" + fromID + "'.");
292  }
293  if (to->getLanes().size() <= (size_t)toLane) {
294  throw ProcessError("invalid toLane '" + toString(toLane) + "' in connection to '" + toID + "'.");
295  }
296  from->getLanes()[fromLane]->addOutgoingLane(to->getLanes()[toLane]);
297  from->addSuccessor(to, dir);
298 }
299 
300 
301 void
303  bool ok = true;
305  // get the id, throw if not given or empty...
306  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "busStop", ok);
307  // get the lane
308  stop->lane = attrs.get<std::string>(SUMO_ATTR_LANE, "busStop", ok);
309  if (!ok) {
310  throw ProcessError();
311  }
312  const ROEdge* edge = myNet.getEdge(stop->lane.substr(0, stop->lane.rfind("_")));
313  if (edge == 0) {
314  throw InvalidArgument("Unknown lane '" + stop->lane + "' for bus stop '" + id + "'.");
315  }
316  // get the positions
317  stop->startPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0);
318  stop->endPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_ENDPOS, id.c_str(), ok, edge->getLength());
319  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
320  if (!ok || !SUMORouteHandler::checkStopPos(stop->startPos, stop->endPos, edge->getLength(), POSITION_EPS, friendlyPos)) {
321  throw InvalidArgument("Invalid position for bus stop '" + id + "'.");
322  }
323  myNet.addBusStop(id, stop);
324 }
325 
326 
327 void
329  bool ok = true;
331  // get the id, throw if not given or empty...
332  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "containerStop", ok);
333  // get the lane
334  stop->lane = attrs.get<std::string>(SUMO_ATTR_LANE, "containerStop", ok);
335  if (!ok) {
336  throw ProcessError();
337  }
338  const ROEdge* edge = myNet.getEdge(stop->lane.substr(0, stop->lane.rfind("_")));
339  if (edge == 0) {
340  throw InvalidArgument("Unknown lane '" + stop->lane + "' for container stop '" + id + "'.");
341  }
342  // get the positions
343  stop->startPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0);
344  stop->endPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_ENDPOS, id.c_str(), ok, edge->getLength());
345  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
346  if (!ok || !SUMORouteHandler::checkStopPos(stop->startPos, stop->endPos, edge->getLength(), POSITION_EPS, friendlyPos)) {
347  throw InvalidArgument("Invalid position for container stop '" + id + "'.");
348  }
349  myNet.addContainerStop(id, stop);
350 }
351 
352 
353 void
355  myCurrentEdge = 0;
356  bool ok = true;
357  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
358  if (!ok) {
359  return;
360  }
362  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
363  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
364  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
366  myNet.addDistrictEdge(myCurrentName, *i, false);
367  }
368  }
369 }
370 
371 
372 void
374  bool ok = true;
375  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, myCurrentName.c_str(), ok);
376  myNet.addDistrictEdge(myCurrentName, id, isSource);
377 }
378 
379 
380 
381 /****************************************************************************/
382 
bool addDistrictEdge(const std::string tazID, const std::string edgeID, const bool isSource)
Definition: RONet.cpp:113
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:199
A single lane the router may use.
Definition: ROLane.h:52
void parseDistrict(const SUMOSAXAttributes &attrs)
void setJunctions(RONode *from, RONode *to)
Definition: ROEdge.h:429
void addNode(RONode *node)
Definition: RONet.cpp:135
int SVCPermissions
ROEdge * getEdge(const std::string &name) const
Retrieves an edge from the network.
Definition: RONet.h:138
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:90
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.
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:438
virtual SumoXMLEdgeFunc getEdgeFunc(bool &ok) const =0
Returns the value of the named attribute.
JunctionGraph myJunctionGraph
Definition: RONetHandler.h:203
An internal edge which models walking areas for pedestrians.
Definition: ROEdge.h:89
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
An edge where vehicles are inserted at (no vehicle may come from back)
Definition: ROEdge.h:85
RONetHandler(RONet &net, ROAbstractEdgeBuilder &eb)
Constructor.
void setType(EdgeType type)
Sets the type of te edge.
Definition: ROEdge.cpp:225
SUMOReal startPos
The stopping position start.
the edges of a route
Encapsulated SAX-Attributes.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:46
SUMOReal endPos
The stopping position end.
#define POSITION_EPS
Definition: config.h:189
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.
#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's network representation.
Definition: RONet.h:72
bool addDistrict(const std::string id, ROEdge *source, ROEdge *sink)
Definition: RONet.cpp:96
void setRestrictionFound()
Definition: RONet.cpp:553
SUMOReal getLength() const
Returns the length of the edge.
Definition: ROEdge.h:183
A normal edge.
Definition: ROEdge.h:81
Definition of vehicle stop (position and duration)
An internal edge which models pedestrian crossings.
Definition: ROEdge.h:91
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:144
RONode * getNode(const std::string &id) const
Retrieves an node from the network.
Definition: RONet.h:160
The abstract direction of a link.
virtual bool addEdge(ROEdge *edge)
Definition: RONet.cpp:82
RONet & myNet
The net to store the information into.
Definition: RONetHandler.h:187
void parseConnection(const SUMOSAXAttributes &attrs)
#define SUMOReal
Definition: config.h:218
virtual void myEndElement(int element)
Called when a closing tag occurs.
Base class for nodes used by the router.
Definition: RONode.h:53
An edge where vehicles disappear (no vehicle may leave this edge)
Definition: ROEdge.h:87
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:196
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
void addContainerStop(const std::string &id, SUMOVehicleParameter::Stop *stop)
Definition: RONet.cpp:155
ROEdge * myCurrentEdge
The currently built edge.
Definition: RONetHandler.h:193
void parseDistrictEdge(const SUMOSAXAttributes &attrs, bool isSource)
void parseEdge(const SUMOSAXAttributes &attrs)
Parses and builds an edge.
void parseContainerStop(const SUMOSAXAttributes &attrs)