PGROUTING  3.2
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
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 
44 
45 // CREATE OR REPLACE FUNCTION pgr_withPointsKSP(
46 // edges_sql TEXT,
47 // points_sql TEXT,
48 // start_pid BIGINT,
49 // end_pid BIGINT,
50 // directed BOOLEAN DEFAULT true
51 
52 
53 int
55  pgr_edge_t *edges, size_t total_edges,
56  Point_on_edge_t *points_p, size_t total_points,
57  pgr_edge_t *edges_of_points, size_t total_edges_of_points,
58  int64_t start_pid,
59  int64_t end_pid,
60  size_t k,
61  bool directed,
62  bool heap_paths,
63  char driving_side,
64  bool details,
65  General_path_element_t **return_tuples,
66  size_t *return_count,
67  char ** log_msg,
68  char ** notice_msg,
69  char ** err_msg) {
70  std::ostringstream log;
71  std::ostringstream err;
72  std::ostringstream notice;
73  try {
74  pgassert(!(*log_msg));
75  pgassert(!(*notice_msg));
76  pgassert(!(*err_msg));
77  pgassert(!(*return_tuples));
78  pgassert(*return_count == 0);
79  pgassert(total_edges != 0);
80 
82  std::vector<Point_on_edge_t>(
83  points_p,
84  points_p + total_points),
85  std::vector< pgr_edge_t >(
86  edges_of_points,
87  edges_of_points + total_edges_of_points),
88  true,
89  driving_side,
90  directed);
91 
92  if (pg_graph.has_error()) {
93  log << pg_graph.get_log();
94  err << pg_graph.get_error();
95  *log_msg = pgr_msg(log.str().c_str());
96  *err_msg = pgr_msg(err.str().c_str());
97  return -1;
98  }
99 
100 
101  int64_t start_vid(start_pid);
102  int64_t end_vid(end_pid);
103 
104  log << "start_pid" << start_pid << "\n";
105  log << "end_pid" << end_pid << "\n";
106  log << "driving_side" << driving_side << "\n";
107  log << "start_vid" << start_vid << "\n";
108  log << "end_vid" << end_vid << "\n";
109  graphType gType = directed? DIRECTED: UNDIRECTED;
110 
111  std::deque< Path > paths;
112 
113  auto vertices(pgrouting::extract_vertices(edges, total_edges));
114  vertices = pgrouting::extract_vertices(vertices, pg_graph.new_edges());
115 
116  log << "extracted vertices: ";
117  for (const auto& v : vertices) {
118  log << v.id << ", ";
119  }
120  log << "\n";
121 
122  if (directed) {
123  log << "Working with directed Graph\n";
124  pgrouting::DirectedGraph digraph(vertices, gType);
125  digraph.insert_edges(edges, total_edges);
126  log << "graph after inserting edges\n";
127  log << digraph << "\n";
128 
129  digraph.insert_edges(pg_graph.new_edges());
130  log << "graph after inserting new edges\n";
131  log << digraph << "\n";
132 
134  paths = fn_yen.Yen(digraph, start_vid, end_vid, k, heap_paths);
135  // pgassert(true==false);
136  } else {
137  log << "Working with undirected Graph\n";
138  pgrouting::UndirectedGraph undigraph(gType);
139  undigraph.insert_edges(edges, total_edges);
140  undigraph.insert_edges(pg_graph.new_edges());
141 
143  paths = fn_yen.Yen(undigraph, start_vid, end_vid, k, heap_paths);
144  }
145 
146 
147  if (!details) {
148  for (auto &path : paths) {
149  path = pg_graph.eliminate_details(path);
150  }
151  }
152 
153  auto count(count_tuples(paths));
154 
155  if (count == 0) {
156  return 0;
157  }
158 
159 
160  *return_tuples = NULL;
161  *return_tuples = pgr_alloc(count, (*return_tuples));
162 
163  size_t sequence = 0;
164  int route_id = 0;
165  for (const auto &path : paths) {
166  if (path.size() > 0)
167  path.get_pg_ksp_path(return_tuples, sequence, route_id);
168  ++route_id;
169  }
170 
171  if (count != sequence) {
172  return 2;
173  }
174  (*return_count) = sequence;
175 
176  *log_msg = log.str().empty()?
177  *log_msg :
178  pgr_msg(log.str().c_str());
179  *notice_msg = notice.str().empty()?
180  *notice_msg :
181  pgr_msg(notice.str().c_str());
182  return 0;
183  } catch (AssertFailedException &except) {
184  (*return_tuples) = pgr_free(*return_tuples);
185  (*return_count) = 0;
186  err << except.what();
187  *err_msg = pgr_msg(err.str().c_str());
188  *log_msg = pgr_msg(log.str().c_str());
189  } catch (std::exception &except) {
190  (*return_tuples) = pgr_free(*return_tuples);
191  (*return_count) = 0;
192  err << except.what();
193  *err_msg = pgr_msg(err.str().c_str());
194  *log_msg = pgr_msg(log.str().c_str());
195  } catch(...) {
196  (*return_tuples) = pgr_free(*return_tuples);
197  (*return_count) = 0;
198  err << "Caught unknown exception!";
199  *err_msg = pgr_msg(err.str().c_str());
200  *log_msg = pgr_msg(log.str().c_str());
201  }
202  return 1000;
203 }
pgr_ksp.hpp
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
count_tuples
size_t count_tuples(const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:387
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
pgrouting::yen::Pgr_ksp::Yen
std::deque< Path > Yen(G &graph, int64_t start_vertex, int64_t end_vertex, size_t K, bool heap_paths)
Definition: pgr_ksp.hpp:61
pgrouting::Pg_points_graph
Definition: pgr_withPoints.hpp:40
withPoints_ksp_driver.h
pgrouting::Pg_points_graph::eliminate_details
Path eliminate_details(Path path) const
Definition: pgr_withPoints.cpp:229
pgrouting::Pgr_messages::get_error
std::string get_error() const
get_error
Definition: pgr_messages.cpp:53
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
UNDIRECTED
@ UNDIRECTED
Definition: graph_enum.h:30
pgrouting::graph::Pgr_base_graph::insert_edges
void insert_edges(const T *edges, size_t count)
Inserts count edges of type T into the graph.
Definition: pgr_base_graph.hpp:357
pgr_alloc.hpp
pgrouting::Pgr_messages::get_log
std::string get_log() const
get_log
Definition: pgr_messages.cpp:36
pgrouting::Pg_points_graph::new_edges
std::vector< pgr_edge_t > new_edges() const
Definition: pgr_withPoints.cpp:323
pgrouting::yen::Pgr_ksp
Definition: pgr_ksp.hpp:47
do_pgr_withPointsKsp
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, size_t 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)
Definition: withPoints_ksp_driver.cpp:54
graphType
graphType
Definition: graph_enum.h:30
pgr_assert.h
An assert functionality that uses C++ throw().
pgrouting::Pgr_messages::has_error
bool has_error() const
get_error
Definition: pgr_messages.cpp:48
Point_on_edge_t
Definition: point_on_edge_t.h:37
pgr_withPoints.hpp
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
General_path_element_t
Definition: general_path_element_t.h:37
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
pgrouting::graph::Pgr_base_graph
Definition: pgr_base_graph.hpp:168
pgrouting::extract_vertices
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
Definition: basic_vertex.cpp:59
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139