PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
39 #include "./../../withPoints/src/pgr_withPoints.hpp"
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  /*
91  * storing on C++ containers
92  */
93  std::vector<int64_t> start_vids(
94  start_pidsArr, start_pidsArr + s_len);
95 
96  std::vector<Point_on_edge_t>
97  points(points_p, points_p + total_points);
98 
99  std::vector<pgr_edge_t> edges_to_modify(
100  edges_of_points, edges_of_points + total_edges_of_points);
101 
102  log << "start_vids :";
103  for (const auto v : start_vids) log << v << ", ";
104  /*
105  * checking here is easier than on the C code
106  */
107  int errcode = check_points(points, log);
108  if (errcode) {
109  *log_msg = strdup(log.str().c_str());
110  err << "Unexpected point(s) with same pid"
111  << " but different edge/fraction/side combination found.";
112  *err_msg = pgr_msg(err.str().c_str());
113  return;
114  }
115 
116  std::vector< pgr_edge_t > new_edges;
118  points,
119  edges_to_modify,
120  driving_side,
121  new_edges,
122  log);
123 
124  graphType gType = directed? DIRECTED: UNDIRECTED;
125 
126  std::deque< Path >paths;
127 
128  if (directed) {
129  pgrouting::DirectedGraph digraph(gType);
130  digraph.insert_edges(edges, total_edges);
131  digraph.insert_edges(new_edges);
132  paths = pgr_drivingDistance(
133  digraph, start_vids, distance, equiCost, log);
134  } else {
135  pgrouting::UndirectedGraph undigraph(gType);
136  undigraph.insert_edges(edges, total_edges);
137  undigraph.insert_edges(new_edges);
138  paths = pgr_drivingDistance(
139  undigraph, start_vids, distance, equiCost, log);
140  }
141 
142  for (auto &path : paths) {
143  log << path;
144 
145  if (!details) {
146  eliminate_details_dd(path);
147  }
148  log << path;
149  std::sort(path.begin(), path.end(),
150  [](const Path_t &l, const Path_t &r)
151  {return l.node < r.node;});
152  std::stable_sort(path.begin(), path.end(),
153  [](const Path_t &l, const Path_t &r)
154  {return l.agg_cost < r.agg_cost;});
155  log << path;
156  }
157 
158  size_t count(count_tuples(paths));
159 
160 
161  if (count == 0) {
162  *notice_msg = pgr_msg("No return values was found");
163  return;
164  }
165  *return_tuples = pgr_alloc(count, (*return_tuples));
166  *return_count = collapse_paths(return_tuples, paths);
167  *log_msg = log.str().empty()?
168  *log_msg :
169  pgr_msg(log.str().c_str());
170  *notice_msg = notice.str().empty()?
171  *notice_msg :
172  pgr_msg(notice.str().c_str());
173  } catch (AssertFailedException &except) {
174  (*return_tuples) = pgr_free(*return_tuples);
175  (*return_count) = 0;
176  err << except.what();
177  *err_msg = pgr_msg(err.str().c_str());
178  *log_msg = pgr_msg(log.str().c_str());
179  } catch (std::exception &except) {
180  (*return_tuples) = pgr_free(*return_tuples);
181  (*return_count) = 0;
182  err << except.what();
183  *err_msg = pgr_msg(err.str().c_str());
184  *log_msg = pgr_msg(log.str().c_str());
185  } catch(...) {
186  (*return_tuples) = pgr_free(*return_tuples);
187  (*return_count) = 0;
188  err << "Caught unknown exception!";
189  *err_msg = pgr_msg(err.str().c_str());
190  *log_msg = pgr_msg(log.str().c_str());
191  }
192 }
193 
194 
static edge_t edges[22573]
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.
size_t collapse_paths(General_path_element_t **ret_path, const std::deque< Path > &paths)
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:75
int check_points(std::vector< Point_on_edge_t > &points, std::ostringstream &log)
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
int64_t node
Definition: path_t.h:37
bool create_new_edges(std::vector< Point_on_edge_t > &points, const std::vector< pgr_edge_t > &edges, char driving_side, std::vector< pgr_edge_t > &new_edges)
std::deque< Path > pgr_drivingDistance(G &graph, std::vector< int64_t > start_vids, double distance, bool equicost, std::ostringstream &log)
void eliminate_details_dd(Path &path)
Definition: path_t.h:36
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:29
size_t count_tuples(const std::deque< Path > &paths)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:64
virtual const char * what() const
Definition: pgr_assert.cpp:53
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