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_one_withPoints_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 #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 <vector>
40 #include "./pgr_dijkstra.hpp"
41 #include "./pgr_withPoints.hpp"
43 
44 
45 #include "./../../common/src/basic_vertex.h"
46 #include "./../../common/src/pgr_alloc.hpp"
47 #include "./../../common/src/pgr_assert.h"
48 
49 
50 // CREATE OR REPLACE FUNCTION pgr_withPoint(
51 // edges_sql TEXT,
52 // points_sql TEXT,
53 // start_pid BIGINT,
54 // end_pid BIGINT,
55 // directed BOOLEAN DEFAULT true
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  int64_t start_pid,
64  int64_t end_pid,
65  bool directed,
66  char driving_side,
67  bool details,
68  bool only_cost,
69  General_path_element_t **return_tuples,
70  size_t *return_count,
71  char ** log_msg,
72  char ** err_msg) {
73  std::ostringstream log;
74  std::ostringstream err;
75  try {
76  pgassert(!(*return_tuples));
77  pgassert((*return_count) == 0);
78  pgassert(!(*log_msg));
79  pgassert(!(*err_msg));
80 
81  log << "ntering do_pgr_withPoints\n";
82  std::vector< Point_on_edge_t >
83  points(points_p, points_p + total_points);
84 
85  log << "total points" << points.size() << "\n";
86 
87  /*
88  * This test is easier in C++
89  */
90  int errcode = check_points(points, log);
91  if (errcode) {
92  *log_msg = strdup(log.str().c_str());
93  err << "Unexpected point(s) with same pid but different edge/fraction/side combination found.";
94  *err_msg = strdup(err.str().c_str());
95  return;
96  }
97 
98  std::vector< pgr_edge_t >
99  edges_to_modify(edges_of_points, edges_of_points + total_edges_of_points);
100 
101  std::vector< pgr_edge_t > new_edges;
102  log << "start_pid" << start_pid << "\n";
103  log << "end_pid" << end_pid << "\n";
104  log << "driving_side" << driving_side << "\n";
106  points,
107  edges_to_modify,
108  driving_side,
109  new_edges,
110  log);
111 
112  int64_t start_vid(start_pid);
113  int64_t end_vid(end_pid);
114 
115  log << "start_vid" << start_vid << "\n";
116  log << "end_vid" << end_vid << "\n";
117  graphType gType = directed? DIRECTED: UNDIRECTED;
118 
119  Path path;
120 
121  auto vertices(pgrouting::extract_vertices(edges, total_edges));
122  vertices = pgrouting::extract_vertices(vertices, new_edges);
123 
124  log << "extracted vertices: ";
125  for (const auto v : vertices) {
126  log << v.id << ", ";
127  }
128  log << "\n";
129 
130  if (directed) {
131  log << "Working with directed Graph\n";
132 
133  pgrouting::DirectedGraph digraph(vertices, gType);
134  digraph.graph_insert_data(edges, total_edges);
135  digraph.graph_insert_data(new_edges);
136 
137 
138  pgr_dijkstra(digraph, path, start_vid, end_vid, only_cost);
139  } else {
140  log << "Working with Undirected Graph\n";
141 
142  pgrouting::UndirectedGraph undigraph(gType);
143  undigraph.graph_insert_data(edges, total_edges);
144  undigraph.graph_insert_data(new_edges);
145 
146  pgr_dijkstra(undigraph, path, start_vid, end_vid, only_cost);
147  }
148 
149 
150  size_t count(path.size());
151  if (count == 0) {
152  (*return_tuples) = NULL;
153  (*return_count) = 0;
154  *log_msg = strdup(log.str().c_str());
155  err <<
156  "No paths found between Starting and any of the Ending vertices\n";
157  *log_msg = strdup(err.str().c_str());
158  return;
159  }
160 
161  if (!details) {
162  eliminate_details(path, edges_to_modify);
163  }
164 
165  (*return_tuples) = pgr_alloc(count, (*return_tuples));
166  size_t sequence = 0;
167  path.generate_postgres_data(return_tuples, sequence);
168  (*return_count) = sequence;
169 
170  *log_msg = strdup(log.str().c_str());
171  pgassert(!*err_msg);
172  } catch (AssertFailedException &except) {
173  if (*return_tuples) free(*return_tuples);
174  (*return_count) = 0;
175  *log_msg = strdup(log.str().c_str());
176  err << except.what() << "\n";
177  *err_msg = strdup(err.str().c_str());
178  } catch (std::exception& except) {
179  if (*return_tuples) free(*return_tuples);
180  (*return_count) = 0;
181  *log_msg = strdup(log.str().c_str());
182  err << except.what() << "\n";
183  *err_msg = strdup(err.str().c_str());
184  } catch(...) {
185  if (*return_tuples) free(*return_tuples);
186  (*return_count) = 0;
187  *log_msg = strdup(log.str().c_str());
188  err << "Caught unknown exception!\n";
189  *err_msg = strdup(err.str().c_str());
190  }
191 }
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)
void generate_postgres_data(General_path_element_t **postgres_data, size_t &sequence) 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)
graphType
Definition: pgr_types.h:192
void do_pgr_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_pid, int64_t end_pid, bool directed, char driving_side, bool details, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **err_msg)
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
size_t size() const
void graph_insert_data(const T *edges, int64_t count)
Inserts count edges of type T into the graph.