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-2017 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:
95  parseStoppingPlace(attrs, (SumoXMLTag)element);
96  break;
97  case SUMO_TAG_ACCESS:
98  parseAccess(attrs);
99  break;
100  case SUMO_TAG_TAZ:
101  parseDistrict(attrs);
102  break;
103  case SUMO_TAG_TAZSOURCE:
104  parseDistrictEdge(attrs, true);
105  break;
106  case SUMO_TAG_TAZSINK:
107  parseDistrictEdge(attrs, false);
108  break;
109  case SUMO_TAG_TYPE: {
110  bool ok = true;
111  myCurrentTypeID = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
112  break;
113  }
114  case SUMO_TAG_RESTRICTION: {
115  bool ok = true;
116  const SUMOVehicleClass svc = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_VCLASS, myCurrentTypeID.c_str(), ok));
117  const SUMOReal speed = attrs.get<SUMOReal>(SUMO_ATTR_SPEED, myCurrentTypeID.c_str(), ok);
118  if (ok) {
119  myNet.addRestriction(myCurrentTypeID, svc, speed);
120  }
121  break;
122  }
123  default:
124  break;
125  }
126 }
127 
128 
129 void
131  switch (element) {
132  case SUMO_TAG_NET:
133  // build junction graph
134  for (JunctionGraph::iterator it = myJunctionGraph.begin(); it != myJunctionGraph.end(); ++it) {
135  ROEdge* edge = myNet.getEdge(it->first);
136  RONode* from = myNet.getNode(it->second.first);
137  RONode* to = myNet.getNode(it->second.second);
138  if (edge != 0 && from != 0 && to != 0) {
139  from->addOutgoing(edge);
140  to->addIncoming(edge);
141  }
142  }
143  break;
144  default:
145  break;
146  }
147 }
148 
149 
150 void
152  // get the id, report an error if not given or empty...
153  bool ok = true;
154  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
155  if (!ok) {
156  throw ProcessError();
157  }
158  const SumoXMLEdgeFunc func = attrs.getEdgeFunc(ok);
159  if (!ok) {
160  WRITE_ERROR("Edge '" + myCurrentName + "' has an unknown type.");
161  return;
162  }
163  // get the edge
164  std::string from;
165  std::string to;
166  RONode* fromNode;
167  RONode* toNode;
168  int priority;
169  myCurrentEdge = 0;
170  if (func == EDGEFUNC_INTERNAL || func == EDGEFUNC_CROSSING || func == EDGEFUNC_WALKINGAREA) {
171  assert(myCurrentName[0] == ':');
172  std::string junctionID = myCurrentName.substr(1, myCurrentName.rfind('_') - 1);
173  myJunctionGraph[myCurrentName] = std::make_pair(junctionID, junctionID);
174  from = junctionID;
175  to = junctionID;
176  priority = 0;
177  } else {
178  from = attrs.get<std::string>(SUMO_ATTR_FROM, myCurrentName.c_str(), ok);
179  to = attrs.get<std::string>(SUMO_ATTR_TO, myCurrentName.c_str(), ok);
180  priority = attrs.get<int>(SUMO_ATTR_PRIORITY, myCurrentName.c_str(), ok);
181  if (!ok) {
182  return;
183  }
184  }
185  myJunctionGraph[myCurrentName] = std::make_pair(from, to);
186  fromNode = myNet.getNode(from);
187  if (fromNode == 0) {
188  fromNode = new RONode(from);
189  myNet.addNode(fromNode);
190  }
191  toNode = myNet.getNode(to);
192  if (toNode == 0) {
193  toNode = new RONode(to);
194  myNet.addNode(toNode);
195  }
196  // build the edge
197  myCurrentEdge = myEdgeBuilder.buildEdge(myCurrentName, fromNode, toNode, priority);
198  // set the type
199  myCurrentEdge->setRestrictions(myNet.getRestrictions(attrs.getOpt<std::string>(SUMO_ATTR_TYPE, myCurrentName.c_str(), ok, "")));
200  myProcess = true;
201  switch (func) {
202  case EDGEFUNC_CONNECTOR:
203  case EDGEFUNC_NORMAL:
205  break;
206  case EDGEFUNC_SOURCE:
208  break;
209  case EDGEFUNC_SINK:
211  break;
214  break;
215  case EDGEFUNC_CROSSING:
217  break;
218  case EDGEFUNC_INTERNAL:
220  myProcess = false;
221  break;
222  default:
223  throw ProcessError("Unhandled EdgeFunc " + toString(func));
224  }
225 
226  if (!myNet.addEdge(myCurrentEdge)) {
227  myCurrentEdge = 0;
228  }
229 }
230 
231 
232 void
234  if (myCurrentEdge == 0) {
235  // was an internal edge to skip or an error occured
236  return;
237  }
238  bool ok = true;
239  // get the id, report an error if not given or empty...
240  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
241  if (!ok) {
242  return;
243  }
244  // get the speed
245  SUMOReal maxSpeed = attrs.get<SUMOReal>(SUMO_ATTR_SPEED, id.c_str(), ok);
246  SUMOReal length = attrs.get<SUMOReal>(SUMO_ATTR_LENGTH, id.c_str(), ok);
247  std::string allow = attrs.getOpt<std::string>(SUMO_ATTR_ALLOW, id.c_str(), ok, "");
248  std::string disallow = attrs.getOpt<std::string>(SUMO_ATTR_DISALLOW, id.c_str(), ok, "");
249  if (!ok) {
250  return;
251  }
252  // get the length
253  // get the vehicle classes
254  SVCPermissions permissions = parseVehicleClasses(allow, disallow);
255  if (permissions != SVCAll) {
257  }
258  // add when both values are valid
259  if (maxSpeed > 0 && length > 0 && id.length() > 0) {
260  myCurrentEdge->addLane(new ROLane(id, myCurrentEdge, length, maxSpeed, permissions));
261  }
262 }
263 
264 
265 void
267  bool ok = true;
268  // get the id, report an error if not given or empty...
269  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
270  if (!ok) {
271  return;
272  }
273  // get the position of the node
274  SUMOReal x = attrs.get<SUMOReal>(SUMO_ATTR_X, id.c_str(), ok);
275  SUMOReal y = attrs.get<SUMOReal>(SUMO_ATTR_Y, id.c_str(), ok);
276  if (ok) {
277  RONode* n = myNet.getNode(id);
278  if (n == 0) {
279  n = new RONode(id);
280  myNet.addNode(n);
281  }
282  n->setPosition(Position(x, y));
283  } else {
284  throw ProcessError();
285  }
286 }
287 
288 
289 void
291  bool ok = true;
292  std::string fromID = attrs.get<std::string>(SUMO_ATTR_FROM, 0, ok);
293  std::string toID = attrs.get<std::string>(SUMO_ATTR_TO, 0, ok);
294  int fromLane = attrs.get<int>(SUMO_ATTR_FROM_LANE, 0, ok);
295  int toLane = attrs.get<int>(SUMO_ATTR_TO_LANE, 0, ok);
296  std::string dir = attrs.get<std::string>(SUMO_ATTR_DIR, 0, ok);
297  ROEdge* from = myNet.getEdge(fromID);
298  ROEdge* to = myNet.getEdge(toID);
299  if (from == 0) {
300  throw ProcessError("unknown from-edge '" + fromID + "' in connection");
301  }
302  if (to == 0) {
303  throw ProcessError("unknown to-edge '" + toID + "' in connection");
304  }
305  if (from->getFunc() == ROEdge::ET_INTERNAL) { // skip inner lane connections
306  return;
307  }
308  if ((int)from->getLanes().size() <= fromLane) {
309  throw ProcessError("invalid fromLane '" + toString(fromLane) + "' in connection from '" + fromID + "'.");
310  }
311  if ((int)to->getLanes().size() <= toLane) {
312  throw ProcessError("invalid toLane '" + toString(toLane) + "' in connection to '" + toID + "'.");
313  }
314  from->getLanes()[fromLane]->addOutgoingLane(to->getLanes()[toLane]);
315  from->addSuccessor(to, dir);
316 }
317 
318 
319 void
321  bool ok = true;
323  // get the id, throw if not given or empty...
324  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, toString(element).c_str(), ok);
325  // get the lane
326  myCurrentStoppingPlace->lane = attrs.get<std::string>(SUMO_ATTR_LANE, toString(element).c_str(), ok);
327  if (!ok) {
328  throw ProcessError();
329  }
331  if (edge == 0) {
332  throw InvalidArgument("Unknown lane '" + myCurrentStoppingPlace->lane + "' for " + toString(element) + " '" + id + "'.");
333  }
334  // get the positions
335  myCurrentStoppingPlace->startPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0);
336  myCurrentStoppingPlace->endPos = attrs.getOpt<SUMOReal>(SUMO_ATTR_ENDPOS, id.c_str(), ok, edge->getLength());
337  const bool friendlyPos = attrs.getOpt<bool>(SUMO_ATTR_FRIENDLY_POS, id.c_str(), ok, false);
339  throw InvalidArgument("Invalid position for " + toString(element) + " '" + id + "'.");
340  }
341  if (element == SUMO_TAG_CONTAINER_STOP) {
343  } else if (element == SUMO_TAG_PARKING_AREA) {
345  } else {
347  }
348 }
349 
350 
351 void
353  bool ok = true;
354  const std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, "access", ok);
355  const ROEdge* edge = myNet.getEdgeForLaneID(lane);
356  if (edge == 0) {
357  throw InvalidArgument("Unknown lane '" + lane + "' for access.");
358  }
359  const SUMOReal pos = attrs.getOpt<SUMOReal>(SUMO_ATTR_POSITION, "access", ok, 0);
360  if (!ok) {
361  throw ProcessError();
362  }
363  myCurrentStoppingPlace->accessPos.insert(std::make_pair(lane, pos));
364 }
365 
366 void
368  myCurrentEdge = 0;
369  bool ok = true;
370  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
371  if (!ok) {
372  return;
373  }
375  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
376  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
377  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
379  myNet.addDistrictEdge(myCurrentName, *i, false);
380  }
381  }
382 }
383 
384 
385 void
387  bool ok = true;
388  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, myCurrentName.c_str(), ok);
389  myNet.addDistrictEdge(myCurrentName, id, isSource);
390 }
391 
392 
393 
394 /****************************************************************************/
395 
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 source within a district (connection road)
A single lane the router may use.
Definition: ROLane.h:57
SUMOVehicleParameter::Stop * myCurrentStoppingPlace
The currently built stopping place.
Definition: RONetHandler.h:200
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:193
connectio between two lanes
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&#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: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
begin/end of the description of an edge restriction
SAX-handler base for SUMO-files.
Interface for building instances of router-edges.
const std::vector< ROLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: ROEdge.h:449
JunctionGraph myJunctionGraph
Definition: RONetHandler.h:210
void addParkingArea(const std::string &id, SUMOVehicleParameter::Stop *stop)
Definition: RONet.cpp:224
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
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
An edge where vehicles disappear (no vehicle may leave this edge)
Definition: ROEdge.h:91
Encapsulated SAX-Attributes.
virtual std::vector< std::string > getStringVector(int attr) const =0
Tries to read given attribute assuming it is a string vector.
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:182
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.
void addIncoming(ROEdge *edge)
Definition: RONode.h:87
A basic edge for routing applications.
Definition: ROEdge.h:77
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
begin/end of the description of an edge
std::string lane
The lane to stop at.
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:206
void setRestrictions(const std::map< SUMOVehicleClass, SUMOReal > *restrictions)
Sets the vehicle class specific speed limits of the edge.
Definition: ROEdge.h:149
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&#39;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
a sink within a district (connection road)
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:208
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
virtual SumoXMLEdgeFunc getEdgeFunc(bool &ok) const =0
Returns the value of the named attribute.
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:707
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