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