SUMO - Simulation of Urban MObility
NBPTLineCont.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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // Container for NBPTLine during netbuild
19 /****************************************************************************/
20 
21 #include <iostream>
23 #include <utils/common/ToString.h>
25 #include "NBPTLineCont.h"
26 #include "NBPTStop.h"
27 #include "NBEdge.h"
28 #include "NBNode.h"
29 #include "NBPTStopCont.h"
30 
31 // ===========================================================================
32 // static value definitions
33 // ===========================================================================
34 const int NBPTLineCont::FWD(1);
35 const int NBPTLineCont::BWD(-1);
36 // ===========================================================================
37 // method definitions
38 // ===========================================================================
39 
41  :
42  myIdCnt(0) { }
43 
44 
46  for (auto& myPTLine : myPTLines) {
47  delete myPTLine;
48  }
49  myPTLines.clear();
50 }
51 
52 void
54  pLine->setId(myIdCnt++);
55  myPTLines.push_back(pLine);
56 }
58  for (auto& myPTLine : myPTLines) {
59  reviseStops(myPTLine, cont);
60 
61  constructRoute(myPTLine, cont);
62 
63 
64  //
65 
66 
67 
68  }
69 }
71  std::vector<NBPTStop*> stops = myPTLine->getStops();
72  for (auto& stop : stops) {
73  //get the corresponding and one of the two adjacent ways
74  std::string origId = stop->getOrigEdgeId();
75 
76  std::vector<std::string> waysIds = myPTLine->getMyWays();
77  auto waysIdsIt = waysIds.begin();
78  if (waysIds.size() <= 1) {
79  WRITE_WARNING("Cannot revise pt stop localization for pt line: " + myPTLine->getName()
80  + ", which consist of one way only. Ignoring!");
81  continue;
82  }
83  for (; waysIdsIt != waysIds.end(); waysIdsIt++) {
84  if ((*waysIdsIt) == origId) {
85  break;
86  }
87  }
88 
89  if (waysIdsIt == waysIds.end()) {
90  for (auto& edgeCand : stop->getMyAdditionalEdgeCandidates()) {
91  bool found = false;
92  waysIdsIt = waysIds.begin();
93  for (; waysIdsIt != waysIds.end(); waysIdsIt++) {
94  if ((*waysIdsIt) == edgeCand.first) {
95  if (stop->setEdgeId(edgeCand.second, cont)) {
96  stop->setMyOrigEdgeId(edgeCand.first);
97  origId = edgeCand.first;
98  found = true;
99  break;
100  }
101  }
102  }
103  if (found) {
104  break;
105  }
106  }
107  }
108 
109 
110  if (waysIdsIt == waysIds.end()) {
111  WRITE_WARNING("Cannot revise pt stop localization for incomplete pt line: " + myPTLine->getName()
112  + ". Ignoring!");
113  continue;
114  }
115 
116  std::vector<long long int>* way = myPTLine->getWaysNodes(origId);
117  if (way == nullptr) {
118  WRITE_WARNING("Cannot revise pt stop localization for incomplete pt line: " + myPTLine->getName()
119  + ". Ignoring!");
120  continue;
121  }
122 
123 
124  int dir;
125  std::string adjIdPrev;
126  std::string adjIdNext;
127  if (waysIdsIt != waysIds.begin()) {
128  adjIdPrev = *(waysIdsIt - 1);
129  }
130  if (waysIdsIt != (waysIds.end() - 1)) {
131  adjIdNext = *(waysIdsIt + 1);
132  }
133  std::vector<long long int>* wayPrev = myPTLine->getWaysNodes(adjIdPrev);
134  std::vector<long long int>* wayNext = myPTLine->getWaysNodes(adjIdNext);
135  if (wayPrev == nullptr && wayNext == nullptr) {
136  WRITE_WARNING("Cannot revise pt stop localization for incomplete pt line: " + myPTLine->getName()
137  + ". Ignoring!");
138  continue;
139  }
140  long long int wayEnds = *(way->end() - 1);
141  long long int wayBegins = *(way->begin());
142  long long int wayPrevEnds = wayPrev != nullptr ? *(wayPrev->end() - 1) : 0;
143  long long int wayPrevBegins = wayPrev != nullptr ? *(wayPrev->begin()) : 0;
144  long long int wayNextEnds = wayNext != nullptr ? *(wayNext->end() - 1) : 0;
145  long long int wayNextBegins = wayNext != nullptr ? *(wayNext->begin()) : 0;
146  if (wayBegins == wayPrevEnds || wayBegins == wayPrevBegins || wayEnds == wayNextBegins
147  || wayEnds == wayNextEnds) {
148  dir = FWD;
149  } else if (wayEnds == wayPrevBegins || wayEnds == wayPrevEnds || wayBegins == wayNextEnds
150  || wayBegins == wayNextBegins) {
151  dir = BWD;
152  } else {
153  WRITE_WARNING("Cannot revise pt stop localization for incomplete pt line: " + myPTLine->getName()
154  + ". Ignoring!");
155  continue;
156  }
157 
158  std::string edgeId = stop->getEdgeId();
159  NBEdge* current = cont.getByID(edgeId);
160  int assignedTo = edgeId.at(0) == '-' ? BWD : FWD;
161 
162  if (dir != assignedTo) {
163  NBEdge* reverse = NBPTStopCont::getReverseEdge(current);
164  if (reverse == nullptr) {
165  WRITE_WARNING("Could not re-assign PT stop: " + stop->getID() + " probably broken osm file");
166  continue;
167  }
168  stop->setEdgeId(reverse->getID(), cont);
169  WRITE_WARNING("PT stop: " + stop->getID() + " has been moved to edge: " + reverse->getID());
170  }
171  myServedPTStops.insert(stop->getID());
172  }
173 }
175 
176 
177  std::vector<NBEdge*> edges;
178 
179  NBNode* first = nullptr;
180  NBNode* last = nullptr;
181  std::vector<NBEdge*> prevWayEdges;
182  std::vector<NBEdge*> prevWayMinusEdges;
183  prevWayEdges.clear();
184  prevWayMinusEdges.clear();
185  std::vector<NBEdge*> currentWayEdges;
186  std::vector<NBEdge*> currentWayMinusEdges;
187  for (auto it3 = pTLine->getMyWays().begin();
188  it3 != pTLine->getMyWays().end(); it3++) {
189 
190  if (cont.retrieve(*it3, false) != nullptr) {
191  currentWayEdges.push_back(cont.retrieve(*it3, false));
192  } else {
193  int i = 0;
194  while (cont.retrieve(*it3 + "#" + std::to_string(i), false) != nullptr) {
195  currentWayEdges.push_back(cont.retrieve(*it3 + "#" + std::to_string(i), false));
196  i++;
197  }
198  }
199 
200  if (cont.retrieve("-" + *it3, false) != nullptr) {
201  currentWayMinusEdges.push_back(cont.retrieve("-" + *it3, false));
202  } else {
203  int i = 0;
204  while (cont.retrieve("-" + *it3 + "#" + std::to_string(i), false) != nullptr) {
205  currentWayMinusEdges.insert(currentWayMinusEdges.begin(),
206  cont.retrieve("-" + *it3 + "#" + std::to_string(i), false));
207  i++;
208  }
209  }
210  if (currentWayEdges.empty()) {
211  continue;
212  }
213  if (last == currentWayEdges.front()->getFromNode() && last != nullptr) {
214  if (!prevWayEdges.empty()) {
215  edges.insert(edges.end(), prevWayEdges.begin(), prevWayEdges.end());
216  prevWayEdges.clear();
217  prevWayMinusEdges.clear();
218  }
219  edges.insert(edges.end(), currentWayEdges.begin(), currentWayEdges.end());
220  last = currentWayEdges.back()->getToNode();
221  } else if (last == currentWayEdges.back()->getToNode() && last != nullptr) {
222  if (!prevWayEdges.empty()) {
223  edges.insert(edges.end(), prevWayEdges.begin(), prevWayEdges.end());
224  prevWayEdges.clear();
225  prevWayMinusEdges.clear();
226  }
227  if (currentWayMinusEdges.empty()) {
228  currentWayEdges.clear();
229  last = nullptr;
230  continue;
231  } else {
232  edges.insert(edges.end(), currentWayMinusEdges.begin(), currentWayMinusEdges.end());
233  last = currentWayMinusEdges.back()->getToNode();
234  }
235  } else if (first == currentWayEdges.front()->getFromNode() && first != nullptr) {
236  edges.insert(edges.end(), prevWayMinusEdges.begin(), prevWayMinusEdges.end());
237  edges.insert(edges.end(), currentWayEdges.begin(), currentWayEdges.end());
238  last = currentWayEdges.back()->getToNode();
239  prevWayEdges.clear();
240  prevWayMinusEdges.clear();
241  } else if (first == currentWayEdges.back()->getToNode() && first != nullptr) {
242  edges.insert(edges.end(), prevWayMinusEdges.begin(), prevWayMinusEdges.end());
243  if (currentWayMinusEdges.empty()) {
244  currentWayEdges.clear();
245  last = nullptr;
246  prevWayEdges.clear();
247  prevWayMinusEdges.clear();
248  continue;
249  } else {
250  edges.insert(edges.end(), currentWayMinusEdges.begin(), currentWayMinusEdges.end());
251  last = currentWayMinusEdges.back()->getToNode();
252  prevWayEdges.clear();
253  prevWayMinusEdges.clear();
254  }
255  } else {
256  if (it3 != pTLine->getMyWays().begin()) {
257  WRITE_WARNING("Incomplete route for ptline '" + toString(pTLine->getLineID()) + "' (" + pTLine->getName() + ")");
258  }
259  prevWayEdges = currentWayEdges;
260  prevWayMinusEdges = currentWayMinusEdges;
261  if (!prevWayEdges.empty()) {
262  first = prevWayEdges.front()->getFromNode();
263  last = prevWayEdges.back()->getToNode();
264  } else {
265  first = nullptr;
266  last = nullptr;
267  }
268  }
269  currentWayEdges.clear();
270  currentWayMinusEdges.clear();
271  }
272 
273  auto fr = edges.begin();
274  NBPTStop* frStop = pTLine->getStops()[0];
275  for (; fr != edges.end(); fr++) {
276  if ((*fr)->getID() == frStop->getEdgeId()) {
277  break;
278  }
279  }
280  auto to = fr;
281  NBPTStop* toStop = *(pTLine->getStops().end() - 1);
282  for (; to != edges.end(); to++) {
283  if ((*to)->getID() == toStop->getEdgeId()) {
284  to++;
285  break;
286  }
287  }
288 
289  pTLine->addEdgeVector(fr, to);
290 }
291 
292 
293 void
294 NBPTLineCont::addEdges2Keep(const OptionsCont& oc, std::set<std::string>& into) {
295  if (oc.isSet("ptline-output")) {
296  for (auto line : myPTLines) {
297  for (auto edge : line->getRoute()) {
298  into.insert(edge->getID());
299  }
300  }
301  }
302 }
303 std::set<std::string>& NBPTLineCont::getServedPTStops() {
304  return myServedPTStops;
305 }
std::vector< long long int > * getWaysNodes(std::string wayId)
Definition: NBPTLine.cpp:105
NBEdge * getByID(const std::string &edgeID) const
Returns the edge with id if it exists.
Definition: NBEdgeCont.cpp:850
void reviseStops(NBPTLine *myPTLine, NBEdgeCont &cont)
const std::string getEdgeId()
Definition: NBPTStop.cpp:64
The representation of a single edge during network building.
Definition: NBEdge.h:70
static const int BWD
Definition: NBPTLineCont.h:63
const std::string & getID() const
Returns the id.
Definition: Named.h:74
The representation of a single pt stop.
Definition: NBPTStop.h:51
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
const std::vector< std::string > & getMyWays() const
Definition: NBPTLine.cpp:102
void process(NBEdgeCont &cont)
bool isSet(const std::string &name, bool failOnNonExistant=true) const
Returns the information whether the named option is set.
std::set< std::string > & getServedPTStops()
void insert(NBPTLine *pLine)
insert new line
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:55
void addEdgeVector(std::vector< NBEdge *>::iterator fr, std::vector< NBEdge *>::iterator to)
Definition: NBPTLine.cpp:115
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
static const int FWD
Definition: NBPTLineCont.h:62
PTLinesCont myPTLines
The map of names to pt lines.
Definition: NBPTLineCont.h:69
void addEdges2Keep(const OptionsCont &oc, std::set< std::string > &into)
add edges that must be kept
long long int myIdCnt
Definition: NBPTLineCont.h:71
~NBPTLineCont()
destructor
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:250
A storage for options typed value containers)
Definition: OptionsCont.h:98
Represents a single node (junction) during network building.
Definition: NBNode.h:74
const std::string & getName() const
Definition: NBPTLine.cpp:40
std::vector< NBPTStop * > getStops()
Definition: NBPTLine.cpp:49
void constructRoute(NBPTLine *myPTLine, NBEdgeCont &cont)
NBPTLineCont()
constructor
long long int getLineID() const
Definition: NBPTLine.cpp:45
static NBEdge * getReverseEdge(NBEdge *edge)
void setId(long long int id)
Definition: NBPTLine.cpp:90
std::set< std::string > myServedPTStops
Definition: NBPTLineCont.h:75