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