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