pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
many_to_many_dijkstra_driver.cpp
1 /*PGR-GNU*****************************************************************
2 File: many_to_many_dijkstra_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: vicky_vergara@hotmail.com
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 #ifdef __MINGW32__
31 #include <winsock2.h>
32 #include <windows.h>
33 #endif
34 
35 
36 #include <sstream>
37 #include <deque>
38 #include <vector>
39 #include <set>
40 #include "./pgr_dijkstra.hpp"
41 #include "./many_to_many_dijkstra_driver.h"
42 
43 // #define DEBUG
44 
45 #include "../../common/src/memory_func.hpp"
46 
47 extern "C" {
48 #include "./../../common/src/pgr_types.h"
49 }
50 
51 // CREATE OR REPLACE FUNCTION pgr_dijkstra(
52 // sql text,
53 // start_vids anyarray,
54 // end_vids anyarray,
55 // directed boolean default true,
56 void
57 do_pgr_many_to_many_dijkstra(
58  pgr_edge_t *data_edges,
59  size_t total_tuples,
60  int64_t *start_vidsArr,
61  size_t size_start_vidsArr,
62  int64_t *end_vidsArr,
63  size_t size_end_vidsArr,
64  bool directed,
65  bool only_cost,
66  General_path_element_t **return_tuples,
67  size_t *return_count,
68  char ** err_msg) {
69  std::ostringstream log;
70  try {
71  graphType gType = directed? DIRECTED: UNDIRECTED;
72  const auto initial_size = total_tuples;
73 
74  std::deque< Path >paths;
75  log << "Inserting vertices into a c++ vector structure\n";
76  std::set< int64_t >
77  s_start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
78  std::set< int64_t >
79  s_end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr);
80 
81  std::vector< int64_t > start_vertices(s_start_vertices.begin(), s_start_vertices.end());
82  std::vector< int64_t > end_vertices(s_end_vertices.begin(), s_end_vertices.end());
83  if (directed) {
84  log << "Working with directed Graph\n";
85  Pgr_base_graph< DirectedGraph > digraph(gType, initial_size);
86  digraph.graph_insert_data(data_edges, total_tuples);
87  pgr_dijkstra(digraph, paths, start_vertices, end_vertices, only_cost);
88  } else {
89  log << "Working with Undirected Graph\n";
90  Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size);
91  undigraph.graph_insert_data(data_edges, total_tuples);
92  pgr_dijkstra(undigraph, paths, start_vertices, end_vertices, only_cost);
93  }
94 
95 
96  size_t count(0);
97 
98  count = count_tuples(paths);
99 
100  if (count == 0) {
101  (*return_tuples) = NULL;
102  (*return_count) = 0;
103  log <<
104  "No paths found between Starting and any of the Ending vertices\n";
105  *err_msg = strdup(log.str().c_str());
106  return;
107  }
108 
109  (*return_tuples) = get_memory(count, (*return_tuples));
110  log << "Converting a set of paths into the tuples\n";
111  (*return_count) = (collapse_paths(return_tuples, paths));
112 
113 
114 #ifndef DEBUG
115  *err_msg = strdup("OK");
116 #else
117  *err_msg = strdup(log.str().c_str());
118 #endif
119 
120  return;
121  } catch ( ... ) {
122  log << "Caught unknown expection!\n";
123  *err_msg = strdup(log.str().c_str());
124  return;
125  }
126 }
127 
128 
129 
130 
131