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 
SUMO_TAG_TRAIN_STOP
A train stop (alias for bus stop)
Definition: SUMOXMLDefinitions.h:100
SUMO_ATTR_TYPE
Definition: SUMOXMLDefinitions.h:382
ToString.h
SUMOSAXAttributes::hasAttribute
virtual bool hasAttribute(int id) const =0
Returns the information whether the named (by its enum-value) attribute is within the current list.
NBPTStopCont
Definition: NBPTStopCont.h:28
NBEdgeCont::retrieve
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:245
NIXMLPTHandler::addPTLine
void addPTLine(const SUMOSAXAttributes &attrs)
Parses a public transport line.
Definition: NIXMLPTHandler.cpp:156
NIXMLPTHandler::myLineCont
NBPTLineCont & myLineCont
The line container (for loading of lines)
Definition: NIXMLPTHandler.h:137
NBEdgeCont
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:61
SUMOSAXAttributes::getString
virtual std::string getString(int id) const =0
Returns the string-value of the named (by its enum-value) attribute.
SUMO_ATTR_LENGTH
Definition: SUMOXMLDefinitions.h:394
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:42
NIXMLPTHandler::~NIXMLPTHandler
~NIXMLPTHandler()
Destructor.
Definition: NIXMLPTHandler.cpp:60
GeomConvHelper.h
NBPTLineCont::insert
void insert(NBPTLine *ptLine)
insert new line
Definition: NBPTLineCont.cpp:51
OptionsCont.h
SUMO_TAG_PARAM
parameter associated to a certain key
Definition: SUMOXMLDefinitions.h:170
NIXMLPTHandler::addPTLineStop
void addPTLineStop(const SUMOSAXAttributes &attrs)
Parses an public transport stop reference within a line element.
Definition: NIXMLPTHandler.cpp:179
SUMO_ATTR_LINE
Definition: SUMOXMLDefinitions.h:772
StringUtils::toDouble
static double toDouble(const std::string &sData)
converts a string into the double value described by it by calling the char-type converter
Definition: StringUtils.cpp:313
SUMOSAXAttributes::get
T get(int attr, const char *objectid, bool &ok, bool report=true) const
Tries to read given attribute assuming it is an int.
Definition: SUMOSAXAttributes.h:493
NBPTLine::getStops
std::vector< NBPTStop * > getStops()
Definition: NBPTLine.cpp:41
MsgHandler.h
SUMOSAXHandler.h
SUMO_ATTR_PERIOD
Definition: SUMOXMLDefinitions.h:643
NIXMLPTHandler::myCurrentStop
NBPTStop * myCurrentStop
The currently processed stop.
Definition: NIXMLPTHandler.h:140
NBPTLine
Definition: NBPTLine.h:34
GeoConvHelper.h
NIXMLPTHandler::myStartElement
void myStartElement(int element, const SUMOSAXAttributes &attrs)
Called on the opening of a tag;.
Definition: NIXMLPTHandler.cpp:64
NIXMLNodesHandler.h
SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:379
SUMO_ATTR_LANE
Definition: SUMOXMLDefinitions.h:635
NBEdge::getPermissions
SVCPermissions getPermissions(int lane=-1) const
get the union of allowed classes over all lanes or for a specific lane
Definition: NBEdge.cpp:3441
SUMO_ATTR_ENDPOS
Definition: SUMOXMLDefinitions.h:795
NBPTLine::setMyNumOfStops
void setMyNumOfStops(int numStops)
Definition: NBPTLine.cpp:113
NIXMLPTHandler.h
NBPTLine::getLineID
const std::string & getLineID() const
Definition: NBPTLine.h:42
NBEdge
The representation of a single edge during network building.
Definition: NBEdge.h:86
NBEdge::getGeometry
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:680
NBPTLineCont
Definition: NBPTLineCont.h:27
SUMO_ATTR_STARTPOS
Definition: SUMOXMLDefinitions.h:794
SVCPermissions
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
Definition: SUMOVehicleClass.h:219
NIXMLPTHandler::myEdgeCont
NBEdgeCont & myEdgeCont
The edges container (for retrieving referenced stop edge)
Definition: NIXMLPTHandler.h:131
NBTypeCont.h
NBEdge::getLaneIndexFromLaneID
static int getLaneIndexFromLaneID(const std::string laneID)
Definition: NBEdge.cpp:3800
NIXMLPTHandler::NIXMLPTHandler
NIXMLPTHandler(NBEdgeCont &ec, NBPTStopCont &sc, NBPTLineCont &lc)
Constructor.
Definition: NIXMLPTHandler.cpp:51
NBEdge::getNumLanes
int getNumLanes() const
Returns the number of lanes.
Definition: NBEdge.h:465
NIXMLPTHandler::addAccess
void addAccess(const SUMOSAXAttributes &attrs)
Parses an stop access definition.
Definition: NIXMLPTHandler.cpp:143
NBNetBuilder.h
NBPTLine::addPTStop
void addPTStop(NBPTStop *pStop)
Definition: NBPTLine.cpp:36
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:39
NIXMLPTHandler::addPTStop
void addPTStop(const SUMOSAXAttributes &attrs)
Parses an public transport stop.
Definition: NIXMLPTHandler.cpp:113
SUMOSAXAttributes::getOpt
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.
Definition: SUMOSAXAttributes.h:519
NIXMLPTHandler::myEndElement
void myEndElement(int element)
Called when a closing tag occurs.
Definition: NIXMLPTHandler.cpp:96
NBPTStop::addAccess
void addAccess(std::string laneID, double offset, double length)
Definition: NBPTStop.cpp:232
SUMO_ATTR_POSITION
Definition: SUMOXMLDefinitions.h:658
NBNodeCont.h
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:48
StringUtils.h
SUMO_TAG_BUS_STOP
A bus stop.
Definition: SUMOXMLDefinitions.h:98
SUMO_TAG_PT_LINE
A pt line.
Definition: SUMOXMLDefinitions.h:102
PositionVector::positionAtOffset2D
Position positionAtOffset2D(double pos, double lateralOffset=0) const
Returns the position at the given length.
Definition: PositionVector.cpp:268
NBPTStopCont::get
NBPTStop * get(std::string id)
Retrieve a previously inserted pt stop.
Definition: NBPTStopCont.cpp:51
InvalidArgument
Definition: UtilExceptions.h:57
SUMO_ATTR_KEY
Definition: SUMOXMLDefinitions.h:409
SUMOXMLDefinitions::getEdgeIDFromLane
static std::string getEdgeIDFromLane(const std::string laneID)
return edge id when given the lane ID
Definition: SUMOXMLDefinitions.cpp:958
SUMO_ATTR_VALUE
Definition: SUMOXMLDefinitions.h:776
NIXMLPTHandler::myStopCont
NBPTStopCont & myStopCont
The stop container (for loading of stops)
Definition: NIXMLPTHandler.h:134
NIXMLPTHandler::myCurrentLine
NBPTLine * myCurrentLine
The currently processed line.
Definition: NIXMLPTHandler.h:143
config.h
StringTokenizer.h
SUMO_ATTR_NAME
Definition: SUMOXMLDefinitions.h:381
SUMOSAXAttributes::getStringSecure
virtual std::string getStringSecure(int id, const std::string &def) const =0
Returns the string-value of the named (by its enum-value) attribute.
NBPTStopCont::insert
bool insert(NBPTStop *ptStop)
Inserts a node into the map.
Definition: NBPTStopCont.cpp:39
SUMO_TAG_ACCESS
An access point for a train stop.
Definition: SUMOXMLDefinitions.h:104
NBPTStop
The representation of a single pt stop.
Definition: NBPTStop.h:45
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:57
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:245
SUMOXMLDefinitions.h
NIXMLPTHandler::myCurrentCompletion
double myCurrentCompletion
the completion level of the current line
Definition: NIXMLPTHandler.h:146
NIXMLPTHandler::myLastParameterised
std::vector< Parameterised * > myLastParameterised
element to receive parameters
Definition: NIXMLPTHandler.h:149