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 /****************************************************************************/
14 // Importer for static public transport information
15 /****************************************************************************/
16 
17 
18 // ===========================================================================
19 // included modules
20 // ===========================================================================
21 #include <config.h>
22 
23 #include <string>
24 #include <iostream>
25 #include <map>
26 #include <cmath>
27 #include <xercesc/sax/HandlerBase.hpp>
28 #include <xercesc/sax/AttributeList.hpp>
29 #include <xercesc/sax/SAXParseException.hpp>
30 #include <xercesc/sax/SAXException.hpp>
32 #include <netbuild/NBNodeCont.h>
33 #include <netbuild/NBTypeCont.h>
34 #include <netbuild/NBNetBuilder.h>
40 #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_ROUTE:
82  addRoute(attrs);
83  break;
84  case SUMO_TAG_PARAM:
85  if (myLastParameterised.size() != 0) {
86  bool ok = true;
87  const std::string key = attrs.get<std::string>(SUMO_ATTR_KEY, nullptr, ok);
88  // circumventing empty string test
89  const std::string val = attrs.hasAttribute(SUMO_ATTR_VALUE) ? attrs.getString(SUMO_ATTR_VALUE) : "";
90  myLastParameterised.back()->setParameter(key, val);
91  }
92  break;
93  default:
94  break;
95  }
96 }
97 
98 void
100  switch (element) {
101  case SUMO_TAG_BUS_STOP:
102  case SUMO_TAG_TRAIN_STOP:
103  myCurrentStop = nullptr;
104  break;
105  case SUMO_TAG_PT_LINE:
107  myCurrentLine = nullptr;
108  break;
109  default:
110  break;
111  }
112 }
113 
114 
115 void
117  bool ok = true;
118  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "busStop", ok);
119  const std::string name = attrs.getOpt<std::string>(SUMO_ATTR_NAME, id.c_str(), ok, "");
120  const std::string laneID = attrs.get<std::string>(SUMO_ATTR_LANE, id.c_str(), ok);
121  const double startPos = attrs.get<double>(SUMO_ATTR_STARTPOS, id.c_str(), ok);
122  const double endPos = attrs.get<double>(SUMO_ATTR_ENDPOS, id.c_str(), ok);
123  //const std::string lines = attrs.get<std::string>(SUMO_ATTR_LINES, id.c_str(), ok);
124  const int laneIndex = NBEdge::getLaneIndexFromLaneID(laneID);
125  const std::string edgeID = SUMOXMLDefinitions::getEdgeIDFromLane(laneID);
126  NBEdge* edge = myEdgeCont.retrieve(edgeID);
127  if (edge == nullptr) {
128  WRITE_ERROR("Edge '" + edgeID + "' for stop '" + id + "' not found");
129  return;
130  }
131  if (edge->getNumLanes() <= laneIndex) {
132  WRITE_ERROR("Lane '" + laneID + "' for stop '" + id + "' not found");
133  return;
134  }
135  SVCPermissions permissions = edge->getPermissions(laneIndex);
136  if (ok) {
137  Position pos = edge->geometryPositionAtOffset((startPos + endPos) / 2);
138  myCurrentStop = new NBPTStop(id, pos, edgeID, edgeID, endPos - startPos, name, permissions);
140  WRITE_ERROR("Could not add public transport stop '" + id + "' (already exists)");
141  }
142  }
143 }
144 
145 void
147  if (myCurrentStop == nullptr) {
148  throw InvalidArgument("Could not add access outside a stopping place.");
149  }
150  bool ok = true;
151  const std::string lane = attrs.get<std::string>(SUMO_ATTR_LANE, "access", ok);
152  const double pos = attrs.get<double>(SUMO_ATTR_POSITION, "access", ok);
153  const double length = attrs.getOpt<double>(SUMO_ATTR_LENGTH, "access", ok, -1);
154  myCurrentStop->addAccess(lane, pos, length);
155 }
156 
157 
158 void
160  bool ok = true;
161  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "ptLine", ok);
162  const std::string name = attrs.getOpt<std::string>(SUMO_ATTR_ID, id.c_str(), ok, "");
163  const std::string line = attrs.get<std::string>(SUMO_ATTR_LINE, id.c_str(), ok);
164  const std::string type = attrs.get<std::string>(SUMO_ATTR_TYPE, id.c_str(), ok);
166  if (attrs.hasAttribute(SUMO_ATTR_VCLASS)) {
167  vClass = getVehicleClassID(attrs.get<std::string>(SUMO_ATTR_ID, id.c_str(), ok));
168  }
169  const int intervalS = attrs.getOpt<int>(SUMO_ATTR_PERIOD, id.c_str(), ok, -1);
170  const std::string nightService = attrs.getStringSecure("nightService", "");
171  myCurrentCompletion = StringUtils::toDouble(attrs.getStringSecure("completeness", "1"));
172  if (ok) {
173  myCurrentLine = new NBPTLine(id, name, type, line, intervalS / 60, nightService, vClass);
175  }
176 }
177 
178 void
180  if (myCurrentLine == nullptr) {
181  WRITE_ERROR("Found route outside line definition");
182  return;
183  }
184  const std::vector<std::string>& edgeIDs = attrs.getStringVector(SUMO_ATTR_EDGES);
185  EdgeVector edges;
186  for (const std::string& edgeID : edgeIDs) {
187  NBEdge* edge = myEdgeCont.retrieve(edgeID);
188  if (edge == nullptr) {
189  WRITE_ERROR("Edge '" + edgeID + "' in route of line '" + myCurrentLine->getName() + "' not found");
190  } else {
191  edges.push_back(edge);
192  }
193  }
194  myCurrentLine->setEdges(edges);
195 }
196 
197 
198 void
200  bool ok = true;
201  const std::string id = attrs.get<std::string>(SUMO_ATTR_ID, "ptLine", ok);
202  NBPTStop* stop = myStopCont.get(id);
203  if (stop == nullptr) {
204  WRITE_ERROR("Stop '" + id + "' within line '" + toString(myCurrentLine->getLineID()) + "' not found");
205  return;
206  }
207  myCurrentLine->addPTStop(stop);
208 }
209 
210 
211 /****************************************************************************/
212 
SUMO_TAG_TRAIN_STOP
A train stop (alias for bus stop)
Definition: SUMOXMLDefinitions.h:99
NBPTLine::getName
const std::string & getName() const
Definition: NBPTLine.h:46
SUMO_ATTR_TYPE
Definition: SUMOXMLDefinitions.h:381
SUMOSAXAttributes::getStringVector
const std::vector< std::string > getStringVector(int attr) const
Tries to read given attribute assuming it is a string vector.
Definition: SUMOSAXAttributes.cpp:113
SUMOVehicleClass
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
Definition: SUMOVehicleClass.h:133
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:27
NBEdgeCont::retrieve
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:246
NIXMLPTHandler::addPTLine
void addPTLine(const SUMOSAXAttributes &attrs)
Parses a public transport line.
Definition: NIXMLPTHandler.cpp:159
getVehicleClassID
SUMOVehicleClass getVehicleClassID(const std::string &name)
Returns the class id of the abstract class given by its name.
Definition: SUMOVehicleClass.cpp:200
NIXMLPTHandler::myLineCont
NBPTLineCont & myLineCont
The line container (for loading of lines)
Definition: NIXMLPTHandler.h:136
NBEdgeCont
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:60
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:393
SUMOSAXHandler
SAX-handler base for SUMO-files.
Definition: SUMOSAXHandler.h:41
NIXMLPTHandler::~NIXMLPTHandler
~NIXMLPTHandler()
Destructor.
Definition: NIXMLPTHandler.cpp:60
GeomConvHelper.h
NBPTLineCont::insert
void insert(NBPTLine *ptLine)
insert new line
Definition: NBPTLineCont.cpp:53
OptionsCont.h
SUMO_TAG_PARAM
parameter associated to a certain key
Definition: SUMOXMLDefinitions.h:169
NIXMLPTHandler::addPTLineStop
void addPTLineStop(const SUMOSAXAttributes &attrs)
Parses an public transport stop reference within a line element.
Definition: NIXMLPTHandler.cpp:199
SUMO_ATTR_LINE
Definition: SUMOXMLDefinitions.h:775
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:345
NBEdge::geometryPositionAtOffset
Position geometryPositionAtOffset(double offset) const
return position taking into account loaded length
Definition: NBEdge.cpp:3672
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:492
NBPTLine::getStops
std::vector< NBPTStop * > getStops()
Definition: NBPTLine.cpp:43
MsgHandler.h
EdgeVector
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
NIImporter_OpenStreetMap.h
SUMOSAXHandler.h
SUMO_ATTR_PERIOD
Definition: SUMOXMLDefinitions.h:645
NIXMLPTHandler::myCurrentStop
NBPTStop * myCurrentStop
The currently processed stop.
Definition: NIXMLPTHandler.h:139
NBPTLine
Definition: NBPTLine.h:33
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:378
SUMO_ATTR_LANE
Definition: SUMOXMLDefinitions.h:637
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:3404
SUMO_ATTR_ENDPOS
Definition: SUMOXMLDefinitions.h:798
NBPTLine::setMyNumOfStops
void setMyNumOfStops(int numStops)
Definition: NBPTLine.cpp:133
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:91
NIImporter_OpenStreetMap::interpretTransportType
static SUMOVehicleClass interpretTransportType(const std::string &type, NIOSMNode *toSet=nullptr)
translate osm transport designations into sumo vehicle class
Definition: NIImporter_OpenStreetMap.cpp:1839
NBPTLineCont
Definition: NBPTLineCont.h:26
SUMO_ATTR_STARTPOS
Definition: SUMOXMLDefinitions.h:797
SVCPermissions
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
Definition: SUMOVehicleClass.h:218
NIXMLPTHandler::myEdgeCont
NBEdgeCont & myEdgeCont
The edges container (for retrieving referenced stop edge)
Definition: NIXMLPTHandler.h:130
NBTypeCont.h
NBEdge::getLaneIndexFromLaneID
static int getLaneIndexFromLaneID(const std::string laneID)
Definition: NBEdge.cpp:3778
SUMO_ATTR_EDGES
the edges of a route
Definition: SUMOXMLDefinitions.h:427
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:477
NIXMLPTHandler::addAccess
void addAccess(const SUMOSAXAttributes &attrs)
Parses an stop access definition.
Definition: NIXMLPTHandler.cpp:146
NBNetBuilder.h
NBPTLine::addPTStop
void addPTStop(NBPTStop *pStop)
Definition: NBPTLine.cpp:38
Position
A point in 2D or 3D with translation and scaling methods.
Definition: Position.h:38
NIXMLPTHandler::addPTStop
void addPTStop(const SUMOSAXAttributes &attrs)
Parses an public transport stop.
Definition: NIXMLPTHandler.cpp:116
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:518
NIXMLPTHandler::myEndElement
void myEndElement(int element)
Called when a closing tag occurs.
Definition: NIXMLPTHandler.cpp:99
NBPTStop::addAccess
void addAccess(std::string laneID, double offset, double length)
Definition: NBPTStop.cpp:233
SUMO_ATTR_POSITION
Definition: SUMOXMLDefinitions.h:660
NBNodeCont.h
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
StringUtils.h
SUMO_TAG_BUS_STOP
A bus stop.
Definition: SUMOXMLDefinitions.h:97
SUMO_TAG_PT_LINE
A pt line.
Definition: SUMOXMLDefinitions.h:101
NIXMLPTHandler::addRoute
void addRoute(const SUMOSAXAttributes &attrs)
Parses a route as port of a public transport line.
Definition: NIXMLPTHandler.cpp:179
NBPTLine::setEdges
void setEdges(const std::vector< NBEdge * > &edges)
Definition: NBPTLine.cpp:110
NBPTStopCont::get
NBPTStop * get(std::string id)
Retrieve a previously inserted pt stop.
Definition: NBPTStopCont.cpp:50
InvalidArgument
Definition: UtilExceptions.h:56
SUMO_ATTR_KEY
Definition: SUMOXMLDefinitions.h:408
SUMOXMLDefinitions::getEdgeIDFromLane
static std::string getEdgeIDFromLane(const std::string laneID)
return edge id when given the lane ID
Definition: SUMOXMLDefinitions.cpp:961
SUMO_ATTR_VALUE
Definition: SUMOXMLDefinitions.h:779
NIXMLPTHandler::myStopCont
NBPTStopCont & myStopCont
The stop container (for loading of stops)
Definition: NIXMLPTHandler.h:133
SUMO_ATTR_VCLASS
Definition: SUMOXMLDefinitions.h:450
NIXMLPTHandler::myCurrentLine
NBPTLine * myCurrentLine
The currently processed line.
Definition: NIXMLPTHandler.h:142
SUMO_TAG_ROUTE
begin/end of the description of a route
Definition: SUMOXMLDefinitions.h:125
config.h
StringTokenizer.h
SUMO_ATTR_NAME
Definition: SUMOXMLDefinitions.h:380
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:38
SUMO_TAG_ACCESS
An access point for a train stop.
Definition: SUMOXMLDefinitions.h:103
NBPTStop
The representation of a single pt stop.
Definition: NBPTStop.h:44
SUMOSAXAttributes
Encapsulated SAX-Attributes.
Definition: SUMOSAXAttributes.h:56
WRITE_ERROR
#define WRITE_ERROR(msg)
Definition: MsgHandler.h:283
SUMOXMLDefinitions.h
NIXMLPTHandler::myCurrentCompletion
double myCurrentCompletion
the completion level of the current line
Definition: NIXMLPTHandler.h:145
NIXMLPTHandler::myLastParameterised
std::vector< Parameterised * > myLastParameterised
element to receive parameters
Definition: NIXMLPTHandler.h:148