PGROUTING  3.2
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
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 
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
Path_t
Definition: path_t.h:36
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
collapse_paths
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
Definition: basePath_SSEC.cpp:310
do_pgr_many_withPointsDD
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)
Definition: withPoints_dd_driver.cpp:59
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
graphType
graphType
Definition: graph_enum.h:30
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
withPoints_dd_driver.h
pgr_dijkstra.hpp
pgr_withPoints.hpp
pgrouting::pgr_drivingDistance
std::deque< Path > pgr_drivingDistance(G &graph, std::vector< int64_t > start_vids, double distance, bool equicost, std::ostringstream &log)
Definition: pgr_dijkstra.hpp:68
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
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139
pgrouting::Pg_points_graph::eliminate_details_dd
void eliminate_details_dd(Path &path) const
Definition: pgr_withPoints.cpp:176