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 
52 // ===========================================================================
53 // method definitions
54 // ===========================================================================
57  : SUMOSAXHandler("sumo-network"),
58  myNet(net), myCurrentName(),
59  myCurrentEdge(0), myCurrentStoppingPlace(0),
60  myProcess(true), myEdgeBuilder(eb) {}
61 
62 
64 
65 
66 void
68  const SUMOSAXAttributes& attrs) {
69  switch (element) {
70  case SUMO_TAG_EDGE:
71  // in the first step, we do need the name to allocate the edge
72  // in the second, we need it to know to which edge we have to add
73  // the following edges to
74  parseEdge(attrs);
75  break;
76  case SUMO_TAG_LANE:
77  if (myProcess) {
78  parseLane(attrs);
79  }
80  break;
81  case SUMO_TAG_JUNCTION:
82  parseJunction(attrs);
83  break;
85  parseConnection(attrs);
86  break;
87  case SUMO_TAG_BUS_STOP:
91  parseStoppingPlace(attrs, (SumoXMLTag)element);
92  break;
93  case SUMO_TAG_ACCESS:
94  parseAccess(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 double speed = attrs.get<double>(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  double maxSpeed = attrs.get<double>(SUMO_ATTR_SPEED, id.c_str(), ok);
242  double length = attrs.get<double>(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  double x = attrs.get<double>(SUMO_ATTR_X, id.c_str(), ok);
271  double y = attrs.get<double>(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 ((int)from->getLanes().size() <= fromLane) {
305  throw ProcessError("invalid fromLane '" + toString(fromLane) + "' in connection from '" + fromID + "'.");
306  }
307  if ((int)to->getLanes().size() <= 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, toString(element).c_str(), ok);
321  // get the lane
322  myCurrentStoppingPlace->lane = attrs.get<std::string>(SUMO_ATTR_LANE, toString(element).c_str(), ok);
323  if (!ok) {
324  throw ProcessError();
325  }
327  if (edge == 0) {
328  throw InvalidArgument("Unknown lane '" + myCurrentStoppingPlace->lane + "' for " + toString(element) + " '" + id + "'.");
329  }
330  // get the positions
331  myCurrentStoppingPlace->startPos = attrs.getOpt<double>(SUMO_ATTR_STARTPOS, id.c_str(), ok, 0);
332  myCurrentStoppingPlace->endPos = attrs.getOpt<double>(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);
335  throw InvalidArgument("Invalid position for " + toString(element) + " '" + id + "'.");
336  }
337  if (element == SUMO_TAG_CONTAINER_STOP) {
339  } else if (element == SUMO_TAG_PARKING_AREA) {
341  } else {
343  }
344 }
345 
346 
347 void
349  bool ok = true;
350  const std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, "access", ok);
351  const ROEdge* edge = myNet.getEdgeForLaneID(lane);
352  if (edge == 0) {
353  throw InvalidArgument("Unknown lane '" + lane + "' for access.");
354  }
355  const double pos = attrs.getOpt<double>(SUMO_ATTR_POSITION, "access", ok, 0);
356  if (!ok) {
357  throw ProcessError();
358  }
359  myCurrentStoppingPlace->accessPos.insert(std::make_pair(lane, pos));
360 }
361 
362 void
364  myCurrentEdge = 0;
365  bool ok = true;
366  myCurrentName = attrs.get<std::string>(SUMO_ATTR_ID, 0, ok);
367  if (!ok) {
368  return;
369  }
371  if (attrs.hasAttribute(SUMO_ATTR_EDGES)) {
372  std::vector<std::string> desc = attrs.getStringVector(SUMO_ATTR_EDGES);
373  for (std::vector<std::string>::const_iterator i = desc.begin(); i != desc.end(); ++i) {
375  myNet.addDistrictEdge(myCurrentName, *i, false);
376  }
377  }
378 }
379 
380 
381 void
383  bool ok = true;
384  std::string id = attrs.get<std::string>(SUMO_ATTR_ID, myCurrentName.c_str(), ok);
385  myNet.addDistrictEdge(myCurrentName, id, isSource);
386 }
387 
388 
389 
390 /****************************************************************************/
391 
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:167
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
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...
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:126
void parseDistrict(const SUMOSAXAttributes &attrs)
double getLength() const
Returns the length of the edge.
Definition: ROEdge.h:198
a traffic assignment zone
void addNode(RONode *node)
Definition: RONet.cpp:189
connectio between two lanes
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
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.
virtual void addLane(ROLane *lane)
Adds a lane to the edge while loading.
Definition: ROEdge.cpp:92
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
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.
const std::vector< ROLane * > & getLanes() const
Returns this edge&#39;s lanes.
Definition: ROEdge.h:459
JunctionGraph myJunctionGraph
Definition: RONetHandler.h:210
void addParkingArea(const std::string &id, SUMOVehicleParameter::Stop *stop)
Definition: RONet.cpp:220
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
void addRestriction(const std::string &id, const SUMOVehicleClass svc, const double speed)
Adds a restriction for an edge type.
Definition: RONet.cpp:120
the edges of a route
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:56
An edge where vehicles disappear (no vehicle may leave this edge)
Definition: ROEdge.h:91
double startPos
The stopping position start.
Encapsulated SAX-Attributes.
virtual std::vector< std::string > getStringVector(int attr) const =0
Tries to read given attribute assuming it is a string vector.
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:46
An edge where vehicles are inserted at (no vehicle may come from back)
Definition: ROEdge.h:89
#define POSITION_EPS
Definition: config.h:176
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:47
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
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.
static bool checkStopPos(double &startPos, double &endPos, const double laneLength, const double minLength, const bool friendlyPos)
check start and end position of a stop
The router&#39;s network representation.
Definition: RONet.h:76
bool addDistrict(const std::string id, ROEdge *source, ROEdge *sink)
Definition: RONet.cpp:150
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)
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:198
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:136
RONet & myNet
The net to store the information into.
Definition: RONetHandler.h:188
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: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
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
void setRestrictions(const std::map< SUMOVehicleClass, double > *restrictions)
Sets the vehicle class specific speed limits of the edge.
Definition: ROEdge.h:149
An access point for a train stop.
void addContainerStop(const std::string &id, SUMOVehicleParameter::Stop *stop)
Definition: RONet.cpp:209
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:703
void parseStoppingPlace(const SUMOSAXAttributes &attrs, const SumoXMLTag element)