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
withPoints_ksp_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 #include <sstream>
37 #include <deque>
38 #include <vector>
39 #include "./pgr_ksp.hpp"
41 #include "./../../withPoints/src/pgr_withPoints.hpp"
42 #include "./../../common/src/pgr_alloc.hpp"
43 
44 
45 // CREATE OR REPLACE FUNCTION pgr_withPointsKSP(
46 // edges_sql TEXT,
47 // points_sql TEXT,
48 // start_pid BIGINT,
49 // end_pid BIGINT,
50 // directed BOOLEAN DEFAULT true
51 
52 
53 int
55  pgr_edge_t *edges, size_t total_edges,
56  Point_on_edge_t *points_p, size_t total_points,
57  pgr_edge_t *edges_of_points, size_t total_edges_of_points,
58  int64_t start_vid,
59  int64_t end_vid,
60  int k,
61  bool directed,
62  bool heap_paths,
63  char driving_side,
64  bool details,
65  General_path_element_t **return_tuples,
66  size_t *return_count,
67  char ** err_msg) {
68  std::ostringstream log;
69  try {
70  /*
71  * This is the original state
72  */
73  if (*err_msg) free(err_msg);
74  if (*return_tuples) free(return_tuples);
75  (*return_count) = 0;
76 
77  std::vector< Point_on_edge_t >
78  points(points_p, points_p + total_points);
79 
80  int errcode = check_points(points, log);
81  if (errcode) {
82  return errcode;
83  }
84 
85 
86  std::vector< pgr_edge_t >
87  edges_to_modify(edges_of_points, edges_of_points + total_edges_of_points);
88 
89  std::vector< pgr_edge_t > new_edges;
91  points,
92  edges_to_modify,
93  driving_side,
94  new_edges,
95  log);
96 
97 
98  graphType gType = directed? DIRECTED: UNDIRECTED;
99 
100  std::deque< Path > paths;
101 
102  if (directed) {
103  log << "Working with directed Graph\n";
104  pgrouting::DirectedGraph digraph(gType);
105  digraph.graph_insert_data(edges, total_edges);
106  digraph.graph_insert_data(new_edges);
108  paths = fn_yen.Yen(digraph, start_vid, end_vid, k, heap_paths);
109  } else {
110  log << "Working with undirected Graph\n";
111  pgrouting::UndirectedGraph undigraph(gType);
112  undigraph.graph_insert_data(edges, total_edges);
113  undigraph.graph_insert_data(new_edges);
115  paths = fn_yen.Yen(undigraph, start_vid, end_vid, k, heap_paths);
116  }
117 
118 #if 0
119  for (auto &path : paths) {
120  path.print_path(log);
121  adjust_pids(points, path);
122  path.print_path(log);
123  }
124 #endif
125 
126  if (!details) {
127  for (auto &path : paths) {
128  eliminate_details(path, edges_to_modify);
129  }
130  }
131 
132  auto count(count_tuples(paths));
133 
134  if (count == 0) {
135  return 0;
136  }
137 
138 
139  *return_tuples = NULL;
140  *return_tuples = pgr_alloc(count, (*return_tuples));
141 
142  size_t sequence = 0;
143  int route_id = 0;
144  for (const auto &path : paths) {
145  if (path.size() > 0)
146  path.get_pg_ksp_path(return_tuples, sequence, route_id);
147  ++route_id;
148  }
149 
150  if (count != sequence) {
151  return 2;
152  }
153  (*return_count) = sequence;
154 
155 
156 #ifndef DEBUG
157  *err_msg = strdup("OK");
158 #else
159  *err_msg = strdup(log.str().c_str());
160 #endif
161  return 0;
162  } catch ( ... ) {
163  log << "Caught unknown exception!\n";
164  *err_msg = strdup(log.str().c_str());
165  }
166  return 1000;
167 }
int do_pgr_withPointsKsp(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_vid, int k, bool directed, bool heap_paths, char driving_side, bool details, General_path_element_t **return_tuples, size_t *return_count, char **err_msg)
void eliminate_details(Path &path, const std::vector< pgr_edge_t > &point_edges)
static void adjust_pids(const std::vector< Point_on_edge_t > &points, const int64_t &start_pid, const int64_t &end_pid, Path &path)
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)
graphType
Definition: pgr_types.h:192
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:52
path_element_t * path
Definition: BDATester.cpp:49
std::deque< Path > Yen(G &graph, int64_t source, int64_t target, int K, bool heap_paths)
Definition: pgr_ksp.cpp:56
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.