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-2023 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>
27#include "NBEdge.h"
28#include "NBEdgeCont.h"
29#include "NBPTStop.h"
30#include "NBPTStopCont.h"
31#include "NBPTLine.h"
32
33
34// ===========================================================================
35// method definitions
36// ===========================================================================
37NBPTLine::NBPTLine(const std::string& id, const std::string& name, const std::string& type, const std::string& ref, int interval, const std::string& nightService,
38 SUMOVehicleClass vClass, RGBColor color) :
39 myName(name),
40 myType(type),
41 myPTLineId(id),
42 myRef(ref != "" ? ref : name),
43 myColor(color),
44 myInterval(interval),
45 myNightService(nightService),
46 myVClass(vClass)
47{ }
48
49
50void
51NBPTLine::addPTStop(std::shared_ptr<NBPTStop> pStop) {
52 if (!myPTStops.empty() && pStop->getName() != "" && myPTStops.back()->getName() == pStop->getName()) {
53 // avoid duplicate stop when both platform and stop_position are given as nodes
54 if (myPTStops.back()->isPlatform() && !pStop->isPlatform()) {
55 myPTStops.pop_back();
56 } else if (pStop->isPlatform()) {
57 return;
58 }
59 }
60 myPTStops.push_back(pStop);
61}
62
63
64const std::vector<std::shared_ptr<NBPTStop> >&
66 return myPTStops;
67}
68
69
70void
74 if (!myName.empty()) {
76 }
77
81 if (myInterval > 0) {
82 // write seconds
84 }
85 if (myNightService != "") {
86 device.writeAttr("nightService", myNightService);
87 }
88
89 if (myColor.isValid()) {
91 }
92 device.writeAttr("completeness", toString((double)myPTStops.size() / (double)myNumOfStops));
93
94 if (!myRoute.empty()) {
95 device.openTag(SUMO_TAG_ROUTE);
97 device.closeTag();
98 }
99
100 for (auto& myPTStop : myPTStops) {
102 device.writeAttr(SUMO_ATTR_ID, myPTStop->getID());
103 device.writeAttr(SUMO_ATTR_NAME, StringUtils::escapeXML(myPTStop->getName()));
104 device.closeTag();
105 }
106 device.closeTag();
107
108}
109
110
111void
112NBPTLine::addWayNode(long long int way, long long int node) {
113 std::string wayStr = toString(way);
114 if (wayStr != myCurrentWay) {
115 myCurrentWay = wayStr;
116 myWays.push_back(wayStr);
117 }
118 myWayNodes[wayStr].push_back(node);
119}
120
121
122const std::vector<long long int>*
123NBPTLine::getWayNodes(std::string wayId) {
124 if (myWayNodes.find(wayId) != myWayNodes.end()) {
125 return &myWayNodes[wayId];
126 }
127 return nullptr;
128}
129
130
131void
132NBPTLine::setEdges(const std::vector<NBEdge*>& edges) {
133 myRoute = edges;
134 // ensure permissions
135 for (NBEdge* e : edges) {
136 SVCPermissions permissions = e->getPermissions();
137 if ((permissions & myVClass) != myVClass) {
139 if (permissions != 0 && (permissions & nVuln) == 0) {
140 // this is a footpath or sidewalk. Add another lane
141 e->addRestrictedLane(SUMO_const_laneWidth, myVClass);
142 } else {
143 // add permissions to the rightmost lane that is not exclusively used for pedestrians / bicycles
144 for (int i = 0; i < (int)e->getNumLanes(); i++) {
145 if ((e->getPermissions(i) & nVuln) != 0) {
146 e->allowVehicleClass(i, myVClass);
147 break;
148 }
149 }
150 }
151 }
152 }
153}
154
155
156void
158 myNumOfStops = numStops;
159}
160
161
162const std::vector<NBEdge*>&
164 return myRoute;
165}
166
167
168std::vector<std::pair<NBEdge*, std::string> >
170 std::vector<std::pair<NBEdge*, std::string> > result;
171 for (std::shared_ptr<NBPTStop> stop : myPTStops) {
172 NBEdge* e = ec.retrieve(stop->getEdgeId());
173 if (e != nullptr) {
174 result.push_back({e, stop->getID()});
175 }
176 }
177 return result;
178}
179
180
181NBEdge*
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 first stop
194 if (myPTStops.size() > 0) {
195 NBEdge* firstStopEdge = ec.retrieve(myPTStops.front()->getEdgeId());
196 if (firstStopEdge == nullptr) {
197 WRITE_WARNINGF(TL("Could not retrieve edge '%' for first stop of line '%'."), myPTStops.front()->getEdgeId(), myPTLineId);
198 return nullptr;
199
200 }
201 auto it = std::find(validEdges.begin(), validEdges.end(), firstStopEdge);
202 if (it == validEdges.end()) {
203 WRITE_WARNINGF(TL("First stop edge '%' is not part of the route of line '%'."), firstStopEdge->getID(), myPTLineId);
204 return nullptr;
205 }
206 }
207 return validEdges.front();
208}
209
210
211NBEdge*
213 std::vector<NBEdge*> validEdges;
214 // filter out edges that have been removed due to joining junctions
215 for (NBEdge* e : myRoute) {
216 if (ec.retrieve(e->getID())) {
217 validEdges.push_back(e);
218 }
219 }
220 if (validEdges.size() == 0) {
221 return nullptr;
222 }
223 // filter out edges after the last stop
224 if (myPTStops.size() > 0) {
225 NBEdge* lastStopEdge = ec.retrieve(myPTStops.back()->getEdgeId());
226 if (lastStopEdge == nullptr) {
227 WRITE_WARNINGF(TL("Could not retrieve edge '%' for last stop of line '%'."), myPTStops.back()->getEdgeId(), myPTLineId);
228 return nullptr;
229
230 }
231 auto it = std::find(validEdges.begin(), validEdges.end(), lastStopEdge);
232 if (it == validEdges.end()) {
233 WRITE_WARNINGF(TL("Last stop edge '%' is not part of the route of line '%'."), lastStopEdge->getID(), myPTLineId);
234 return nullptr;
235 }
236 }
237 return validEdges.back();
238}
239
240
241bool
242NBPTLine::isConsistent(const std::vector<NBEdge*>& stops) const {
243 if (myRoute.empty() || stops.empty()) {
244 return true;
245 }
246 std::vector<NBEdge*>::const_iterator stopIt = stops.begin();
247 for (const NBEdge* const e : myRoute) {
248 while (stopIt != stops.end() && e == *stopIt) {
249 ++stopIt;
250 }
251 if (stopIt == stops.end()) {
252 return true;
253 }
254 }
255 return false;
256}
257
258
259void
260NBPTLine::replaceStop(std::shared_ptr<NBPTStop> oldStop, std::shared_ptr<NBPTStop> newStop) {
261 for (int i = 0; i < (int)myPTStops.size(); i++) {
262 if (myPTStops[i] == oldStop) {
263 myPTStops[i] = newStop;
264 }
265 }
266}
267
268
269void
270NBPTLine::replaceEdge(const std::string& edgeID, const EdgeVector& replacement) {
271 EdgeVector oldRoute = myRoute;
272 myRoute.clear();
273 for (NBEdge* e : oldRoute) {
274 if (e->getID() == edgeID) {
275 for (NBEdge* e2 : replacement) {
276 if (myRoute.empty() || myRoute.back() != e2) {
277 myRoute.push_back(e2);
278 }
279 }
280 } else {
281 myRoute.push_back(e);
282 }
283 }
284}
285
286
287void
289 // delete stops that are missing or have no edge
290 for (auto it = myPTStops.begin(); it != myPTStops.end();) {
291 std::shared_ptr<NBPTStop> stop = *it;
292 if (sc.get(stop->getID()) == nullptr ||
293 ec.getByID(stop->getEdgeId()) == nullptr) {
294 WRITE_WARNINGF(TL("Removed invalid stop '%' from line '%'."), stop->getID(), getLineID());
295 it = myPTStops.erase(it);
296 } else {
297 it++;
298 }
299
300 }
301}
302
303
304void
306 // delete subsequent stops that belong to the same stopArea
307 long long int lastAreaID = -1;
308 std::string lastName = "";
309 for (auto it = myPTStops.begin(); it != myPTStops.end();) {
310 std::shared_ptr<NBPTStop> stop = *it;
311 if (lastAreaID != -1 && stop->getAreaID() == lastAreaID) {
312 WRITE_WARNINGF(TL("Removed duplicate stop '%' at area '%' from line '%'."), stop->getID(), toString(lastAreaID), getLineID());
313 it = myPTStops.erase(it);
314 } else if (lastName != "" && stop->getName() == lastName) {
315 WRITE_WARNINGF(TL("Removed duplicate stop '%' named '%' from line '%'."), stop->getID(), lastName, getLineID());
316 it = myPTStops.erase(it);
317 } else {
318 it++;
319 }
320 lastAreaID = stop->getAreaID();
321 lastName = stop->getName();
322 }
323}
324
325
326void
328 for (int i = 0; i < (int)myRoute.size();) {
329 const std::pair<NBEdge*, NBEdge*>* split = ec.getSplit(myRoute[i]);
330 if (split != nullptr) {
331 myRoute[i] = split->first;
332 myRoute.insert(myRoute.begin() + i + 1, split->second);
333 } else if (ec.retrieve(myRoute[i]->getID()) == nullptr) {
334 myRoute.erase(myRoute.begin() + i);
335 } else {
336 i++;
337 }
338 }
339}
340
341
342/****************************************************************************/
std::vector< std::string > & split(const std::string &s, char delim, std::vector< std::string > &elems)
#define WRITE_WARNINGF(...)
Definition: MsgHandler.h:268
#define TL(string)
Definition: MsgHandler.h:284
std::vector< NBEdge * > EdgeVector
container for (sorted) edges
Definition: NBCont.h:42
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_COLOR
A color information.
@ SUMO_ATTR_ID
const double SUMO_const_laneWidth
Definition: StdDefs.h:48
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:46
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:59
NBEdge * getByID(const std::string &edgeID) const
Returns the edge with id if it exists.
const std::pair< NBEdge *, NBEdge * > * getSplit(const NBEdge *const origEdge) const
Returns the edge split if the edge has been split, nullptr otherwise.
Definition: NBEdgeCont.h:300
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:279
The representation of a single edge during network building.
Definition: NBEdge.h:92
const std::string & getID() const
Definition: NBEdge.h:1515
void replaceStop(std::shared_ptr< NBPTStop > oldStop, std::shared_ptr< NBPTStop > newStop)
replace the given stop
Definition: NBPTLine.cpp:260
void deleteDuplicateStops()
Definition: NBPTLine.cpp:305
const std::vector< long long int > * getWayNodes(std::string wayId)
Definition: NBPTLine.cpp:123
SUMOVehicleClass myVClass
Definition: NBPTLine.h:132
std::string myPTLineId
Definition: NBPTLine.h:123
void replaceEdge(const std::string &edgeID, const std::vector< NBEdge * > &replacement)
replace the edge with the given edge list
Definition: NBPTLine.cpp:270
bool isConsistent(const std::vector< NBEdge * > &stops) const
return whether the mentioned edges appear in that order in the route
Definition: NBPTLine.cpp:242
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, RGBColor color)
Definition: NBPTLine.cpp:37
std::map< std::string, std::vector< long long int > > myWayNodes
Definition: NBPTLine.h:120
std::vector< std::string > myWays
Definition: NBPTLine.h:121
void deleteInvalidStops(const NBEdgeCont &ec, const NBPTStopCont &sc)
remove invalid stops from the line
Definition: NBPTLine.cpp:288
void write(OutputDevice &device)
Definition: NBPTLine.cpp:71
std::vector< NBEdge * > myRoute
Definition: NBPTLine.h:138
void removeInvalidEdges(const NBEdgeCont &ec)
remove invalid edges from the line
Definition: NBPTLine.cpp:327
int myNumOfStops
Definition: NBPTLine.h:143
std::vector< std::pair< NBEdge *, std::string > > getStopEdges(const NBEdgeCont &ec) const
get stop edges and stop ids
Definition: NBPTLine.cpp:169
int myInterval
Definition: NBPTLine.h:129
std::string myName
Definition: NBPTLine.h:117
RGBColor myColor
Definition: NBPTLine.h:126
const std::string & getLineID() const
Definition: NBPTLine.h:51
std::string myCurrentWay
Definition: NBPTLine.h:122
std::string myRef
Definition: NBPTLine.h:124
NBEdge * getRouteEnd(const NBEdgeCont &ec) const
return last valid edge of myRoute (if it doest not lie before the last stop)
Definition: NBPTLine.cpp:212
const std::vector< NBEdge * > & getRoute() const
Definition: NBPTLine.cpp:163
std::vector< std::shared_ptr< NBPTStop > > myPTStops
Definition: NBPTLine.h:119
void addWayNode(long long int way, long long int node)
Definition: NBPTLine.cpp:112
std::string myType
Definition: NBPTLine.h:118
std::string myNightService
Definition: NBPTLine.h:131
NBEdge * getRouteStart(const NBEdgeCont &ec) const
return first valid edge of myRoute (if it doest not lie after the first stop)
Definition: NBPTLine.cpp:182
const std::vector< std::shared_ptr< NBPTStop > > & getStops()
Definition: NBPTLine.cpp:65
void setMyNumOfStops(int numStops)
Definition: NBPTLine.cpp:157
void addPTStop(std::shared_ptr< NBPTStop > pStop)
Definition: NBPTLine.cpp:51
void setEdges(const std::vector< NBEdge * > &edges)
Definition: NBPTLine.cpp:132
Container for public transport stops during the net building process.
Definition: NBPTStopCont.h:44
std::shared_ptr< NBPTStop > get(std::string id) const
Retrieve a previously inserted pt stop.
Static storage of an output device and its base (abstract) implementation.
Definition: OutputDevice.h:61
OutputDevice & writeAttr(const SumoXMLAttr attr, const T &val)
writes a named attribute
Definition: OutputDevice.h:254
OutputDevice & openTag(const std::string &xmlElement)
Opens an XML tag.
bool closeTag(const std::string &comment="")
Closes the most recently opened tag and optionally adds a comment.
bool isValid() const
check if RGBColor is valid
Definition: RGBColor.cpp:120
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.