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-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // Importer for static public transport information
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <string>
25 #include <iostream>
26 #include <map>
27 #include <cmath>
28 #include <xercesc/sax/HandlerBase.hpp>
29 #include <xercesc/sax/AttributeList.hpp>
30 #include <xercesc/sax/SAXParseException.hpp>
31 #include <xercesc/sax/SAXException.hpp>
33 #include <netbuild/NBNodeCont.h>
34 #include <netbuild/NBTypeCont.h>
35 #include <netbuild/NBNetBuilder.h>
41 #include <utils/common/ToString.h>
44 #include "NIXMLNodesHandler.h"
45 #include "NIXMLPTHandler.h"
46 
47 
48 // ===========================================================================
49 // method definitions
50 // ===========================================================================
52  SUMOSAXHandler("public transport - file"),
53  myEdgeCont(ec),
54  myStopCont(sc),
55  myLineCont(lc),
56  myCurrentLine(nullptr) {
57 }
58 
59 
61 
62 
63 void
65  const SUMOSAXAttributes& attrs) {
66  switch (element) {
67  case SUMO_TAG_BUS_STOP:
69  if (myCurrentLine == nullptr) {
70  addPTStop(attrs);
71  } else {
72  addPTLineStop(attrs);
73  }
74  break;
75  case SUMO_TAG_ACCESS:
76  addAccess(attrs);
77  break;
78  case SUMO_TAG_PT_LINE:
79  addPTLine(attrs);
80  break;
81  case SUMO_TAG_PARAM:
82  if (myLastParameterised.size() != 0) {
83  bool ok = true;
84  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
85  // circumventing empty string test
86  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
87  myLastParameterised.back()->setParameter(key, val);
88  }
89  break;
90  default:
91  break;
92  }
93 }
94 
95 void
97  switch (element) {
98  case SUMO_TAG_BUS_STOP:
100  myCurrentStop = nullptr;
101  break;
102  case SUMO_TAG_PT_LINE:
104  myCurrentLine = nullptr;
105  break;
106  default:
107  break;
108  }
109 }
110 
111 
112 void
114  bool ok = true;
115  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "busStop", ok);
116  const std::string name = attrs.getOpt<std::string>(SUMO_ATTR_NAME, id.c_str(), ok, "");
117  const std::string laneID = attrs.get<std::string>(SUMO_ATTR_LANE, id.c_str(), ok);
118  const double startPos = attrs.get<double>(SUMO_ATTR_STARTPOS, id.c_str(), ok);
119  const double endPos = attrs.get<double>(SUMO_ATTR_ENDPOS, id.c_str(), ok);
120  //const std::string lines = attrs.get<std::string>(SUMO_ATTR_LINES, id.c_str(), ok);
121  const int laneIndex = NBEdge::getLaneIndexFromLaneID(laneID);
122  const std::string edgeID = SUMOXMLDefinitions::getEdgeIDFromLane(laneID);
123  NBEdge* edge = myEdgeCont.retrieve(edgeID);
124  if (edge == nullptr) {
125  WRITE_ERROR("Edge '" + edgeID + "' for stop '" + id + "' not found");
126  return;
127  }
128  if (edge->getNumLanes() <= laneIndex) {
129  WRITE_ERROR("Lane '" + laneID + "' for stop '" + id + "' not found");
130  return;
131  }
132  SVCPermissions permissions = edge->getPermissions(laneIndex);
133  if (ok) {
134  Position pos = edge->getGeometry().positionAtOffset2D((startPos + endPos) / 2);
135  myCurrentStop = new NBPTStop(id, pos, edgeID, edgeID, endPos - startPos, name, permissions);
137  WRITE_ERROR("Could not add public transport stop '" + id + "' (already exists)");
138  }
139  }
140 }
141 
142 void
144  if (myCurrentStop == nullptr) {
145  throw InvalidArgument("Could not add access outside a stopping place.");
146  }
147  bool ok = true;
148  const std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, "access", ok);
149  const double pos = attrs.get<double>(SUMO_ATTR_POSITION, "access", ok);
150  const double length = attrs.getOpt<double>(SUMO_ATTR_LENGTH, "access", ok, -1);
151  myCurrentStop->addAccess(lane, pos, length);
152 }
153 
154 
155 void
157  bool ok = true;
158  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "ptLine", ok);
159  const std::string name = attrs.getOpt<std::string>(SUMO_ATTR_ID, id.c_str(), ok, "");
160  const std::string line = attrs.get<std::string>(SUMO_ATTR_LINE, id.c_str(), ok);
161  const std::string type = attrs.get<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok);
162  const int intervalS = attrs.getOpt<int>(SUMO_ATTR_PERIOD, id.c_str(), ok, -1);
163  const std::string nightService = attrs.getStringSecure("nightService", "");
164  myCurrentCompletion = StringUtils::toDouble(attrs.getStringSecure("completeness", "1"));
166  //if (!myRoute.empty()) {
167  // device.openTag(SUMO_TAG_ROUTE);
168  // device.writeAttr(SUMO_ATTR_EDGES, validEdgeIDs);
169  // device.closeTag();
170  //}
171  if (ok) {
172  myCurrentLine = new NBPTLine(id, name, type, line, intervalS / 60, nightService);
174  }
175 }
176 
177 
178 void
180  bool ok = true;
181  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "ptLine", ok);
182  NBPTStop* stop = myStopCont.get(id);
183  if (stop == nullptr) {
184  WRITE_ERROR("Stop '" + id + "' within line '" + toString(myCurrentLine->getLineID()) + "' not found");
185  return;
186  }
187  myCurrentLine->addPTStop(stop);
188 }
189 
190 
191 /****************************************************************************/
192 
void addAccess(std::string laneID, double offset, double length)
Definition: NBPTStop.cpp:232
void myEndElement(int element)
Called when a closing tag occurs.
void addAccess(const SUMOSAXAttributes &attrs)
Parses an stop access definition.
void addPTStop(const SUMOSAXAttributes &attrs)
Parses an public transport stop.
Position positionAtOffset2D(double pos, double lateralOffset=0) const
Returns the position at the given length.
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
The representation of a single edge during network building.
Definition: NBEdge.h:86
void addPTStop(NBPTStop *pStop)
Definition: NBPTLine.cpp:36
The representation of a single pt stop.
Definition: NBPTStop.h:45
SAX-handler base for SUMO-files.
NBPTStopCont & myStopCont
The stop container (for loading of stops)
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list...
NBPTLine * myCurrentLine
The currently processed line.
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
void setMyNumOfStops(int numStops)
Definition: NBPTLine.cpp:113
std::vector< Parameterised * > myLastParameterised
element to receive parameters
double myCurrentCompletion
the completion level of the current line
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
Encapsulated SAX-Attributes.
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter ...
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:465
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
parameter associated to a certain key
NBPTLineCont & myLineCont
The line container (for loading of lines)
static int getLaneIndexFromLaneID(const std::string laneID)
Definition: NBEdge.cpp:3800
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:61
const std::string & getLineID() const
Definition: NBPTLine.h:42
~NIXMLPTHandler()
Destructor.
void insert(NBPTLine *ptLine)
insert new line
void addPTLine(const SUMOSAXAttributes &attrs)
Parses a public transport line.
SVCPermissions getPermissions(int lane=-1) const
get the union of allowed classes over all lanes or for a specific lane
Definition: NBEdge.cpp:3441
NBPTStop * myCurrentStop
The currently processed stop.
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:680
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
void addPTLineStop(const SUMOSAXAttributes &attrs)
Parses an public transport stop reference within a line element.
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.
A train stop (alias for bus stop)
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:245
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
virtual std::string getStringSecure(int id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
NBPTStop * get(std::string id)
Retrieve a previously inserted pt stop.
std::vector< NBPTStop * > getStops()
Definition: NBPTLine.cpp:41
bool insert(NBPTStop *ptStop)
Inserts a node into the map.
NBEdgeCont & myEdgeCont
The edges container (for retrieving referenced stop edge)
static std::string getEdgeIDFromLane(const std::string laneID)
return edge id when given the lane ID
An access point for a train stop.
NIXMLPTHandler(NBEdgeCont &ec, NBPTStopCont &sc, NBPTLineCont &lc)
Constructor.