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