PGROUTING  2.6
withPoints_dd_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 
30 
32 
33 #include <sstream>
34 #include <deque>
35 #include <vector>
36 #include <algorithm>
37 
40 
41 
42 #include "cpp_common/pgr_alloc.hpp"
43 
44 
45 /**********************************************************************/
46 // CREATE OR REPLACE FUNCTION pgr_withPointsDD(
47 // edges_sql TEXT,
48 // points_sql TEXT,
49 // start_pids anyarray,
50 // distance FLOAT,
51 //
52 // driving_side CHAR -- DEFAULT 'b',
53 // details BOOLEAN -- DEFAULT false,
54 // directed BOOLEAN -- DEFAULT true,
55 // equicost BOOLEAN -- DEFAULT false,
56 
57 
58 void
60  pgr_edge_t *edges, size_t total_edges,
61  Point_on_edge_t *points_p, size_t total_points,
62  pgr_edge_t *edges_of_points, size_t total_edges_of_points,
63 
64  int64_t *start_pidsArr, size_t s_len,
65  double distance,
66 
67  bool directed,
68  char driving_side,
69  bool details,
70  bool equiCost,
71 
72  General_path_element_t **return_tuples, size_t *return_count,
73  char** log_msg,
74  char** notice_msg,
75  char** err_msg) {
76  std::ostringstream log;
77  std::ostringstream notice;
78  std::ostringstream err;
79  try {
80  pgassert(!(*log_msg));
81  pgassert(!(*notice_msg));
82  pgassert(!(*err_msg));
83  pgassert(!(*return_tuples));
84  pgassert((*return_count) == 0);
85  pgassert(edges);
86  pgassert(points_p);
87  pgassert(edges_of_points);
88  pgassert(start_pidsArr);
89 
90 
92  std::vector<Point_on_edge_t>(
93  points_p,
94  points_p + total_points),
95  std::vector< pgr_edge_t >(
96  edges_of_points,
97  edges_of_points + total_edges_of_points),
98  true,
99  driving_side,
100  directed);
101 
102  if (pg_graph.has_error()) {
103  log << pg_graph.get_log();
104  err << pg_graph.get_error();
105  *log_msg = pgr_msg(log.str().c_str());
106  *err_msg = pgr_msg(err.str().c_str());
107  return;
108  }
109 
110 
111 
112  /*
113  * storing on C++ containers
114  */
115  std::vector<int64_t> start_vids(
116  start_pidsArr, start_pidsArr + s_len);
117 
118 
119  graphType gType = directed? DIRECTED: UNDIRECTED;
120 
121  std::deque< Path >paths;
122 
123  if (directed) {
124  pgrouting::DirectedGraph digraph(gType);
125  digraph.insert_edges(edges, total_edges);
126  digraph.insert_edges(pg_graph.new_edges());
127  paths = pgr_drivingDistance(
128  digraph, start_vids, distance, equiCost, log);
129  } else {
130  pgrouting::UndirectedGraph undigraph(gType);
131  undigraph.insert_edges(edges, total_edges);
132  undigraph.insert_edges(pg_graph.new_edges());
133 
134  paths = pgr_drivingDistance(
135  undigraph, start_vids, distance, equiCost, log);
136  }
137 
138  for (auto &path : paths) {
139  log << path;
140 
141  if (!details) {
142  pg_graph.eliminate_details_dd(path);
143  }
144  log << path;
145  std::sort(path.begin(), path.end(),
146  [](const Path_t &l, const Path_t &r)
147  {return l.node < r.node;});
148  std::stable_sort(path.begin(), path.end(),
149  [](const Path_t &l, const Path_t &r)
150  {return l.agg_cost < r.agg_cost;});
151  log << path;
152  }
153 
154  size_t count(count_tuples(paths));
155 
156 
157  if (count == 0) {
158  *notice_msg = pgr_msg("No return values was found");
159  return;
160  }
161  *return_tuples = pgr_alloc(count, (*return_tuples));
162  *return_count = collapse_paths(return_tuples, paths);
163  *log_msg = log.str().empty()?
164  *log_msg :
165  pgr_msg(log.str().c_str());
166  *notice_msg = notice.str().empty()?
167  *notice_msg :
168  pgr_msg(notice.str().c_str());
169  } catch (AssertFailedException &except) {
170  (*return_tuples) = pgr_free(*return_tuples);
171  (*return_count) = 0;
172  err << except.what();
173  *err_msg = pgr_msg(err.str().c_str());
174  *log_msg = pgr_msg(log.str().c_str());
175  } catch (std::exception &except) {
176  (*return_tuples) = pgr_free(*return_tuples);
177  (*return_count) = 0;
178  err << except.what();
179  *err_msg = pgr_msg(err.str().c_str());
180  *log_msg = pgr_msg(log.str().c_str());
181  } catch(...) {
182  (*return_tuples) = pgr_free(*return_tuples);
183  (*return_count) = 0;
184  err << "Caught unknown exception!";
185  *err_msg = pgr_msg(err.str().c_str());
186  *log_msg = pgr_msg(log.str().c_str());
187  }
188 }
189 
190 
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)
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
int64_t node
Definition: path_t.h:37
std::deque< Path > pgr_drivingDistance(G &graph, std::vector< int64_t > start_vids, double distance, bool equicost, std::ostringstream &log)
Definition: path_t.h:36
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)
void eliminate_details_dd(Path &path) const
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
virtual const char * what() const
Definition: pgr_assert.cpp:53
bool has_error() const
get_error
void do_pgr_many_withPointsDD(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 s_len, double distance, bool directed, char driving_side, bool details, bool equiCost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
graphType
Definition: graph_enum.h:30
double agg_cost
Definition: path_t.h:40