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
many_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 <sstream>
38 #include <deque>
39 #include <algorithm>
40 #include <set>
41 #include <vector>
42 #include <cassert>
43 
44 
45 #include "./pgr_dijkstra.hpp"
46 #include "./pgr_withPoints.hpp"
48 #include "./../../common/src/pgr_types.h"
49 #include "./../../common/src/pgr_assert.h"
50 #include "./../../common/src/pgr_alloc.hpp"
51 
52 
53 // CREATE OR REPLACE FUNCTION pgr_withPoint(
54 // edges_sql TEXT,
55 // points_sql TEXT,
56 // start_pid ANYARRAY,
57 // end_pid BIGINT,
58 // directed BOOLEAN DEFAULT true
59 
60 int
63  size_t total_edges,
64  Point_on_edge_t *points_p,
65  size_t total_points,
66  pgr_edge_t *edges_of_points,
67  size_t total_edges_of_points,
68  int64_t *start_pidsArr,
69  size_t size_start_pidsArr,
70  int64_t *end_pidsArr,
71  size_t size_end_pidsArr,
72 
73  char driving_side,
74  bool details,
75  bool directed,
76  bool only_cost,
77  General_path_element_t **return_tuples,
78  size_t *return_count,
79  char ** err_msg) {
80  std::ostringstream log;
81  try {
82  std::vector< Point_on_edge_t >
83  points(points_p, points_p + total_points);
84 
85  int errcode = check_points(points, log);
86  if (errcode) {
87  /* Point(s) with same pid but different edge/fraction/side combination found */
88  *err_msg = strdup(log.str().c_str());
89  return errcode;
90  }
91 
92  std::vector< pgr_edge_t >
93  edges_to_modify(edges_of_points, edges_of_points + total_edges_of_points);
94 
95  std::vector< pgr_edge_t > new_edges;
97  points,
98  edges_to_modify,
99  driving_side,
100  new_edges);
101 
102 
103  std::set< int64_t > s_start_vertices(start_pidsArr, start_pidsArr + size_start_pidsArr);
104  std::set< int64_t > s_end_vertices(end_pidsArr, end_pidsArr + size_end_pidsArr);
105 
106  std::vector< int64_t > start_vertices(s_start_vertices.begin(), s_start_vertices.end());
107  std::vector< int64_t > end_vertices(s_end_vertices.begin(), s_end_vertices.end());
108 
109 #if 0
110 
111  std::set< int64_t > start_vertices;
112  std::set< int64_t > end_vertices;
113 
114  for (const auto &start_pid : start_points) {
115  for (const auto point : points) {
116  if (point.pid == start_pid) {
117  start_vertices.insert(point.vertex_id);
118  break;
119  }
120  }
121  }
122  for (const auto &end_pid : end_points) {
123  for (const auto point : points) {
124  if (point.pid == end_pid) {
125  end_vertices.insert(point.vertex_id);
126  break;
127  }
128  }
129  }
130 #endif
131  graphType gType = directed? DIRECTED: UNDIRECTED;
132 
133  std::deque< Path > paths;
134 
135 
136  if (directed) {
137  log << "Working with directed Graph\n";
138  pgrouting::DirectedGraph digraph(gType);
139  digraph.graph_insert_data(edges, total_edges);
140  digraph.graph_insert_data(new_edges);
141  pgr_dijkstra(digraph, paths, start_vertices, end_vertices, only_cost);
142  } else {
143  log << "Working with Undirected Graph\n";
144  pgrouting::UndirectedGraph undigraph(gType);
145  undigraph.graph_insert_data(edges, total_edges);
146  undigraph.graph_insert_data(new_edges);
147  pgr_dijkstra(undigraph, paths, start_vertices, end_vertices, only_cost);
148  }
149 
150 #if 0
151  for (auto &path : paths) {
152  adjust_pids(points, path);
153  }
154 #endif
155  if (!details) {
156  for (auto &path : paths) {
157  eliminate_details(path, edges_to_modify);
158  }
159  }
160 
161  /*
162  * order paths based on the start_pid, end_pid
163  */
164  std::sort(paths.begin(), paths.end(),
165  [](const Path &a, const Path &b)
166  -> bool {
167  if (b.start_id() != a.start_id()) {
168  return a.start_id() < b.start_id();
169  }
170  return a.end_id() < b.end_id();
171  });
172 
173  size_t count(0);
174  count = count_tuples(paths);
175 
176 
177  if (count == 0) {
178  (*return_tuples) = NULL;
179  (*return_count) = 0;
180  log <<
181  "No paths found between Starting and any of the Ending vertices\n";
182  *err_msg = strdup(log.str().c_str());
183  return 0;
184  }
185 
186  (*return_tuples) = pgr_alloc(count, (*return_tuples));
187  log << "Converting a set of paths into the tuples\n";
188  (*return_count) = (collapse_paths(return_tuples, paths));
189 
190  return 0;
191  } catch (AssertFailedException &except) {
192  if (*return_tuples) free(*return_tuples);
193  (*return_count) = 0;
194  log << except.what() << "\n";
195  *err_msg = strdup(log.str().c_str());
196  } catch (std::exception& except) {
197  if (*return_tuples) free(*return_tuples);
198  (*return_count) = 0;
199  log << except.what() << "\n";
200  *err_msg = strdup(log.str().c_str());
201  } catch(...) {
202  if (*return_tuples) free(*return_tuples);
203  (*return_count) = 0;
204  log << "Caught unknown exception!\n";
205  *err_msg = strdup(log.str().c_str());
206  }
207 
208  return 1000;
209 }
210 
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)
int do_pgr_many_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_pidsArr, size_t size_start_pidsArr, 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 **err_msg)
static int a
Definition: tsplib.c:114
static void adjust_pids(const std::vector< Point_on_edge_t > &points, const int64_t &start_pid, const int64_t &end_pid, Path &path)
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)
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
int64_t start_id() const
Definition: tsp.h:38
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.