pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
dijkstra.hpp
1 /*PGR-GNU*****************************************************************
2 
3 Copyright (c) 2015 pgRouting developers
4 Mail: project@pgrouting.org
5 
6 ------
7 
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 
22 ********************************************************************PGR-GNU*/
23 
24 
25 template <typename G>
26 void process_dijkstra(G &graph, const std::vector<std::string> &tokens) {
27 
28  std::string::size_type sz;
29  if (tokens[1].compare("from") != 0) {
30  std::cout << "missing 'from' kewyword\n";
31  return;
32  }
33 
34  std::vector< int64_t > sources;
35  unsigned int i_ptr = 2;
36 
37  for ( ; i_ptr < tokens.size(); ++i_ptr) {
38  if (tokens[i_ptr].compare("to") == 0) break;
39  try {
40  uint64_t start_vertex(stol(tokens[i_ptr], &sz));
41  sources.push_back(start_vertex);
42  } catch(...) {
43  break;
44  }
45  }
46 
47  if (i_ptr == tokens.size() || tokens[i_ptr].compare("to") != 0) {
48  std::cout << "dijkstra: 'dist' kewyword not found\n";
49  return;
50  }
51 
52  if (sources.size() == 0) {
53  std::cout << "dijkstra: No start value found\n";
54  return;
55  }
56 
57  ++i_ptr;
58  if (i_ptr == tokens.size()) {
59  std::cout << "dijkstra: No 'to' values found\n";
60  return;
61  }
62 
63  std::vector< int64_t > targets;
64  for ( ; i_ptr < tokens.size(); ++i_ptr) {
65  auto end_vertex(stol(tokens[i_ptr], &sz));
66  targets.push_back(end_vertex);
67  }
68 
69 
70  if (sources.size() == 1 && targets.size() == 1) {
71  // one to one
72  Path path;
73  pgr_dijkstra(graph, path, sources[0], targets[0]);
74  std::cout << "THE OPUTPUT ----> total cost: " << path.cost << "\n";
75  path.print_path();
76  path.clear();
77 
78 
79  } else if (sources.size() == 1 && targets.size() > 1){
80  // one to many
81  pgr_dijkstra(graph, paths, sources[0], targets);
82 
83  std::cout << "THE OPUTPUTS ----> total outputs: " << paths.size() << "\n";
84  for (unsigned int i = 0; i < paths.size(); ++i) {
85  if (sizeof(paths[i]) == 0) continue; //no solution found
86  std::cout << "Path #" << i << " cost: " << paths[i].cost << "\n";
87  paths[i].print_path();
88  }
89  } else if (sources.size() > 1 && targets.size() == 1){
90  // many to 1
91  std::deque<Path> paths;
92  pgr_dijkstra(graph, paths, sources, targets[0]);
93 
94 
95  std::cout << "THE OPUTPUTS ----> total outputs: " << paths.size() << "\n";
96  for (unsigned int i = 0; i < paths.size(); ++i) {
97  if (sizeof(paths[i]) == 0) continue; //no solution found
98  std::cout << "Path #" << i << " cost: " << paths[i].cost << "\n";
99  paths[i].print_path();
100  }
101  } else {
102  //many to many
103  std::deque<Path> paths;
104  pgr_dijkstra(graph, paths, sources, targets);
105 
106  std::cout << "THE OPUTPUTS ----> total outputs: " << paths.size() << "\n";
107  for (unsigned int i = 0; i < paths.size(); ++i) {
108  if (sizeof(paths[i]) == 0) continue; //no solution found
109  std::cout << "Path #" << i << " cost: " << paths[i].cost << "\n";
110  paths[i].print_path();
111  }
112  }
113 }