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-2020 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  if (myCurrentLine != nullptr) {
97  bool ok = true;
98  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
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  }
105  break;
106  default:
107  break;
108  }
109 }
110 
111 void
113  switch (element) {
114  case SUMO_TAG_BUS_STOP:
115  case SUMO_TAG_TRAIN_STOP:
116  myCurrentStop = nullptr;
117  myCurrentStopWasIgnored = false;
118  break;
119  case SUMO_TAG_PT_LINE:
120  case SUMO_TAG_FLOW:
121  case SUMO_TAG_TRIP:
123  myCurrentLine = nullptr;
124  break;
125  case SUMO_TAG_ROUTE:
126  myCurrentRouteID = "";
127  break;
128  default:
129  break;
130  }
131 }
132 
133 
134 void
136  bool ok = true;
137  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "busStop", ok);
138  const std::string name = attrs.getOpt<std::string>(SUMO_ATTR_NAME, id.c_str(), ok, "");
139  const std::string laneID = attrs.get<std::string>(SUMO_ATTR_LANE, id.c_str(), ok);
140  const double startPos = attrs.get<double>(SUMO_ATTR_STARTPOS, id.c_str(), ok);
141  const double endPos = attrs.get<double>(SUMO_ATTR_ENDPOS, id.c_str(), ok);
142  const double parkingLength = attrs.getOpt<double>(SUMO_ATTR_PARKING_LENGTH, id.c_str(), ok, 0);
143  //const std::string lines = attrs.get<std::string>(SUMO_ATTR_LINES, id.c_str(), ok);
144  const int laneIndex = NBEdge::getLaneIndexFromLaneID(laneID);
145  const std::string edgeID = SUMOXMLDefinitions::getEdgeIDFromLane(laneID);
146  NBEdge* edge = myEdgeCont.retrieve(edgeID);
147  if (edge == nullptr) {
148  if (!myEdgeCont.wasIgnored(edgeID)) {
149  WRITE_ERROR("Edge '" + edgeID + "' for stop '" + id + "' not found");
150  } else {
152  }
153  return;
154  }
155  if (edge->getNumLanes() <= laneIndex) {
156  WRITE_ERROR("Lane '" + laneID + "' for stop '" + id + "' not found");
157  return;
158  }
159  SVCPermissions permissions = edge->getPermissions(laneIndex);
160  // 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
161  if (!isRailway(permissions) && permissions != SVC_SHIP && permissions != SVC_TAXI) {
162  permissions = SVC_BUS;
163  }
164  if (ok) {
165  Position pos = edge->geometryPositionAtOffset((startPos + endPos) / 2);
166  myCurrentStop = new NBPTStop(id, pos, edgeID, edgeID, endPos - startPos, name, permissions, parkingLength);
168  WRITE_ERROR("Could not add public transport stop '" + id + "' (already exists)");
169  }
170  }
171 }
172 
173 void
175  if (myCurrentStop == nullptr) {
177  return;
178  } else {
179  throw InvalidArgument("Could not add access outside a stopping place.");
180  }
181  }
182  bool ok = true;
183  const std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, "access", ok);
184  const double pos = attrs.get<double>(SUMO_ATTR_POSITION, "access", ok);
185  const double length = attrs.getOpt<double>(SUMO_ATTR_LENGTH, "access", ok, -1);
186  myCurrentStop->addAccess(lane, pos, length);
187 }
188 
189 
190 void
192  bool ok = true;
193  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "ptLine", ok);
194  const std::string name = attrs.getOpt<std::string>(SUMO_ATTR_ID, id.c_str(), ok, "");
195  const std::string line = attrs.get<std::string>(SUMO_ATTR_LINE, id.c_str(), ok);
196  const std::string type = attrs.get<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok);
198  if (attrs.hasAttribute(SUMO_ATTR_VCLASS)) {
199  vClass = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_VCLASS, id.c_str(), ok));
200  }
201  const int intervalS = attrs.getOpt<int>(SUMO_ATTR_PERIOD, id.c_str(), ok, -1);
202  const std::string nightService = attrs.getStringSecure("nightService", "");
203  myCurrentCompletion = StringUtils::toDouble(attrs.getStringSecure("completeness", "1"));
204  if (ok) {
205  myCurrentLine = new NBPTLine(id, name, type, line, intervalS / 60, nightService, vClass);
207  }
208 }
209 
210 
211 void
213  bool ok = true;
214  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "flow", ok);
215  const std::string line = attrs.get<std::string>(SUMO_ATTR_LINE, id.c_str(), ok);
216  const std::string type = attrs.get<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok);
217  const std::string route = attrs.get<std::string>(SUMO_ATTR_ROUTE, id.c_str(), ok);
219  const int intervalS = attrs.getOpt<int>(SUMO_ATTR_PERIOD, id.c_str(), ok, -1);
220  if (ok) {
221  myCurrentLine = new NBPTLine(id, "", type, line, intervalS / 60, "", vClass);
223  for (NBPTStop* stop : myRouteStops[route]) {
224  myCurrentLine->addPTStop(stop);
225  }
227  }
228 }
229 
230 
231 void
233  if (myCurrentLine == nullptr) {
234  WRITE_ERROR("Found route outside line definition");
235  return;
236  }
237  const std::vector<std::string>& edgeIDs = attrs.getStringVector(SUMO_ATTR_EDGES);
238  EdgeVector edges;
239  for (const std::string& edgeID : edgeIDs) {
240  NBEdge* edge = myEdgeCont.retrieve(edgeID);
241  if (edge == nullptr) {
242  if (!myEdgeCont.wasIgnored(edgeID)) {
243  WRITE_ERROR("Edge '" + edgeID + "' in route of line '" + myCurrentLine->getName() + "' not found");
244  }
245  } else {
246  edges.push_back(edge);
247  }
248  }
249  myCurrentLine->setEdges(edges);
250 }
251 
252 void
254  bool ok = true;
255  myCurrentRouteID = attrs.get<std::string>(SUMO_ATTR_ID, "route", ok);
256  const std::vector<std::string>& edgeIDs = attrs.getStringVector(SUMO_ATTR_EDGES);
257  EdgeVector edges;
258  for (const std::string& edgeID : edgeIDs) {
259  NBEdge* edge = myEdgeCont.retrieve(edgeID);
260  if (edge == nullptr) {
261  if (!myEdgeCont.wasIgnored(edgeID)) {
262  WRITE_ERROR("Edge '" + edgeID + "' in route of line '" + myCurrentLine->getName() + "' not found");
263  }
264  } else {
265  edges.push_back(edge);
266  }
267  }
269 }
270 
271 
272 void
274  bool ok = true;
275  const std::string id = attrs.hasAttribute(SUMO_ATTR_ID)
276  ? attrs.get<std::string>(SUMO_ATTR_ID, "ptLine", ok)
277  : attrs.get<std::string>(SUMO_ATTR_BUS_STOP, "ptline", ok);
278  NBPTStop* stop = myStopCont.get(id);
279  if (stop == nullptr) {
280  WRITE_ERROR("Stop '" + id + "' within line '" + toString(myCurrentLine->getLineID()) + "' not found");
281  return;
282  }
283  myCurrentLine->addPTStop(stop);
284 }
285 
286 void
288  assert(myCurrentRouteID != "");
289  bool ok = true;
290  const std::string id = attrs.hasAttribute(SUMO_ATTR_ID)
291  ? attrs.get<std::string>(SUMO_ATTR_ID, "ptLine", ok)
292  : attrs.get<std::string>(SUMO_ATTR_BUS_STOP, "ptline", ok);
293  NBPTStop* stop = myStopCont.get(id);
294  if (stop == nullptr) {
295  WRITE_ERROR("Stop '" + id + "' within route '" + toString(myCurrentRouteID) + "' not found");
296  return;
297  }
298  myRouteStops[myCurrentRouteID].push_back(stop);
299 }
300 
301 
302 /****************************************************************************/
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:284
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
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_ID
@ SUMO_ATTR_KEY
@ SUMO_ATTR_POSITION
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:44
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:3655
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:490
Position geometryPositionAtOffset(double offset) const
return position taking into account loaded length
Definition: NBEdge.cpp:3944
static int getLaneIndexFromLaneID(const std::string laneID)
Definition: NBEdge.cpp:4050
void insert(NBPTLine *ptLine)
insert new line
void setName(const std::string &name)
Definition: NBPTLine.h:86
void addPTStop(NBPTStop *pStop)
Definition: NBPTLine.cpp:42
const std::string & getLineID() const
Definition: NBPTLine.h:45
const std::string & getName() const
Definition: NBPTLine.h:49
void setMyNumOfStops(int numStops)
Definition: NBPTLine.cpp:137
std::vector< NBPTStop * > getStops()
Definition: NBPTLine.cpp:47
void setEdges(const std::vector< NBEdge * > &edges)
Definition: NBPTLine.cpp:114
bool insert(NBPTStop *ptStop)
Inserts a node into the map.
NBPTStop * get(std::string id)
Retrieve a previously inserted pt stop.
The representation of a single pt stop.
Definition: NBPTStop.h:44
void addAccess(std::string laneID, double offset, double length)
Definition: NBPTStop.cpp:247
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.
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:36
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.
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.
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