pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
ksp_driver.cpp
1 /*PGR-GNU*****************************************************************
2 File: ksp_driver.cpp
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 vicky_vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 ********************************************************************PGR-GNU*/
24 
25 #ifdef __MINGW32__
26 #include <winsock2.h>
27 #include <windows.h>
28 #ifdef unlink
29 #undef unlink
30 #endif
31 #endif
32 
33 
34 #include <deque>
35 #include <sstream>
36 
37 
38 #include "./ksp_driver.h"
39 #include "../../common/src/memory_func.hpp"
40 #include "./pgr_ksp.hpp"
41 
42 
43 
44 int do_pgr_ksp(
45  pgr_edge_t *data_edges, size_t total_tuples,
46  int64_t start_vertex, int64_t end_vertex,
47  int k, bool directedFlag, bool heap_paths,
48  General_path_element_t **ksp_path, size_t *path_count,
49  char ** err_msg) {
50  try {
51  std::ostringstream log;
52 
53  graphType gType = directedFlag? DIRECTED: UNDIRECTED;
54  const auto initial_size = total_tuples;
55 
56  std::deque< Path > paths;
57 
58  if (directedFlag) {
59  Pgr_base_graph< DirectedGraph > digraph(gType, initial_size);
61  digraph.graph_insert_data(data_edges, initial_size);
62  paths = fn_yen.Yen(digraph, start_vertex, end_vertex, k, heap_paths);
63  } else {
64  Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size);
66  undigraph.graph_insert_data(data_edges, initial_size);
67  paths = fn_yen.Yen(undigraph, start_vertex, end_vertex, k, heap_paths);
68  }
69 
70 
71  auto count(count_tuples(paths));
72 
73  if (count == 0) {
74  *err_msg = strdup(
75  "NOTICE: No paths found between Starting and Ending vertices");
76  *ksp_path = noResult(path_count, (*ksp_path));
77  return 0;
78  }
79 
80  // get the space required to store all the paths
81  *ksp_path = NULL;
82  *ksp_path = get_memory(count, (*ksp_path));
83 
84  size_t sequence = 0;
85  int route_id = 0;
86  for (const auto &path : paths) {
87  if (path.size() > 0)
88  path.get_pg_ksp_path(ksp_path, sequence, route_id);
89  ++route_id;
90  }
91 
92  if (count != sequence) {
93  *err_msg = NULL;
94  return 2;
95  }
96  *path_count = count;
97 
98 #if 1
99  *err_msg = strdup("OK");
100 #else
101  *err_msg = strdup(log.str().c_str());
102 #endif
103  return EXIT_SUCCESS;
104  } catch ( ... ) {
105  *err_msg = strdup("Caught unknown expection!");
106  return -1;
107  }
108 }
109