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-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 /****************************************************************************/
19 // The representation of one direction of a single pt line
20 /****************************************************************************/
22 
23 #include <utility>
24 #include <utils/common/ToString.h>
27 #include "NBEdgeCont.h"
28 #include "NBPTLine.h"
29 #include "NBPTStop.h"
30 
31 NBPTLine::NBPTLine(const std::string& id, const std::string& name, const std::string& type, const std::string& ref, int interval, const std::string& nightService,
32  SUMOVehicleClass vClass) :
33  myName(name),
34  myType(type),
35  myPTLineId(id),
36  myRef(ref != "" ? ref : name),
37  myInterval(interval),
38  myNightService(nightService),
39  myVClass(vClass)
40 { }
41 
43  myPTStops.push_back(pStop);
44 
45 }
46 
47 std::vector<NBPTStop*> NBPTLine::getStops() {
48  return myPTStops;
49 }
50 
52  device.openTag(SUMO_TAG_PT_LINE);
54  if (!myName.empty()) {
56  }
57 
61  if (myInterval > 0) {
62  // write seconds
64  }
65  if (myNightService != "") {
66  device.writeAttr("nightService", myNightService);
67  }
68  device.writeAttr("completeness", toString((double)myPTStops.size() / (double)myNumOfStops));
69 
70  std::vector<std::string> validEdgeIDs;
71  // filter out edges that have been removed due to joining junctions
72  // (the rest of the route is valid)
73  for (NBEdge* e : myRoute) {
74  if (ec.retrieve(e->getID())) {
75  validEdgeIDs.push_back(e->getID());
76  }
77  }
78  if (!myRoute.empty()) {
79  device.openTag(SUMO_TAG_ROUTE);
80  device.writeAttr(SUMO_ATTR_EDGES, validEdgeIDs);
81  device.closeTag();
82  }
83 
84  for (auto& myPTStop : myPTStops) {
85  device.openTag(SUMO_TAG_BUS_STOP);
86  device.writeAttr(SUMO_ATTR_ID, myPTStop->getID());
87  device.writeAttr(SUMO_ATTR_NAME, StringUtils::escapeXML(myPTStop->getName()));
88  device.closeTag();
89  }
90  device.closeTag();
91 
92 }
93 
94 void NBPTLine::addWayNode(long long int way, long long int node) {
95  std::string wayStr = toString(way);
96  if (wayStr != myCurrentWay) {
97  myCurrentWay = wayStr;
98  myWays.push_back(wayStr);
99  }
100  myWaysNodes[wayStr].push_back(node);
101 
102 }
103 const std::vector<std::string>& NBPTLine::getMyWays() const {
104  return myWays;
105 }
106 std::vector<long long int>* NBPTLine::getWaysNodes(std::string wayId) {
107  if (myWaysNodes.find(wayId) != myWaysNodes.end()) {
108  return &myWaysNodes[wayId];
109  }
110  return nullptr;
111 }
112 
113 void
114 NBPTLine::setEdges(const std::vector<NBEdge*>& edges) {
115  myRoute = edges;
116  // ensure permissions
117  for (NBEdge* e : edges) {
118  SVCPermissions permissions = e->getPermissions();
119  if ((permissions & myVClass) != myVClass) {
121  if (permissions != 0 && (permissions & nVuln) == 0) {
122  // this is a footpath or sidewalk. Add another lane
123  e->addRestrictedLane(SUMO_const_laneWidth, myVClass);
124  } else {
125  // add permissions to the rightmost lane that is not exclusively used for pedestrians / bicycles
126  for (int i = 0; i < (int)e->getNumLanes(); i++) {
127  if ((e->getPermissions(i) & nVuln) != 0) {
128  e->allowVehicleClass(i, myVClass);
129  break;
130  }
131  }
132  }
133  }
134  }
135 }
136 
137 void NBPTLine::setMyNumOfStops(int numStops) {
138  myNumOfStops = numStops;
139 }
140 const std::vector<NBEdge*>& NBPTLine::getRoute() const {
141  return myRoute;
142 }
143 
144 std::vector<NBEdge*>
146  std::vector<NBEdge*> result;
147  for (NBPTStop* stop : myPTStops) {
148  NBEdge* e = ec.retrieve(stop->getEdgeId());
149  if (e != nullptr) {
150  result.push_back(e);
151  }
152  }
153  return result;
154 }
155 
156 NBEdge*
158  std::vector<NBEdge*> validEdges;
159  // filter out edges that have been removed due to joining junctions
160  for (NBEdge* e : myRoute) {
161  if (ec.retrieve(e->getID())) {
162  validEdges.push_back(e);
163  }
164  }
165  if (validEdges.size() == 0) {
166  return nullptr;
167  }
168  // filter out edges after the first stop
169  if (myPTStops.size() > 0) {
170  NBEdge* firstStopEdge = ec.retrieve(myPTStops.front()->getEdgeId());
171  if (firstStopEdge == nullptr) {
172  WRITE_WARNING("Could not retrieve edge '" + myPTStops.front()->getEdgeId() + "' for first stop of line '" + myPTLineId + "'");
173  return nullptr;
174 
175  }
176  auto it = std::find(validEdges.begin(), validEdges.end(), firstStopEdge);
177  if (it == validEdges.end()) {
178  WRITE_WARNING("First stop edge '" + firstStopEdge->getID() + "' is not part of the route of line '" + myPTLineId + "'");
179  return nullptr;
180  }
181  }
182  return validEdges.front();
183 }
184 
185 NBEdge*
187  std::vector<NBEdge*> validEdges;
188  // filter out edges that have been removed due to joining junctions
189  for (NBEdge* e : myRoute) {
190  if (ec.retrieve(e->getID())) {
191  validEdges.push_back(e);
192  }
193  }
194  if (validEdges.size() == 0) {
195  return nullptr;
196  }
197  // filter out edges after the last stop
198  if (myPTStops.size() > 0) {
199  NBEdge* lastStopEdge = ec.retrieve(myPTStops.back()->getEdgeId());
200  if (lastStopEdge == nullptr) {
201  WRITE_WARNING("Could not retrieve edge '" + myPTStops.back()->getEdgeId() + "' for last stop of line '" + myPTLineId + "'");
202  return nullptr;
203 
204  }
205  auto it = std::find(validEdges.begin(), validEdges.end(), lastStopEdge);
206  if (it == validEdges.end()) {
207  WRITE_WARNING("Last stop edge '" + lastStopEdge->getID() + "' is not part of the route of line '" + myPTLineId + "'");
208  return nullptr;
209  }
210  }
211  return validEdges.back();
212 }
213 
214 void
216  for (int i = 0; i < (int)myPTStops.size(); i++) {
217  if (myPTStops[i] == oldStop) {
218  myPTStops[i] = newStop;
219  }
220  }
221 }
222 
223 void
224 NBPTLine::replaceEdge(const std::string& edgeID, const EdgeVector& replacement) {
225  EdgeVector oldRoute = myRoute;
226  int stopIndex = 0;
227  myRoute.clear();
228  std::vector<NBPTStop*> unassigned;
229  for (NBEdge* e : oldRoute) {
230  if (e->getID() == edgeID) {
231  myRoute.insert(myRoute.end(), replacement.begin(), replacement.end());
232  } else {
233  myRoute.push_back(e);
234  }
235  while (stopIndex < (int)myPTStops.size() && myPTStops[stopIndex]->getEdgeId() == e->getID()) {
236  if (e->getID() == edgeID) {
237  NBPTStop* stop = myPTStops[stopIndex];
238  // find best edge among replacement edges
239  double bestDist = std::numeric_limits<double>::max();
240  NBEdge* bestEdge = nullptr;
241  for (NBEdge* cand : replacement) {
242  double dist = cand->getGeometry().distance2D(stop->getPosition());
243  if (dist < bestDist) {
244  bestDist = dist;
245  bestEdge = cand;
246  }
247  }
248  if (bestDist != std::numeric_limits<double>::max()) {
249  stop->findLaneAndComputeBusStopExtent(bestEdge);
250  if ((bestEdge->getPermissions() & SVC_PEDESTRIAN) != 0) {
251  // no need for access
252  stop->clearAccess();
253  }
254  } else {
255  WRITE_WARNING("Could not re-assign ptstop '" + stop->getID() + "' after replacing edge '" + edgeID + "'");
256  unassigned.push_back(stop);
257  }
258  }
259  stopIndex++;
260  }
261  }
262  for (NBPTStop* stop : unassigned) {
263  myPTStops.erase(std::find(myPTStops.begin(), myPTStops.end(), stop));
264  }
265 }
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:276
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:34
SUMOVehicleClass
Definition of vehicle classes to differ between different lane usage and authority types.
@ SVC_BICYCLE
vehicle is a bicycle
@ SVC_PEDESTRIAN
pedestrian
int SVCPermissions
bitset where each bit declares whether a certain SVC may use this edge/lane
@ SUMO_TAG_PT_LINE
A pt line.
@ SUMO_TAG_BUS_STOP
A bus stop.
@ SUMO_TAG_ROUTE
begin/end of the description of a route
@ 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_ID
const double SUMO_const_laneWidth
Definition: StdDefs.h:47
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
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
const std::string & getID() const
Definition: NBEdge.h:1423
const PositionVector & getGeometry() const
Returns the geometry of the edge.
Definition: NBEdge.h:716
std::vector< NBPTStop * > myPTStops
Definition: NBPTLine.h:93
SUMOVehicleClass myVClass
Definition: NBPTLine.h:111
std::string myPTLineId
Definition: NBPTLine.h:104
void addPTStop(NBPTStop *pStop)
Definition: NBPTLine.cpp:42
std::vector< std::string > myWays
Definition: NBPTLine.h:97
std::vector< NBEdge * > myRoute
Definition: NBPTLine.h:117
const std::vector< std::string > & getMyWays() const
Definition: NBPTLine.cpp:103
int myNumOfStops
Definition: NBPTLine.h:122
void write(OutputDevice &device, NBEdgeCont &ec)
Definition: NBPTLine.cpp:51
int myInterval
Definition: NBPTLine.h:108
std::string myName
Definition: NBPTLine.h:91
std::vector< long long int > * getWaysNodes(std::string wayId)
Definition: NBPTLine.cpp:106
std::string myCurrentWay
Definition: NBPTLine.h:103
std::string myRef
Definition: NBPTLine.h:105
NBEdge * getRouteEnd(const NBEdgeCont &ec) const
return last valid edge of myRoute (if it doest not lie before the last stop)
Definition: NBPTLine.cpp:186
const std::vector< NBEdge * > & getRoute() const
Definition: NBPTLine.cpp:140
void addWayNode(long long int way, long long int node)
Definition: NBPTLine.cpp:94
std::string myType
Definition: NBPTLine.h:92
std::string myNightService
Definition: NBPTLine.h:110
NBEdge * getRouteStart(const NBEdgeCont &ec) const
return first valid edge of myRoute (if it doest not lie after the first stop)
Definition: NBPTLine.cpp:157
std::map< std::string, std::vector< long long int > > myWaysNodes
Definition: NBPTLine.h:96
std::vector< NBEdge * > getStopEdges(const NBEdgeCont &ec) const
get stop edges
Definition: NBPTLine.cpp:145
void setMyNumOfStops(int numStops)
Definition: NBPTLine.cpp:137
void replaceStop(NBPTStop *oldStop, NBPTStop *newStop)
replace the given stop
Definition: NBPTLine.cpp:215
void replaceEdge(const std::string &edgeID, const EdgeVector &replacement)
replace the edge with the given edge list
Definition: NBPTLine.cpp:224
std::vector< NBPTStop * > getStops()
Definition: NBPTLine.cpp:47
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:31
void setEdges(const std::vector< NBEdge * > &edges)
Definition: NBPTLine.cpp:114
The representation of a single pt stop.
Definition: NBPTStop.h:44
bool findLaneAndComputeBusStopExtent(const NBEdgeCont &ec)
Definition: NBPTStop.cpp:206
void clearAccess()
remove all access definitions
Definition: NBPTStop.cpp:242
std::string getID() const
Definition: NBPTStop.cpp:50
const Position & getPosition() const
Definition: NBPTStop.cpp:73
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:60
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:239
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
double distance2D(const Position &p, bool perpendicular=false) const
closest 2D-distance to point p (or -1 if perpendicular is true and the point is beyond this vector)
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.