SUMO - Simulation of Urban MObility
NBPTLineCont.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Container for NBPTLine during netbuild
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
11 // Copyright (C) 2001-2017 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 #include <iostream>
24 #include "NBPTLineCont.h"
25 #include "NBPTStop.h"
26 #include "NBEdge.h"
27 #include "NBNode.h"
28 #include "NBPTStopCont.h"
29 
30 // ===========================================================================
31 // static value definitions
32 // ===========================================================================
33 const int NBPTLineCont::FWD(1);
34 const int NBPTLineCont::BWD(-1);
35 // ===========================================================================
36 // method definitions
37 // ===========================================================================
38 
40  :
41  myIdCnt(0) { }
42 
43 
45  for (auto& myPTLine : myPTLines) {
46  delete myPTLine;
47  }
48  myPTLines.clear();
49 }
50 
51 void
53  pLine->setId(myIdCnt++);
54  myPTLines.push_back(pLine);
55 }
57  for (auto& myPTLine : myPTLines) {
58  reviseStops(myPTLine, cont);
59 
60  constructRoute(myPTLine, cont);
61 
62 
63  //
64 
65 
66 
67  }
68 }
70  std::vector<NBPTStop*> stops = myPTLine->getStops();
71  for (auto& stop : stops) {
72  //get the corresponding and one of the two adjacent ways
73  std::string origId = stop->getOrigEdgeId();
74 
75  std::vector<std::string> waysIds = myPTLine->getMyWays();
76  auto waysIdsIt = waysIds.begin();
77  if (waysIds.size() <= 1) {
78  WRITE_WARNING("Cannot revise pt stop localization for pt line: " + myPTLine->getName()
79  + ", which consist of one way only. Ignoring!");
80  continue;
81  }
82  for (; waysIdsIt != waysIds.end(); waysIdsIt++) {
83  if ((*waysIdsIt) == origId) {
84  break;
85  }
86  }
87 
88  if (waysIdsIt == waysIds.end()) {
89  for (auto& edgeCand : stop->getMyAdditionalEdgeCandidates()) {
90  bool found = false;
91  waysIdsIt = waysIds.begin();
92  for (; waysIdsIt != waysIds.end(); waysIdsIt++) {
93  if ((*waysIdsIt) == edgeCand.first) {
94  stop->setEdgeId(edgeCand.second);
95  stop->setMyOrigEdgeId(edgeCand.first);
96  origId = edgeCand.first;
97  found = true;
98  break;
99  }
100  }
101  if (found) {
102  break;
103  }
104  }
105  }
106 
107 
108  if (waysIdsIt == waysIds.end()) {
109  WRITE_WARNING("Cannot revise pt stop localization for incomplete pt line: " + myPTLine->getName()
110  + ". Ignoring!");
111  continue;
112  }
113 
114  std::vector<long long int>* way = myPTLine->getWaysNodes(origId);
115  if (way == nullptr) {
116  WRITE_WARNING("Cannot revise pt stop localization for incomplete pt line: " + myPTLine->getName()
117  + ". Ignoring!");
118  continue;
119  }
120 
121 
122  int dir;
123  std::string adjIdPrev;
124  std::string adjIdNext;
125  if (waysIdsIt != waysIds.begin()) {
126  adjIdPrev = *(waysIdsIt - 1);
127  }
128  if (waysIdsIt != (waysIds.end() - 1)) {
129  adjIdNext = *(waysIdsIt + 1);
130  }
131  std::vector<long long int>* wayPrev = myPTLine->getWaysNodes(adjIdPrev);
132  std::vector<long long int>* wayNext = myPTLine->getWaysNodes(adjIdNext);
133  if (wayPrev == nullptr && wayNext == nullptr) {
134  WRITE_WARNING("Cannot revise pt stop localization for incomplete pt line: " + myPTLine->getName()
135  + ". Ignoring!");
136  continue;
137  }
138  long long int wayEnds = *(way->end() - 1);
139  long long int wayBegins = *(way->begin());
140  long long int wayPrevEnds = wayPrev != nullptr ? *(wayPrev->end() - 1) : 0;
141  long long int wayPrevBegins = wayPrev != nullptr ? *(wayPrev->begin()) : 0;
142  long long int wayNextEnds = wayNext != nullptr ? *(wayNext->end() - 1) : 0;
143  long long int wayNextBegins = wayNext != nullptr ? *(wayNext->begin()) : 0;
144  if (wayBegins == wayPrevEnds || wayBegins == wayPrevBegins || wayEnds == wayNextBegins
145  || wayEnds == wayNextEnds) {
146  dir = FWD;
147  } else if (wayEnds == wayPrevBegins || wayEnds == wayPrevEnds || wayBegins == wayNextEnds
148  || wayBegins == wayNextBegins) {
149  dir = BWD;
150  } else {
151  WRITE_WARNING("Cannot revise pt stop localization for incomplete pt line: " + myPTLine->getName()
152  + ". Ignoring!");
153  continue;
154  }
155 
156  std::string edgeId = stop->getEdgeId();
157  NBEdge* current = cont.getByID(edgeId);
158  int assingedTo = edgeId.at(0) == '-' ? BWD : FWD;
159 
160  if (dir != assingedTo) {
161  NBEdge* reverse = NBPTStopCont::getReverseEdge(current);
162  if (reverse == nullptr) {
163  WRITE_WARNING("Could not re-assign PT stop: " + stop->getID() + " probably broken osm file");
164  continue;
165  }
166  stop->setEdgeId(reverse->getID());
168  WRITE_WARNING("PT stop: " + stop->getID() + " has been moved to edge: " + reverse->getID());
169  }
170 
171  }
172 }
174 
175 
176  std::vector<NBEdge*> edges;
177 
178  NBNode* first = nullptr;
179  NBNode* last = nullptr;
180  std::vector<NBEdge*> prevWayEdges;
181  std::vector<NBEdge*> prevWayMinusEdges;
182  prevWayEdges.clear();
183  prevWayMinusEdges.clear();
184  std::vector<NBEdge*> currentWayEdges;
185  std::vector<NBEdge*> currentWayMinusEdges;
186  for (auto it3 = pTLine->getMyWays().begin();
187  it3 != pTLine->getMyWays().end(); it3++) {
188 
189  if (cont.retrieve(*it3, false) != nullptr) {
190  currentWayEdges.push_back(cont.retrieve(*it3, false));
191  } else {
192  int i = 0;
193  while (cont.retrieve(*it3 + "#" + std::to_string(i), false) != nullptr) {
194  currentWayEdges.push_back(cont.retrieve(*it3 + "#" + std::to_string(i), false));
195  i++;
196  }
197  }
198 
199  if (cont.retrieve("-" + *it3, false) != nullptr) {
200  currentWayMinusEdges.push_back(cont.retrieve("-" + *it3, false));
201  } else {
202  int i = 0;
203  while (cont.retrieve("-" + *it3 + "#" + std::to_string(i), false) != nullptr) {
204  currentWayMinusEdges.insert(currentWayMinusEdges.begin(),
205  cont.retrieve("-" + *it3 + "#" + std::to_string(i), false));
206  i++;
207  }
208  }
209  if (currentWayEdges.empty()) {
210  continue;
211  }
212  if (last == currentWayEdges.front()->getFromNode() && last != nullptr) {
213  if (!prevWayEdges.empty()) {
214  edges.insert(edges.end(), prevWayEdges.begin(), prevWayEdges.end());
215  prevWayEdges.clear();
216  prevWayMinusEdges.clear();
217  }
218  edges.insert(edges.end(), currentWayEdges.begin(), currentWayEdges.end());
219  last = currentWayEdges.back()->getToNode();
220  } else if (last == currentWayEdges.back()->getToNode() && last != nullptr) {
221  if (!prevWayEdges.empty()) {
222  edges.insert(edges.end(), prevWayEdges.begin(), prevWayEdges.end());
223  prevWayEdges.clear();
224  prevWayMinusEdges.clear();
225  }
226  if (currentWayMinusEdges.empty()) {
227  currentWayEdges.clear();
228  last = nullptr;
229  continue;
230  } else {
231  edges.insert(edges.end(), currentWayMinusEdges.begin(), currentWayMinusEdges.end());
232  last = currentWayMinusEdges.back()->getToNode();
233  }
234  } else if (first == currentWayEdges.front()->getFromNode() && first != nullptr) {
235  edges.insert(edges.end(), prevWayMinusEdges.begin(), prevWayMinusEdges.end());
236  edges.insert(edges.end(), currentWayEdges.begin(), currentWayEdges.end());
237  last = currentWayEdges.back()->getToNode();
238  prevWayEdges.clear();
239  prevWayMinusEdges.clear();
240  } else if (first == currentWayEdges.back()->getToNode() && first != nullptr) {
241  edges.insert(edges.end(), prevWayMinusEdges.begin(), prevWayMinusEdges.end());
242  if (currentWayMinusEdges.empty()) {
243  currentWayEdges.clear();
244  last = nullptr;
245  prevWayEdges.clear();
246  prevWayMinusEdges.clear();
247  continue;
248  } else {
249  edges.insert(edges.end(), currentWayMinusEdges.begin(), currentWayMinusEdges.end());
250  last = currentWayMinusEdges.back()->getToNode();
251  prevWayEdges.clear();
252  prevWayMinusEdges.clear();
253  }
254  } else {
255  if (it3 != pTLine->getMyWays().begin()) {
256  std::cout << "Warning: incomplete route for " << pTLine->getName() << std::endl;
257  }
258  prevWayEdges = currentWayEdges;
259  prevWayMinusEdges = currentWayMinusEdges;
260  if (!prevWayEdges.empty()) {
261  first = prevWayEdges.front()->getFromNode();
262  last = prevWayEdges.back()->getToNode();
263  } else {
264  first = nullptr;
265  last = nullptr;
266  }
267  }
268  currentWayEdges.clear();
269  currentWayMinusEdges.clear();
270  }
271 
272  auto fr = edges.begin();
273  NBPTStop* frStop = pTLine->getStops()[0];
274  for (; fr != edges.end(); fr++) {
275  if ((*fr)->getID() == frStop->getEdgeId()) {
276  break;
277  }
278  }
279  auto to = fr;
280  NBPTStop* toStop = *(pTLine->getStops().end() - 1);
281  for (; to != edges.end(); to++) {
282  if ((*to)->getID() == toStop->getEdgeId()) {
283  to++;
284  break;
285  }
286  }
287 
288  pTLine->addEdgeVector(fr, to);
289 }
290 
std::vector< long long int > * getWaysNodes(std::string wayId)
Definition: NBPTLine.cpp:84
void reviseStops(NBPTLine *myPTLine, NBEdgeCont &cont)
std::string getName()
Definition: NBPTLine.cpp:35
const std::string getEdgeId()
Definition: NBPTStop.cpp:61
The representation of a single edge during network building.
Definition: NBEdge.h:71
static const int BWD
Definition: NBPTLineCont.h:59
The representation of a single pt stop.
Definition: NBPTStop.h:51
static bool findLaneAndComputeBusStopExtend(NBPTStop *pStop, NBEdgeCont &cont)
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
NBEdge * getByID(const std::string &edgeID) const
Returns the edge with id if it exists.
Definition: NBEdgeCont.cpp:842
void process(NBEdgeCont &cont)
const std::string & getID() const
Returns the id.
Definition: Named.h:66
void insert(NBPTLine *pLine)
insert new line
Storage for edges, including some functionality operating on multiple edges.
Definition: NBEdgeCont.h:66
static const int FWD
Definition: NBPTLineCont.h:58
PTLinesCont myPTLines
The map of names to pt lines.
Definition: NBPTLineCont.h:65
long long int myIdCnt
Definition: NBPTLineCont.h:67
~NBPTLineCont()
destructor
NBEdge * retrieve(const std::string &id, bool retrieveExtracted=false) const
Returns the edge that has the given id.
Definition: NBEdgeCont.cpp:251
const std::vector< std::string > & getMyWays() const
Definition: NBPTLine.cpp:81
Represents a single node (junction) during network building.
Definition: NBNode.h:75
std::vector< NBPTStop * > getStops()
Definition: NBPTLine.cpp:38
void constructRoute(NBPTLine *myPTLine, NBEdgeCont &cont)
NBPTLineCont()
constructor
static NBEdge * getReverseEdge(NBEdge *edge)
void setId(long long int id)
Definition: NBPTLine.cpp:69
void addEdgeVector(std::vector< NBEdge * >::iterator fr, std::vector< NBEdge * >::iterator to)
Definition: NBPTLine.cpp:102