Eclipse SUMO - Simulation of Urban MObility
NIXMLPTHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2001-2022 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials are made available under the
5 // terms of the Eclipse Public License 2.0 which is available at
6 // https://www.eclipse.org/legal/epl-2.0/
7 // This Source Code may also be made available under the following Secondary
8 // Licenses when the conditions for such availability set forth in the Eclipse
9 // Public License 2.0 are satisfied: GNU General Public License, version 2
10 // or later which is available at
11 // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 /****************************************************************************/
18 // Importer for static public transport information
19 /****************************************************************************/
20 #include <config.h>
21 
22 #include <string>
23 #include <iostream>
24 #include <map>
25 #include <cmath>
26 #include <xercesc/sax/HandlerBase.hpp>
27 #include <xercesc/sax/AttributeList.hpp>
28 #include <xercesc/sax/SAXParseException.hpp>
29 #include <xercesc/sax/SAXException.hpp>
31 #include <netbuild/NBNodeCont.h>
32 #include <netbuild/NBTypeCont.h>
33 #include <netbuild/NBNetBuilder.h>
39 #include <utils/common/ToString.h>
43 #include "NIXMLNodesHandler.h"
44 #include "NIXMLPTHandler.h"
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
51  SUMOSAXHandler("public transport - file"),
52  myEdgeCont(ec),
53  myStopCont(sc),
54  myLineCont(lc),
55  myCurrentLine(nullptr),
56  myCurrentStopWasIgnored(false)
57 { }
58 
59 
61 
62 
63 void
65  const SUMOSAXAttributes& attrs) {
66  switch (element) {
67  case SUMO_TAG_BUS_STOP:
69  case SUMO_TAG_STOP:
70  if (myCurrentRouteID != "") {
71  addRouteStop(attrs);
72  } else if (myCurrentLine == nullptr) {
73  addPTStop(attrs);
74  } else {
75  addPTLineStop(attrs);
76  }
77  break;
78  case SUMO_TAG_ACCESS:
79  addAccess(attrs);
80  break;
81  case SUMO_TAG_PT_LINE:
82  addPTLine(attrs);
83  break;
84  case SUMO_TAG_ROUTE:
85  if (myCurrentLine == nullptr) {
86  addRoute(attrs);
87  } else {
88  addPTLineRoute(attrs);
89  }
90  break;
91  case SUMO_TAG_FLOW:
92  case SUMO_TAG_TRIP:
93  addPTLineFromFlow(attrs);
94  break;
95  case SUMO_TAG_PARAM: {
96  bool ok = true;
97  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
98  if (myCurrentLine != nullptr) {
99  if (key == "completeness") {
100  myCurrentCompletion = attrs.get<double>(SUMO_ATTR_VALUE, nullptr, ok);
101  } else if (key == "name") {
102  myCurrentLine->setName(attrs.get<std::string>(SUMO_ATTR_VALUE, nullptr, ok));
103  }
104  } else if (myCurrentStop != nullptr) {
105  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
106  myCurrentStop->setParameter(key, val);
107  }
108  }
109  break;
110  default:
111  break;
112  }
113 }
114 
115 void
117  switch (element) {
118  case SUMO_TAG_BUS_STOP:
119  case SUMO_TAG_TRAIN_STOP:
120  myCurrentStop = nullptr;
121  myCurrentStopWasIgnored = false;
122  break;
123  case SUMO_TAG_PT_LINE:
124  case SUMO_TAG_FLOW:
125  case SUMO_TAG_TRIP:
127  myCurrentLine = nullptr;
128  break;
129  case SUMO_TAG_ROUTE:
130  myCurrentRouteID = "";
131  break;
132  default:
133  break;
134  }
135 }
136 
137 
138 void
140  bool ok = true;
141  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "busStop", ok);
142  const std::string name = attrs.getOpt<std::string>(SUMO_ATTR_NAME, id.c_str(), ok, "");
143  const std::string laneID = attrs.get<std::string>(SUMO_ATTR_LANE, id.c_str(), ok);
144  const double startPos = attrs.get<double>(SUMO_ATTR_STARTPOS, id.c_str(), ok);
145  const double endPos = attrs.get<double>(SUMO_ATTR_ENDPOS, id.c_str(), ok);
146  const double parkingLength = attrs.getOpt<double>(SUMO_ATTR_PARKING_LENGTH, id.c_str(), ok, 0);
147  const RGBColor color = attrs.getOpt<RGBColor>(SUMO_ATTR_COLOR, id.c_str(), ok, RGBColor(false));
148  //const std::string lines = attrs.get<std::string>(SUMO_ATTR_LINES, id.c_str(), ok);
149  const int laneIndex = NBEdge::getLaneIndexFromLaneID(laneID);
150  const std::string edgeID = SUMOXMLDefinitions::getEdgeIDFromLane(laneID);
151  NBEdge* edge = myEdgeCont.retrieve(edgeID);
152  if (edge == nullptr) {
153  if (!myEdgeCont.wasIgnored(edgeID)) {
154  WRITE_ERROR("Edge '" + edgeID + "' for stop '" + id + "' not found");
155  } else {
157  }
158  return;
159  }
160  if (edge->getNumLanes() <= laneIndex) {
161  WRITE_ERROR("Lane '" + laneID + "' for stop '" + id + "' not found");
162  return;
163  }
164  SVCPermissions permissions = edge->getPermissions(laneIndex);
165  // possibly the stops were written for a different network. If the lane is not a typical public transport stop lane, assume bus as the default
166  if (!isRailway(permissions) && permissions != SVC_SHIP && permissions != SVC_TAXI) {
167  permissions = SVC_BUS;
168  }
169  if (ok) {
170  Position pos = edge->geometryPositionAtOffset((startPos + endPos) / 2);
171  myCurrentStop = new NBPTStop(id, pos, edgeID, edgeID, endPos - startPos, name, permissions, parkingLength, color);
173  WRITE_ERROR("Could not add public transport stop '" + id + "' (already exists)");
174  }
175  }
176 }
177 
178 void
180  if (myCurrentStop == nullptr) {
182  return;
183  } else {
184  throw InvalidArgument("Could not add access outside a stopping place.");
185  }
186  }
187  bool ok = true;
188  const std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, "access", ok);
189  const double pos = attrs.get<double>(SUMO_ATTR_POSITION, "access", ok);
190  const double length = attrs.getOpt<double>(SUMO_ATTR_LENGTH, "access", ok, -1);
191  myCurrentStop->addAccess(lane, pos, length);
192 }
193 
194 
195 void
197  bool ok = true;
198  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "ptLine", ok);
199  const std::string name = attrs.getOpt<std::string>(SUMO_ATTR_ID, id.c_str(), ok, "");
200  const std::string line = attrs.get<std::string>(SUMO_ATTR_LINE, id.c_str(), ok);
201  const std::string type = attrs.get<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok);
203  if (attrs.hasAttribute(SUMO_ATTR_VCLASS)) {
204  vClass = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_VCLASS, id.c_str(), ok));
205  }
206  RGBColor color(false);
207  if (attrs.hasAttribute(SUMO_ATTR_COLOR)) {
208  color = attrs.getColor();
209  }
210  const int intervalS = attrs.getOpt<int>(SUMO_ATTR_PERIOD, id.c_str(), ok, -1);
211  const std::string nightService = attrs.getStringSecure("nightService", "");
212  myCurrentCompletion = StringUtils::toDouble(attrs.getStringSecure("completeness", "1"));
213  if (ok) {
214  myCurrentLine = new NBPTLine(id, name, type, line, intervalS / 60, nightService, vClass, color);
216  }
217 }
218 
219 
220 void
222  bool ok = true;
223  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "flow", ok);
224  const std::string line = attrs.get<std::string>(SUMO_ATTR_LINE, id.c_str(), ok);
225  const std::string type = attrs.get<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok);
226  const std::string route = attrs.get<std::string>(SUMO_ATTR_ROUTE, id.c_str(), ok);
228  const int intervalS = attrs.getOpt<int>(SUMO_ATTR_PERIOD, id.c_str(), ok, -1);
229  RGBColor color(false);
230  if (attrs.hasAttribute(SUMO_ATTR_COLOR)) {
231  color = attrs.getColor();
232  }
233  if (ok) {
234  myCurrentLine = new NBPTLine(id, "", type, line, intervalS / 60, "", vClass, color);
236  for (NBPTStop* stop : myRouteStops[route]) {
237  myCurrentLine->addPTStop(stop);
238  }
240  }
241 }
242 
243 
244 void
246  if (myCurrentLine == nullptr) {
247  WRITE_ERROR("Found route outside line definition");
248  return;
249  }
250  const std::vector<std::string>& edgeIDs = attrs.getStringVector(SUMO_ATTR_EDGES);
251  EdgeVector edges;
252  for (const std::string& edgeID : edgeIDs) {
253  NBEdge* edge = myEdgeCont.retrieve(edgeID);
254  if (edge == nullptr) {
255  if (!myEdgeCont.wasIgnored(edgeID)) {
256  WRITE_ERROR("Edge '" + edgeID + "' in route of line '" + myCurrentLine->getName() + "' not found");
257  }
258  } else {
259  edges.push_back(edge);
260  }
261  }
262  myCurrentLine->setEdges(edges);
263 }
264 
265 void
267  bool ok = true;
268  myCurrentRouteID = attrs.get<std::string>(SUMO_ATTR_ID, "route", ok);
269  const std::vector<std::string>& edgeIDs = attrs.getStringVector(SUMO_ATTR_EDGES);
270  EdgeVector edges;
271  for (const std::string& edgeID : edgeIDs) {
272  NBEdge* edge = myEdgeCont.retrieve(edgeID);
273  if (edge == nullptr) {
274  if (!myEdgeCont.wasIgnored(edgeID)) {
275  WRITE_ERROR("Edge '" + edgeID + "' in route of line '" + myCurrentLine->getName() + "' not found");
276  }
277  } else {
278  edges.push_back(edge);
279  }
280  }
282 }
283 
284 
285 void
287  bool ok = true;
288  const std::string id = attrs.hasAttribute(SUMO_ATTR_ID)
289  ? attrs.get<std::string>(SUMO_ATTR_ID, "ptLine", ok)
290  : attrs.get<std::string>(SUMO_ATTR_BUS_STOP, "ptline", ok);
291  NBPTStop* stop = myStopCont.get(id);
292  if (stop == nullptr) {
293  WRITE_ERROR("Stop '" + id + "' within line '" + toString(myCurrentLine->getLineID()) + "' not found");
294  return;
295  }
296  myCurrentLine->addPTStop(stop);
297 }
298 
299 void
301  assert(myCurrentRouteID != "");
302  bool ok = true;
303  const std::string id = attrs.hasAttribute(SUMO_ATTR_ID)
304  ? attrs.get<std::string>(SUMO_ATTR_ID, "ptLine", ok)
305  : attrs.get<std::string>(SUMO_ATTR_BUS_STOP, "ptline", ok);
306  NBPTStop* stop = myStopCont.get(id);
307  if (stop == nullptr) {
308  WRITE_ERROR("Stop '" + id + "' within route '" + toString(myCurrentRouteID) + "' not found");
309  return;
310  }
311  myRouteStops[myCurrentRouteID].push_back(stop);
312 }
313 
314 
315 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:288
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:35
SUMOVehicleClass getVehicleClassID(const std::string &name)
Returns the class id of the abstract class given by its name.
bool isRailway(SVCPermissions permissions)
Returns whether an edge with the given permission is a railway edge.
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SVC_SHIP
is an arbitrary ship
@ SVC_TAXI
vehicle is a taxi
@ SVC_BUS
vehicle is a bus
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
@ SUMO_TAG_ACCESS
An access point for a train stop.
@ SUMO_TAG_PT_LINE
A pt line.
@ SUMO_TAG_BUS_STOP
A bus stop.
@ SUMO_TAG_STOP
stop for vehicles
@ SUMO_TAG_FLOW
a flow definitio nusing a from-to edges instead of a route (used by router)
@ SUMO_TAG_ROUTE
begin/end of the description of a route
@ SUMO_TAG_TRAIN_STOP
A train stop (alias for bus stop)
@ SUMO_TAG_PARAM
parameter associated to a certain key
@ SUMO_TAG_TRIP
a single trip definition (used by router)
@ SUMO_ATTR_STARTPOS
@ SUMO_ATTR_LANE
@ SUMO_ATTR_VALUE
@ SUMO_ATTR_PARKING_LENGTH
@ SUMO_ATTR_BUS_STOP
@ SUMO_ATTR_ENDPOS
@ SUMO_ATTR_EDGES
the edges of a route
@ SUMO_ATTR_LINE
@ SUMO_ATTR_NAME
@ SUMO_ATTR_PERIOD
@ SUMO_ATTR_VCLASS
@ SUMO_ATTR_TYPE
@ SUMO_ATTR_LENGTH
@ SUMO_ATTR_ROUTE
@ SUMO_ATTR_COLOR
A color information.
@ SUMO_ATTR_ID
@ SUMO_ATTR_KEY
@ SUMO_ATTR_POSITION
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:59
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:275
bool wasIgnored(std::string id) const
Returns whether the edge with the id was ignored during parsing.
Definition: NBEdgeCont.h:502
The representation of a single edge during network building.
Definition: NBEdge.h:91
SVCPermissions getPermissions(int lane=-1) const
get the union of allowed classes over all lanes or for a specific lane
Definition: NBEdge.cpp:4022
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:515
Position geometryPositionAtOffset(double offset) const
return position taking into account loaded length
Definition: NBEdge.cpp:4344
static int getLaneIndexFromLaneID(const std::string laneID)
Definition: NBEdge.cpp:4450
void insert(NBPTLine *ptLine)
insert new line
void setName(const std::string &name)
Definition: NBPTLine.h:99
void addPTStop(NBPTStop *pStop)
Definition: NBPTLine.cpp:44
const std::string & getLineID() const
Definition: NBPTLine.h:47
const std::string & getName() const
Definition: NBPTLine.h:51
void setMyNumOfStops(int numStops)
Definition: NBPTLine.cpp:142
std::vector< NBPTStop * > getStops()
Definition: NBPTLine.cpp:56
void setEdges(const std::vector< NBEdge * > &edges)
Definition: NBPTLine.cpp:119
bool insert(NBPTStop *ptStop)
Inserts a node into the map.
NBPTStop * get(std::string id) const
Retrieve a previously inserted pt stop.
The representation of a single pt stop.
Definition: NBPTStop.h:46
void addAccess(std::string laneID, double offset, double length)
Definition: NBPTStop.cpp:237
static SUMOVehicleClass interpretTransportType(const std::string &type, NIOSMNode *toSet=nullptr)
translate osm transport designations into sumo vehicle class
NBPTStop * myCurrentStop
The currently processed stop.
NIXMLPTHandler(NBEdgeCont &ec, NBPTStopCont &sc, NBPTLineCont &lc)
Constructor.
void addPTLine(const SUMOSAXAttributes &attrs)
Parses a public transport line.
~NIXMLPTHandler()
Destructor.
std::string myCurrentRouteID
The currently processed stand-alone route.
void addPTLineStop(const SUMOSAXAttributes &attrs)
Parses an public transport stop reference within a line element.
void addPTLineFromFlow(const SUMOSAXAttributes &attrs)
Parses a public transport line.
NBPTStopCont & myStopCont
The stop container (for loading of stops)
void myEndElement(int element)
Called when a closing tag occurs.
NBEdgeCont & myEdgeCont
The edges container (for retrieving referenced stop edge)
std::map< std::string, std::vector< NBPTStop * > > myRouteStops
stand-alone route information
std::map< std::string, EdgeVector > myRouteEdges
NBPTLineCont & myLineCont
The line container (for loading of lines)
bool myCurrentStopWasIgnored
whether the current stop should be discarded
void addPTLineRoute(const SUMOSAXAttributes &attrs)
Parses a route as port of a public transport line.
void addPTStop(const SUMOSAXAttributes &attrs)
Parses an public transport stop.
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
double myCurrentCompletion
the completion level of the current line
void addRoute(const SUMOSAXAttributes &attrs)
Parses a stand-alone route when parsing implicit ptlines from routes and flows.
void addRouteStop(const SUMOSAXAttributes &attrs)
Parses an public transport stop reference within a route element.
void addAccess(const SUMOSAXAttributes &attrs)
Parses an stop access definition.
NBPTLine * myCurrentLine
The currently processed line.
virtual void setParameter(const std::string &key, const std::string &value)
Sets a parameter.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:37
Encapsulated SAX-Attributes.
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.
virtual RGBColor getColor() const =0
Returns the value of the named attribute.
const std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
virtual std::string getStringSecure(int id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
SAX-handler base for SUMO-files.
static std::string getEdgeIDFromLane(const std::string laneID)
return edge id when given the lane ID
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter