PGROUTING  3.2
withPoints_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: one_to_many_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 #include <algorithm>
33 #include <sstream>
34 #include <deque>
35 #include <vector>
36 #include <cassert>
37 #include <limits>
38 
39 
42 
43 #include "cpp_common/pgr_alloc.hpp"
44 #include "cpp_common/pgr_assert.h"
45 
46 template < class G >
47 std::deque< Path >
49  G &graph,
50  std::vector < pgr_combination_t > &combinations,
51  std::vector < int64_t > sources,
52  std::vector < int64_t > targets,
53  bool only_cost,
54  bool normal) {
55  std::sort(sources.begin(), sources.end());
56  sources.erase(
57  std::unique(sources.begin(), sources.end()),
58  sources.end());
59 
60  std::sort(targets.begin(), targets.end());
61  targets.erase(
62  std::unique(targets.begin(), targets.end()),
63  targets.end());
64 
65  pgrouting::Pgr_dijkstra< G > fn_dijkstra;
66  size_t n_goals = (std::numeric_limits<size_t>::max)();
67  auto paths = combinations.empty()?
68  fn_dijkstra.dijkstra(graph, sources, targets, only_cost)
69  : fn_dijkstra.dijkstra(graph, combinations, only_cost, n_goals);
70 
71  if (!normal) {
72  for (auto &path : paths) {
73  path.reverse();
74  }
75  }
76  if (!only_cost) {
77  for (auto &p : paths) {
78  p.recalculate_agg_cost();
79  }
80  }
81  return paths;
82 }
83 
84 
85 // CREATE OR REPLACE FUNCTION pgr_withPoint(
86 // edges_sql TEXT,
87 // points_sql TEXT,
88 // start_pid ANYARRAY,
89 // end_pid BIGINT,
90 // directed BOOLEAN DEFAULT true
91 
92 void
94  pgr_edge_t *edges, size_t total_edges,
95  Point_on_edge_t *points_p, size_t total_points,
96  pgr_edge_t *edges_of_points, size_t total_edges_of_points,
97 
98  pgr_combination_t *combinations, size_t total_combinations,
99 
100  int64_t *start_pidsArr, size_t size_start_pidsArr,
101  int64_t *end_pidsArr, size_t size_end_pidsArr,
102 
103  char driving_side,
104  bool details,
105  bool directed,
106  bool only_cost,
107  bool normal,
108 
109  General_path_element_t **return_tuples, size_t *return_count,
110 
111  char** log_msg,
112  char** notice_msg,
113  char** err_msg) {
114  std::ostringstream log;
115  std::ostringstream notice;
116  std::ostringstream err;
117  try {
118  pgassert(!(*log_msg));
119  pgassert(!(*notice_msg));
120  pgassert(!(*err_msg));
121  pgassert(!(*return_tuples));
122  pgassert((*return_count) == 0);
123  pgassert(edges || edges_of_points);
124 
126  std::vector<Point_on_edge_t>(
127  points_p,
128  points_p + total_points),
129  std::vector< pgr_edge_t >(
130  edges_of_points,
131  edges_of_points + total_edges_of_points),
132  normal,
133  driving_side,
134  directed);
135 
136  if (pg_graph.has_error()) {
137  log << pg_graph.get_log();
138  err << pg_graph.get_error();
139  *log_msg = pgr_msg(log.str().c_str());
140  *err_msg = pgr_msg(err.str().c_str());
141  return;
142  }
143 
144 
145  std::vector<pgr_combination_t>
146  combinations_vector(combinations, combinations + total_combinations);
147  std::vector<int64_t>
148  start_vertices(start_pidsArr, start_pidsArr + size_start_pidsArr);
149  std::vector< int64_t >
150  end_vertices(end_pidsArr, end_pidsArr + size_end_pidsArr);
151 
152  auto vertices(pgrouting::extract_vertices(edges, total_edges));
153  vertices = pgrouting::extract_vertices(vertices, pg_graph.new_edges());
154 
155  graphType gType = directed? DIRECTED: UNDIRECTED;
156 
157  std::deque< Path > paths;
158 
159  if (directed) {
160  log << "Working with directed Graph\n";
161  pgrouting::DirectedGraph digraph(vertices, gType);
162  digraph.insert_edges(edges, total_edges);
163  digraph.insert_edges(pg_graph.new_edges());
164 
165  paths = pgr_dijkstra(
166  digraph,
167  combinations_vector,
168  start_vertices, end_vertices,
169  only_cost, normal);
170  } else {
171  log << "Working with Undirected Graph\n";
172  pgrouting::UndirectedGraph undigraph(vertices, gType);
173  undigraph.insert_edges(edges, total_edges);
174  undigraph.insert_edges(pg_graph.new_edges());
175  paths = pgr_dijkstra(
176  undigraph,
177  combinations_vector,
178  start_vertices, end_vertices,
179  only_cost, normal);
180  }
181 
182  if (!details) {
183  for (auto &path : paths) {
184  path = pg_graph.eliminate_details(path);
185  }
186  }
187 
188  /*
189  * order paths based on the start_pid, end_pid
190  */
191  std::sort(paths.begin(), paths.end(),
192  [](const Path &a, const Path &b)
193  -> bool {
194  if (b.start_id() != a.start_id()) {
195  return a.start_id() < b.start_id();
196  }
197  return a.end_id() < b.end_id();
198  });
199 
200  size_t count(0);
201  count = count_tuples(paths);
202 
203 
204  if (count == 0) {
205  (*return_tuples) = NULL;
206  (*return_count) = 0;
207  return;
208  }
209 
210  (*return_tuples) = pgr_alloc(count, (*return_tuples));
211  log << "Converting a set of paths into the tuples\n";
212  (*return_count) = (collapse_paths(return_tuples, paths));
213 
214  log << "************************************************";
215  log << pg_graph.get_log();
216  log << "************************************************";
217 
218  *log_msg = log.str().empty()?
219  *log_msg :
220  pgr_msg(log.str().c_str());
221  *notice_msg = notice.str().empty()?
222  *notice_msg :
223  pgr_msg(notice.str().c_str());
224  } catch (AssertFailedException &except) {
225  (*return_tuples) = pgr_free(*return_tuples);
226  (*return_count) = 0;
227  err << except.what();
228  *err_msg = pgr_msg(err.str().c_str());
229  *log_msg = pgr_msg(log.str().c_str());
230  } catch (std::exception &except) {
231  (*return_tuples) = pgr_free(*return_tuples);
232  (*return_count) = 0;
233  err << except.what();
234  *err_msg = pgr_msg(err.str().c_str());
235  *log_msg = pgr_msg(log.str().c_str());
236  } catch(...) {
237  (*return_tuples) = pgr_free(*return_tuples);
238  (*return_count) = 0;
239  err << "Caught unknown exception!";
240  *err_msg = pgr_msg(err.str().c_str());
241  *log_msg = pgr_msg(log.str().c_str());
242  }
243 }
Path::end_id
int64_t end_id() const
Definition: basePath_SSEC.hpp:67
Path
Definition: basePath_SSEC.hpp:47
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::Pg_points_graph
Definition: pgr_withPoints.hpp:40
Path::reverse
void reverse()
Definition: basePath_SSEC.cpp:57
collapse_paths
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:310
pgr_dijkstra
std::deque< Path > pgr_dijkstra(G &graph, std::vector< pgr_combination_t > &combinations, std::vector< int64_t > sources, std::vector< int64_t > targets, bool only_cost, bool normal)
Definition: withPoints_driver.cpp:48
pgrouting::Pg_points_graph::eliminate_details
Path eliminate_details(Path path) const
Definition: pgr_withPoints.cpp:229
do_pgr_withPoints
void do_pgr_withPoints(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, pgr_combination_t *combinations, size_t total_combinations, int64_t *start_pidsArr, size_t size_start_pidsArr, int64_t *end_pidsArr, size_t size_end_pidsArr, char driving_side, bool details, bool directed, bool only_cost, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: withPoints_driver.cpp:93
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
withPoints_driver.h
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
pgr_combination_t
Definition: pgr_combination_t.h:43
pgrouting::Pg_points_graph::new_edges
std::vector< pgr_edge_t > new_edges() const
Definition: pgr_withPoints.cpp:323
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_dijkstra.hpp
pgr_withPoints.hpp
DIRECTED
@ DIRECTED
Definition: graph_enum.h:30
General_path_element_t
Definition: general_path_element_t.h:37
pgrouting::alphashape::G
graph::Pgr_base_graph< BG, XY_vertex, Basic_edge > G
Definition: pgr_alphaShape.h:56
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
pgrouting::Pgr_dijkstra
Definition: pgr_dijkstra.hpp:62
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139
pgrouting::Pgr_dijkstra::dijkstra
Path dijkstra(G &graph, int64_t start_vertex, int64_t end_vertex, bool only_cost=false)
Dijkstra 1 to 1.
Definition: pgr_dijkstra.hpp:172