pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
withPoints_ksp_driver.cpp
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 #ifdef __MINGW32__
32 #include <winsock2.h>
33 #include <windows.h>
34 //#ifdef unlink
35 //#undef unlink
36 //#endif
37 #endif
38 
39 
40 #include <sstream>
41 #include <deque>
42 #include <vector>
43 #include "./withPoints_ksp_driver.h"
44 #include "./../../withPoints/src/pgr_withPoints.hpp"
45 #include "./../../common/src/memory_func.hpp"
46 #include "./pgr_ksp.hpp"
47 
48 
49 
50 // CREATE OR REPLACE FUNCTION pgr_withPointsKSP(
51 // edges_sql TEXT,
52 // points_sql TEXT,
53 // start_pid BIGINT,
54 // end_pid BIGINT,
55 // directed BOOLEAN DEFAULT true
56 
57 
58 int
59 do_pgr_withPointsKsp(
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_vid,
64  int64_t end_vid,
65  int k,
66  bool directed,
67  bool heap_paths,
68  char driving_side,
69  bool details,
70  General_path_element_t **return_tuples,
71  size_t *return_count,
72  char ** err_msg){
73  std::ostringstream log;
74  try {
75  /*
76  * This is the original state
77  */
78  if (*err_msg) free(err_msg);
79  if (*return_tuples) free(return_tuples);
80  (*return_count) = 0;
81 
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  return errcode;
88  }
89 
90 
91  std::vector< pgr_edge_t >
92  edges_to_modify(edges_of_points, edges_of_points + total_edges_of_points);
93 
94  std::vector< pgr_edge_t > new_edges;
95  create_new_edges(
96  points,
97  edges_to_modify,
98  driving_side,
99  new_edges,
100  log);
101 
102 
103  graphType gType = directed? DIRECTED: UNDIRECTED;
104  const auto initial_size = total_edges;
105 
106  std::deque< Path > paths;
107 
108  if (directed) {
109  log << "Working with directed Graph\n";
110  Pgr_base_graph< DirectedGraph > digraph(gType, initial_size);
111  digraph.graph_insert_data(edges, total_edges);
112  digraph.graph_insert_data(new_edges);
114  paths = fn_yen.Yen(digraph, start_vid, end_vid, k, heap_paths);
115  } else {
116  log << "Working with undirected Graph\n";
117  Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size);
118  undigraph.graph_insert_data(edges, total_edges);
119  undigraph.graph_insert_data(new_edges);
121  paths = fn_yen.Yen(undigraph, start_vid, end_vid, k, heap_paths);
122  }
123 
124 #if 0
125  for (auto &path : paths) {
126  path.print_path(log);
127  adjust_pids(points, path);
128  path.print_path(log);
129  }
130 #endif
131 
132  if (!details) {
133  for (auto &path : paths) {
134  eliminate_details(path, edges_to_modify);
135  }
136  }
137 
138  auto count(count_tuples(paths));
139 
140  if (count == 0) {
141  return 0;
142  }
143 
144 
145  *return_tuples = NULL;
146  *return_tuples = get_memory(count, (*return_tuples));
147 
148  size_t sequence = 0;
149  int route_id = 0;
150  for (const auto &path : paths) {
151  if (path.size() > 0)
152  path.get_pg_ksp_path(return_tuples, sequence, route_id);
153  ++route_id;
154  }
155 
156  if (count != sequence) {
157  return 2;
158  }
159  (*return_count) = sequence;
160 
161 
162 #ifndef DEBUG
163  *err_msg = strdup("OK");
164 #else
165  *err_msg = strdup(log.str().c_str());
166 #endif
167  return 0;
168  } catch ( ... ) {
169  log << "Caught unknown expection!\n";
170  *err_msg = strdup(log.str().c_str());
171  }
172  return 1000;
173 }