pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
ksp.cpp
1 
2 
3 template <typename G>
4 void process_ksp(G &graph, const std::vector<std::string> &tokens) {
5 
6  std::string::size_type sz;
7  if (tokens[1].compare("from") != 0) {
8  std::cout << "ksp: missing 'from' kewyword\n";
9  return;
10  }
11 
12  std::vector< int64_t > sources;
13  unsigned int i_ptr = 2;
14 
15  for ( ; i_ptr < tokens.size(); ++i_ptr) {
16  if (tokens[i_ptr].compare("to") == 0) break;
17  try {
18  uint64_t start_vertex(stol(tokens[i_ptr], &sz));
19  sources.push_back(start_vertex);
20  } catch(...) {
21  break;
22  }
23  }
24 
25  if (i_ptr == tokens.size() || tokens[i_ptr].compare("to") != 0) {
26  std::cout << "ksp: 'to' kewyword not found\n";
27  return;
28  }
29 
30  if (sources.size() == 0) {
31  std::cout << "ksp: No start value found\n";
32  return;
33  }
34 
35  ++i_ptr;
36  if (i_ptr == tokens.size()) {
37  std::cout << "ksp: No 'to' values found\n";
38  return;
39  }
40 
41  std::vector< int64_t > targets;
42  for ( ; i_ptr < tokens.size(); ++i_ptr) {
43  auto end_vertex(stol(tokens[i_ptr], &sz));
44  targets.push_back(end_vertex);
45  }
46 
47  Pgr_ksp< G > ksp;
48 
49  if (sources.size() == 1 && targets.size() == 1) {
50  // one to one
51  std::deque< Path > paths;
52  paths = ksp.Yen(graph, sources[0], targets[0], 3); //TODO make variable
53  std::cout << "THE OPUTPUT ----" << "\n";
54  for (unsigned int i = 0; i < paths.size(); ++i) {
55  if (sizeof(paths[i]) == 0) continue; //no solution found
56  std::cout << "Path #" << i << " cost: " << paths[i].cost << "\n";
57  paths[i].print_path();
58  }
59  } else {
60  std::cout << "ksp: unknown number of arguments\n";
61  }
62 #if 0 // ksp is only one to 1
63  else if (sources.size() == 1 && targets.size() > 1){
64  // one to many
65  std::deque<Path> paths;
66  dijkstra.dijkstra(graph, paths, sources[0], targets);
67 
68  std::cout << "THE OPUTPUTS ----> total outputs: " << paths.size() << "\n";
69  for (unsigned int i = 0; i < paths.size(); ++i) {
70  if (sizeof(paths[i]) == 0) continue; //no solution found
71  std::cout << "Path #" << i << " cost: " << paths[i].cost << "\n";
72  paths[i].print_path();
73  }
74  } else if (sources.size() > 1 && targets.size() == 1){
75  // many to 1
76  std::deque<Path> paths;
77  dijkstra.dijkstra(graph, paths, sources, targets[0]);
78 
79 
80  std::cout << "THE OPUTPUTS ----> total outputs: " << paths.size() << "\n";
81  for (unsigned int i = 0; i < paths.size(); ++i) {
82  if (sizeof(paths[i]) == 0) continue; //no solution found
83  std::cout << "Path #" << i << " cost: " << paths[i].cost << "\n";
84  paths[i].print_path();
85  }
86  } else {
87  //many to many
88  std::deque<Path> paths;
89  dijkstra.dijkstra(graph, paths, sources, targets);
90  std::cout << "THE OPUTPUTS ----> total outputs: " << paths.size() << "\n";
91  for (unsigned int i = 0; i < paths.size(); ++i) {
92  if (sizeof(paths[i]) == 0) continue; //no solution found
93  std::cout << "Path #" << i << " cost: " << paths[i].cost << "\n";
94  paths[i].print_path();
95  }
96  }
97 #endif
98 
99 }