pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
one_to_many_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 
30 
31 #if defined(__MINGW32__) || defined(_MSC_VER)
32 #include <winsock2.h>
33 #include <windows.h>
34 #endif
35 
36 
37 #include <set>
38 #include <algorithm>
39 #include <sstream>
40 #include <deque>
41 #include <vector>
42 #include <cassert>
43 
44 
45 #include "./pgr_dijkstra.hpp"
46 #include "./pgr_withPoints.hpp"
48 
49 #include "./../../common/src/basic_vertex.h"
50 #include "./../../common/src/pgr_alloc.hpp"
51 #include "./../../common/src/pgr_assert.h"
52 
53 // CREATE OR REPLACE FUNCTION pgr_withPoint(
54 // edges_sql TEXT,
55 // points_sql TEXT,
56 // start_pid BIGINT,
57 // end_pid BIGINT,
58 // directed BOOLEAN DEFAULT true
59 
60 
61 void
63  pgr_edge_t *edges, size_t total_edges,
64  Point_on_edge_t *points_p, size_t total_points,
65  pgr_edge_t *edges_of_points, size_t total_edges_of_points,
66  int64_t start_vid,
67  int64_t *end_pidsArr, size_t size_end_pidsArr,
68  char driving_side,
69  bool details,
70  bool directed,
71  bool only_cost,
72  General_path_element_t **return_tuples, size_t *return_count,
73  char ** log_msg,
74  char ** err_msg) {
75  std::ostringstream log;
76  std::ostringstream err;
77  try {
78  pgassert(!(*return_tuples));
79  pgassert((*return_count) == 0);
80  pgassert(!(*log_msg));
81  pgassert(!(*err_msg));
82 
83  /*
84  * DOCUMENT:
85  * - Points are treated as the same point when the pid is the same
86  * therefore when two points have the same pid, but different edge/fraction
87  * an error is generated.
88  */
89  std::vector< Point_on_edge_t >
90  points(points_p, points_p + total_points);
91 
92  int errcode = check_points(points, log);
93  if (errcode) {
94  *log_msg = strdup(log.str().c_str());
95  err << "Unexpected point(s) with same pid but different edge/fraction/side combination found.";
96  *err_msg = strdup(err.str().c_str());
97  return;
98  }
99 
100 
101  std::vector< pgr_edge_t >
102  edges_to_modify(edges_of_points, edges_of_points + total_edges_of_points);
103 
104  std::vector< pgr_edge_t > new_edges;
105 
106  log << "driving_side" << driving_side << "\n";
108  points,
109  edges_to_modify,
110  driving_side,
111  new_edges);
112 
113  log << "Inserting points into a c++ vector structure\n";
114  /*
115  * Eliminating duplicates
116  * & ordering the points
117  */
118  std::set< int64_t > s_end_vertices(end_pidsArr, end_pidsArr + size_end_pidsArr);
119 
120  std::vector< int64_t > end_vertices(s_end_vertices.begin(), s_end_vertices.end());
121 
122  log << "start_vid" << start_vid << "\n";
123  log << "end_vertices";
124 
125  for (const auto &vid : end_vertices) {
126  log << vid << "\n";
127  }
128 
129  graphType gType = directed? DIRECTED: UNDIRECTED;
130 
131  std::deque< Path > paths;
132 
133 
134  if (directed) {
135  log << "Working with directed Graph\n";
136  pgrouting::DirectedGraph digraph(
138  pgrouting::extract_vertices(edges, total_edges),
139  new_edges),
140  gType);
141  digraph.graph_insert_data(edges, total_edges);
142  digraph.graph_insert_data(new_edges);
143  pgr_dijkstra(digraph, paths, start_vid, end_vertices, only_cost);
144  } else {
145  log << "Working with Undirected Graph\n";
146  auto vertices(pgrouting::extract_vertices(edges, total_edges));
147  vertices = pgrouting::extract_vertices(vertices, new_edges);
148  pgrouting::UndirectedGraph undigraph(vertices, gType);
149  vertices.clear();
150  undigraph.graph_insert_data(edges, total_edges);
151  undigraph.graph_insert_data(new_edges);
152  pgr_dijkstra(undigraph, paths, start_vid, end_vertices, only_cost);
153  }
154 
155  if (!details) {
156  for (auto &path : paths) {
157  eliminate_details(path, edges_to_modify);
158  }
159  }
160  /*
161  * order paths based on the end_pid
162  */
163  std::sort(paths.begin(), paths.end(), [](const Path &a, const Path &b) {
164  return a.end_id() < b.end_id();
165  });
166 
167  size_t count(0);
168  count = count_tuples(paths);
169 
170 
171  if (count == 0) {
172  (*return_tuples) = NULL;
173  (*return_count) = 0;
174  log <<
175  "No paths found between Starting and any of the Ending vertices\n";
176  *log_msg = strdup(log.str().c_str());
177  return;
178  }
179 
180  (*return_tuples) = pgr_alloc(count, (*return_tuples));
181  log << "Converting a set of paths into the tuples\n";
182  (*return_count) = (collapse_paths(return_tuples, paths));
183 
184 
185 
186  *log_msg = strdup(log.str().c_str());
187  pgassert(!(*err_msg));
188  return;
189  } catch (AssertFailedException &except) {
190  if (*return_tuples) free(*return_tuples);
191  (*return_count) = 0;
192  *log_msg = strdup(log.str().c_str());
193  err << except.what() << "\n";
194  *err_msg = strdup(err.str().c_str());
195  } catch (std::exception& except) {
196  if (*return_tuples) free(*return_tuples);
197  (*return_count) = 0;
198  *log_msg = strdup(log.str().c_str());
199  err << except.what() << "\n";
200  *err_msg = strdup(err.str().c_str());
201  } catch(...) {
202  if (*return_tuples) free(*return_tuples);
203  (*return_count) = 0;
204  *log_msg = strdup(log.str().c_str());
205  err << "Caught unknown exception!\n";
206  *err_msg = strdup(err.str().c_str());
207  }
208 }
209 
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void eliminate_details(Path &path, const std::vector< pgr_edge_t > &point_edges)
static int a
Definition: tsplib.c:114
int64_t end_id() const
void pgr_dijkstra(G &graph, Path &path, int64_t source, int64_t target, bool only_cost=false)
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
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)
static int b
Definition: tsplib.c:115
graphType
Definition: pgr_types.h:192
std::vector< Basic_vertex > extract_vertices(std::vector< Basic_vertex > vertices, const std::vector< pgr_edge_t > data_edges)
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:52
path_element_t * path
Definition: BDATester.cpp:49
virtual const char * what() const
Definition: pgr_assert.cpp:62
char * err_msg
Definition: BDATester.cpp:50
void graph_insert_data(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
void do_pgr_one_to_many_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_vid, int64_t *end_pidsArr, size_t size_end_pidsArr, char driving_side, bool details, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **err_msg)