PGROUTING  2.6-dev
withPoints_ksp_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: withPoints_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
10 Mail:
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28 ********************************************************************PGR-GNU*/
29 
31 
32 
33 #include <sstream>
34 #include <deque>
35 #include <vector>
36 
37 #include "yen/pgr_ksp.hpp"
38 
40 #include "cpp_common/pgr_alloc.hpp"
41 #include "cpp_common/pgr_assert.h"
42 
43 
44 // CREATE OR REPLACE FUNCTION pgr_withPointsKSP(
45 // edges_sql TEXT,
46 // points_sql TEXT,
47 // start_pid BIGINT,
48 // end_pid BIGINT,
49 // directed BOOLEAN DEFAULT true
50 
51 
52 int
54  pgr_edge_t *edges, size_t total_edges,
55  Point_on_edge_t *points_p, size_t total_points,
56  pgr_edge_t *edges_of_points, size_t total_edges_of_points,
57  int64_t start_pid,
58  int64_t end_pid,
59  int k,
60  bool directed,
61  bool heap_paths,
62  char driving_side,
63  bool details,
64  General_path_element_t **return_tuples,
65  size_t *return_count,
66  char ** log_msg,
67  char ** notice_msg,
68  char ** err_msg) {
69  std::ostringstream log;
70  std::ostringstream err;
71  std::ostringstream notice;
72  try {
73  pgassert(!(*log_msg));
74  pgassert(!(*notice_msg));
75  pgassert(!(*err_msg));
76  pgassert(!(*return_tuples));
77  pgassert(*return_count == 0);
78  pgassert(total_edges != 0);
79 
81  std::vector<Point_on_edge_t>(
82  points_p,
83  points_p + total_points),
84  std::vector< pgr_edge_t >(
85  edges_of_points,
86  edges_of_points + total_edges_of_points),
87  true,
88  driving_side,
89  directed);
90 
91  if (pg_graph.has_error()) {
92  log << pg_graph.get_log();
93  err << pg_graph.get_error();
94  *log_msg = pgr_msg(log.str().c_str());
95  *err_msg = pgr_msg(err.str().c_str());
96  return -1;
97  }
98 
99 
100  int64_t start_vid(start_pid);
101  int64_t end_vid(end_pid);
102 
103  log << "start_pid" << start_pid << "\n";
104  log << "end_pid" << end_pid << "\n";
105  log << "driving_side" << driving_side << "\n";
106  log << "start_vid" << start_vid << "\n";
107  log << "end_vid" << end_vid << "\n";
108  graphType gType = directed? DIRECTED: UNDIRECTED;
109 
110  std::deque< Path > paths;
111 
112  auto vertices(pgrouting::extract_vertices(edges, total_edges));
113  vertices = pgrouting::extract_vertices(vertices, pg_graph.new_edges());
114 
115  log << "extracted vertices: ";
116  for (const auto v : vertices) {
117  log << v.id << ", ";
118  }
119  log << "\n";
120 
121  if (directed) {
122  log << "Working with directed Graph\n";
123  pgrouting::DirectedGraph digraph(vertices, gType);
124  digraph.insert_edges(edges, total_edges);
125  log << "graph after inserting edges\n";
126  log << digraph << "\n";
127 
128  digraph.insert_edges(pg_graph.new_edges());
129  log << "graph after inserting new edges\n";
130  log << digraph << "\n";
131 
133  paths = fn_yen.Yen(digraph, start_vid, end_vid, k, heap_paths);
134  // pgassert(true==false);
135  } else {
136  log << "Working with undirected Graph\n";
137  pgrouting::UndirectedGraph undigraph(gType);
138  undigraph.insert_edges(edges, total_edges);
139  undigraph.insert_edges(pg_graph.new_edges());
140 
142  paths = fn_yen.Yen(undigraph, start_vid, end_vid, k, heap_paths);
143  }
144 
145 
146  if (!details) {
147  for (auto &path : paths) {
148  path = pg_graph.eliminate_details(path);
149  }
150  }
151 
152  auto count(count_tuples(paths));
153 
154  if (count == 0) {
155  return 0;
156  }
157 
158 
159  *return_tuples = NULL;
160  *return_tuples = pgr_alloc(count, (*return_tuples));
161 
162  size_t sequence = 0;
163  int route_id = 0;
164  for (const auto &path : paths) {
165  if (path.size() > 0)
166  path.get_pg_ksp_path(return_tuples, sequence, route_id);
167  ++route_id;
168  }
169 
170  if (count != sequence) {
171  return 2;
172  }
173  (*return_count) = sequence;
174 
175  *log_msg = log.str().empty()?
176  *log_msg :
177  pgr_msg(log.str().c_str());
178  *notice_msg = notice.str().empty()?
179  *notice_msg :
180  pgr_msg(notice.str().c_str());
181  return 0;
182  } catch (AssertFailedException &except) {
183  (*return_tuples) = pgr_free(*return_tuples);
184  (*return_count) = 0;
185  err << except.what();
186  *err_msg = pgr_msg(err.str().c_str());
187  *log_msg = pgr_msg(log.str().c_str());
188  } catch (std::exception &except) {
189  (*return_tuples) = pgr_free(*return_tuples);
190  (*return_count) = 0;
191  err << except.what();
192  *err_msg = pgr_msg(err.str().c_str());
193  *log_msg = pgr_msg(log.str().c_str());
194  } catch(...) {
195  (*return_tuples) = pgr_free(*return_tuples);
196  (*return_count) = 0;
197  err << "Caught unknown exception!";
198  *err_msg = pgr_msg(err.str().c_str());
199  *log_msg = pgr_msg(log.str().c_str());
200  }
201  return 1000;
202 }
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void insert_edges(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
std::string get_log() const
get_log
int do_pgr_withPointsKsp(pgr_edge_t *edges, size_t total_edges, Point_on_edge_t *points_p, size_t total_points, pgr_edge_t *edges_of_points, size_t total_edges_of_points, int64_t start_pid, int64_t end_pid, int k, bool directed, bool heap_paths, char driving_side, bool details, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Path eliminate_details(Path path) const
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
std::string get_error() const
get_error
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
std::vector< pgr_edge_t > new_edges() const
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
size_t count_tuples(const std::deque< Path > &paths)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
Assertions Handling.
virtual const char * what() const
Definition: pgr_assert.cpp:53
bool has_error() const
get_error
std::deque< Path > Yen(G &graph, int64_t source, int64_t target, int K, bool heap_paths)
Definition: pgr_ksp.hpp:148
graphType
Definition: graph_enum.h:30