Eclipse SUMO - Simulation of Urban MObility
NBPTLine.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 // The representation of one direction of a single pt line
16 /****************************************************************************/
18 
19 #include <utility>
20 #include <utils/common/ToString.h>
23 #include "NBEdgeCont.h"
24 #include "NBPTLine.h"
25 #include "NBPTStop.h"
26 
27 NBPTLine::NBPTLine(const std::string& id, const std::string& name, const std::string& type, const std::string& ref, int interval, const std::string& nightService,
28  SUMOVehicleClass vClass) :
29  myName(name),
30  myType(type),
31  myPTLineId(id),
32  myRef(ref != "" ? ref : name),
33  myInterval(interval),
34  myNightService(nightService),
35  myVClass(vClass)
36 { }
37 
39  myPTStops.push_back(pStop);
40 
41 }
42 
43 std::vector<NBPTStop*> NBPTLine::getStops() {
44  return myPTStops;
45 }
46 
48  device.openTag(SUMO_TAG_PT_LINE);
50  if (!myName.empty()) {
52  }
53 
57  if (myInterval > 0) {
58  // write seconds
60  }
61  if (myNightService != "") {
62  device.writeAttr("nightService", myNightService);
63  }
64  device.writeAttr("completeness", toString((double)myPTStops.size() / (double)myNumOfStops));
65 
66  std::vector<std::string> validEdgeIDs;
67  // filter out edges that have been removed due to joining junctions
68  // (the rest of the route is valid)
69  for (NBEdge* e : myRoute) {
70  if (ec.retrieve(e->getID())) {
71  validEdgeIDs.push_back(e->getID());
72  }
73  }
74  if (!myRoute.empty()) {
75  device.openTag(SUMO_TAG_ROUTE);
76  device.writeAttr(SUMO_ATTR_EDGES, validEdgeIDs);
77  device.closeTag();
78  }
79 
80  for (auto& myPTStop : myPTStops) {
81  device.openTag(SUMO_TAG_BUS_STOP);
82  device.writeAttr(SUMO_ATTR_ID, myPTStop->getID());
83  device.writeAttr(SUMO_ATTR_NAME, StringUtils::escapeXML(myPTStop->getName()));
84  device.closeTag();
85  }
86  device.closeTag();
87 
88 }
89 
90 void NBPTLine::addWayNode(long long int way, long long int node) {
91  std::string wayStr = toString(way);
92  if (wayStr != myCurrentWay) {
93  myCurrentWay = wayStr;
94  myWays.push_back(wayStr);
95  }
96  myWaysNodes[wayStr].push_back(node);
97 
98 }
99 const std::vector<std::string>& NBPTLine::getMyWays() const {
100  return myWays;
101 }
102 std::vector<long long int>* NBPTLine::getWaysNodes(std::string wayId) {
103  if (myWaysNodes.find(wayId) != myWaysNodes.end()) {
104  return &myWaysNodes[wayId];
105  }
106  return nullptr;
107 }
108 
109 void
110 NBPTLine::setEdges(const std::vector<NBEdge*>& edges) {
111  myRoute = edges;
112  // ensure permissions
113  for (NBEdge* e : edges) {
114  SVCPermissions permissions = e->getPermissions();
115  if ((permissions & myVClass) != myVClass) {
117  if (permissions != 0 && (permissions & nVuln) == 0) {
118  // this is a footpath or sidewalk. Add another lane
119  e->addRestrictedLane(SUMO_const_laneWidth, myVClass);
120  } else {
121  // add permissions to the rightmost lane that is not exclusively used for pedestrians / bicycles
122  for (int i = 0; i < (int)e->getNumLanes(); i++) {
123  if ((e->getPermissions(i) & nVuln) != 0) {
124  e->allowVehicleClass(i, myVClass);
125  break;
126  }
127  }
128  }
129  }
130  }
131 }
132 
133 void NBPTLine::setMyNumOfStops(int numStops) {
134  myNumOfStops = numStops;
135 }
136 const std::vector<NBEdge*>& NBPTLine::getRoute() const {
137  return myRoute;
138 }
139 
140 std::vector<NBEdge*>
142  std::vector<NBEdge*> result;
143  for (NBPTStop* stop : myPTStops) {
144  NBEdge* e = ec.retrieve(stop->getEdgeId());
145  if (e != nullptr) {
146  result.push_back(e);
147  }
148  }
149  return result;
150 }
151 
152 NBEdge*
154  std::vector<NBEdge*> validEdges;
155  // filter out edges that have been removed due to joining junctions
156  for (NBEdge* e : myRoute) {
157  if (ec.retrieve(e->getID())) {
158  validEdges.push_back(e);
159  }
160  }
161  if (validEdges.size() == 0) {
162  return nullptr;
163  }
164  // filter out edges after the first stop
165  if (myPTStops.size() > 0) {
166  NBEdge* firstStopEdge = ec.retrieve(myPTStops.front()->getEdgeId());
167  if (firstStopEdge == nullptr) {
168  WRITE_WARNING("Could not retrieve edge '" + myPTStops.front()->getEdgeId() + "' for first stop of line '" + myPTLineId + "'");
169  return nullptr;
170 
171  }
172  auto it = std::find(validEdges.begin(), validEdges.end(), firstStopEdge);
173  if (it == validEdges.end()) {
174  WRITE_WARNING("First stop edge '" + firstStopEdge->getID() + "' is not part of the route of line '" + myPTLineId + "'");
175  return nullptr;
176  }
177  }
178  return validEdges.front();
179 }
180 
181 NBEdge*
183  std::vector<NBEdge*> validEdges;
184  // filter out edges that have been removed due to joining junctions
185  for (NBEdge* e : myRoute) {
186  if (ec.retrieve(e->getID())) {
187  validEdges.push_back(e);
188  }
189  }
190  if (validEdges.size() == 0) {
191  return nullptr;
192  }
193  // filter out edges after the last stop
194  if (myPTStops.size() > 0) {
195  NBEdge* lastStopEdge = ec.retrieve(myPTStops.back()->getEdgeId());
196  if (lastStopEdge == nullptr) {
197  WRITE_WARNING("Could not retrieve edge '" + myPTStops.back()->getEdgeId() + "' for last stop of line '" + myPTLineId + "'");
198  return nullptr;
199 
200  }
201  auto it = std::find(validEdges.begin(), validEdges.end(), lastStopEdge);
202  if (it == validEdges.end()) {
203  WRITE_WARNING("Last stop edge '" + lastStopEdge->getID() + "' is not part of the route of line '" + myPTLineId + "'");
204  return nullptr;
205  }
206  }
207  return validEdges.back();
208 }
209 
210 void
212  for (int i = 0; i < (int)myPTStops.size(); i++) {
213  if (myPTStops[i] == oldStop) {
214  myPTStops[i] = newStop;
215  }
216  }
217 }
SUMO_ATTR_TYPE
Definition: SUMOXMLDefinitions.h:381
SVC_PEDESTRIAN
pedestrian
Definition: SUMOVehicleClass.h:156
SUMOVehicleClass
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
Definition: SUMOVehicleClass.h:133
ToString.h
NBPTLine::myRoute
std::vector< NBEdge * > myRoute
Definition: NBPTLine.h:104
NBEdgeCont::retrieve
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:246
WRITE_WARNING
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:275
NBEdgeCont
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:60
OutputDevice
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:63
SUMO_ATTR_LINE
Definition: SUMOXMLDefinitions.h:775
NBPTLine::getStops
std::vector< NBPTStop * > getStops()
Definition: NBPTLine.cpp:43
NBPTLine::replaceStop
void replaceStop(NBPTStop *oldStop, NBPTStop *newStop)
replace the given stop
Definition: NBPTLine.cpp:211
MsgHandler.h
SUMO_ATTR_PERIOD
Definition: SUMOXMLDefinitions.h:645
NBEdgeCont.h
NBPTLine::myInterval
int myInterval
Definition: NBPTLine.h:96
SVC_BICYCLE
vehicle is a bicycle
Definition: SUMOVehicleClass.h:179
SUMO_const_laneWidth
const double SUMO_const_laneWidth
Definition: StdDefs.h:49
NBPTLine::getWaysNodes
std::vector< long long int > * getWaysNodes(std::string wayId)
Definition: NBPTLine.cpp:102
NBPTLine::myName
std::string myName
Definition: NBPTLine.h:81
NBPTLine::myPTStops
std::vector< NBPTStop * > myPTStops
Definition: NBPTLine.h:83
SUMO_ATTR_ID
Definition: SUMOXMLDefinitions.h:378
NBPTLine::setMyNumOfStops
void setMyNumOfStops(int numStops)
Definition: NBPTLine.cpp:133
NBPTLine::myNightService
std::string myNightService
Definition: NBPTLine.h:97
NBPTLine::NBPTLine
NBPTLine(const std::string &id, const std::string &name, const std::string &type, const std::string &ref, int interval, const std::string &nightService, SUMOVehicleClass vClass)
Definition: NBPTLine.cpp:27
NBPTLine::write
void write(OutputDevice &device, NBEdgeCont &ec)
Definition: NBPTLine.cpp:47
NBEdge
The representation of a single edge during network building.
Definition: NBEdge.h:91
OutputDevice::closeTag
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
Definition: OutputDevice.cpp:253
OutputDevice::writeAttr
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:255
NBPTLine::getStopEdges
std::vector< NBEdge * > getStopEdges(const NBEdgeCont &ec) const
get stop edges
Definition: NBPTLine.cpp:141
NBPTLine::myWays
std::vector< std::string > myWays
Definition: NBPTLine.h:87
NBPTLine::myCurrentWay
std::string myCurrentWay
Definition: NBPTLine.h:93
SVCPermissions
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
Definition: SUMOVehicleClass.h:218
NBPTLine::myType
std::string myType
Definition: NBPTLine.h:82
StringUtils::escapeXML
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.
Definition: StringUtils.cpp:190
SUMO_ATTR_EDGES
the edges of a route
Definition: SUMOXMLDefinitions.h:427
OutputDevice.h
NBPTLine::addPTStop
void addPTStop(NBPTStop *pStop)
Definition: NBPTLine.cpp:38
NBPTLine::myRef
std::string myRef
Definition: NBPTLine.h:95
NBPTLine::getRoute
const std::vector< NBEdge * > & getRoute() const
Definition: NBPTLine.cpp:136
NBPTLine::myWaysNodes
std::map< std::string, std::vector< long long int > > myWaysNodes
Definition: NBPTLine.h:86
NBPTLine::getRouteEnd
NBEdge * getRouteEnd(const NBEdgeCont &ec) const
return last valid edge of myRoute (if it doest not lie before the last stop)
Definition: NBPTLine.cpp:182
OutputDevice::openTag
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
Definition: OutputDevice.cpp:239
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
NBPTLine::setEdges
void setEdges(const std::vector< NBEdge * > &edges)
Definition: NBPTLine.cpp:110
NBPTLine::myVClass
SUMOVehicleClass myVClass
Definition: NBPTLine.h:98
SUMO_ATTR_VCLASS
Definition: SUMOXMLDefinitions.h:450
NBPTLine::getMyWays
const std::vector< std::string > & getMyWays() const
Definition: NBPTLine.cpp:99
NBPTLine.h
NBPTStop.h
SUMO_TAG_ROUTE
begin/end of the description of a route
Definition: SUMOXMLDefinitions.h:125
NBPTLine::addWayNode
void addWayNode(long long int way, long long int node)
Definition: NBPTLine.cpp:90
NBPTLine::getRouteStart
NBEdge * getRouteStart(const NBEdgeCont &ec) const
return first valid edge of myRoute (if it doest not lie after the first stop)
Definition: NBPTLine.cpp:153
SUMO_ATTR_NAME
Definition: SUMOXMLDefinitions.h:380
NBPTStop
The representation of a single pt stop.
Definition: NBPTStop.h:44
NBPTLine::myPTLineId
std::string myPTLineId
Definition: NBPTLine.h:94
NBEdge::getID
const std::string & getID() const
Definition: NBEdge.h:1380
NBPTLine::myNumOfStops
int myNumOfStops
Definition: NBPTLine.h:109