pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ksp_driver.cpp
Go to the documentation of this file.
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 #if defined(__MINGW32__) || defined(_MSC_VER)
26 #include <winsock2.h>
27 #include <windows.h>
28 #ifdef open
29 #undef open
30 #endif
31 #ifdef unlink
32 #undef unlink
33 #endif
34 #endif
35 
36 #include <deque>
37 #include <sstream>
38 
39 
40 #include "./ksp_driver.h"
41 #include "../../common/src/pgr_alloc.hpp"
42 #include "./pgr_ksp.hpp"
43 
44 
45 
47  pgr_edge_t *data_edges, size_t total_tuples,
48  int64_t start_vertex, int64_t end_vertex,
49  int k, bool directedFlag, bool heap_paths,
50  General_path_element_t **ksp_path, size_t *path_count,
51  char ** err_msg) {
52  try {
53  *ksp_path = NULL;
54  *path_count = 0;
55  std::ostringstream log;
56 
57  graphType gType = directedFlag? DIRECTED: UNDIRECTED;
58 
59  std::deque< Path > paths;
60 
61  if (directedFlag) {
62  pgrouting::DirectedGraph digraph(gType);
64  digraph.graph_insert_data(data_edges, total_tuples);
65  paths = fn_yen.Yen(digraph, start_vertex, end_vertex, k, heap_paths);
66  } else {
67  pgrouting::UndirectedGraph undigraph(gType);
69  undigraph.graph_insert_data(data_edges, total_tuples);
70  paths = fn_yen.Yen(undigraph, start_vertex, end_vertex, k, heap_paths);
71  }
72 
73 
74  auto count(count_tuples(paths));
75 
76  if (count == 0) {
77  *err_msg = strdup(
78  "NOTICE: No paths found between Starting and Ending vertices");
79  return 0;
80  }
81 
82  // get the space required to store all the paths
83  *ksp_path = NULL;
84  *ksp_path = pgr_alloc(count, (*ksp_path));
85 
86  size_t sequence = 0;
87  int route_id = 0;
88  for (const auto &path : paths) {
89  if (path.size() > 0)
90  path.get_pg_ksp_path(ksp_path, sequence, route_id);
91  ++route_id;
92  }
93 
94  if (count != sequence) {
95  *err_msg = NULL;
96  return 2;
97  }
98  *path_count = count;
99 
100 #if 1
101  *err_msg = strdup("OK");
102 #else
103  *err_msg = strdup(log.str().c_str());
104 #endif
105  return EXIT_SUCCESS;
106  } catch ( ... ) {
107  *err_msg = strdup("Caught unknown exception!");
108  if (ksp_path) free(ksp_path);
109  *path_count = 0;
110  return -1;
111  }
112 }
113 
int path_count
Definition: BDATester.cpp:51
graphType
Definition: pgr_types.h:192
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:52
path_element_t * path
Definition: BDATester.cpp:49
std::deque< Path > Yen(G &graph, int64_t source, int64_t target, int K, bool heap_paths)
Definition: pgr_ksp.cpp:56
char * err_msg
Definition: BDATester.cpp:50
int do_pgr_ksp(pgr_edge_t *data_edges, size_t total_tuples, int64_t start_vertex, int64_t end_vertex, int k, bool directedFlag, bool heap_paths, General_path_element_t **ksp_path, size_t *path_count, char **err_msg)
Definition: ksp_driver.cpp:46
void graph_insert_data(const T *edges, int64_t count)
Inserts count edges of type T into the graph.