pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
many_to_one_dijkstra_driver.cpp
1 /*PGR-GNU*****************************************************************
2 File: many_to_one_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 Copyright (c) 2015 pgRouting developers
13 
14 ------
15 
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 2 of the License, or
19 (at your option) any later version.
20 
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25 
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 
30 ********************************************************************PGR-GNU*/
31 
32 #ifdef __MINGW32__
33 #include <winsock2.h>
34 #include <windows.h>
35 #endif
36 
37 
38 #include <sstream>
39 #include <deque>
40 #include <vector>
41 #include "./pgr_dijkstra.hpp"
42 #include "./many_to_one_dijkstra_driver.h"
43 
44 // #define DEBUG
45 
46 #include "../../common/src/memory_func.hpp"
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_vid BIGINT,
55 // directed BOOLEAN default true,
56 void
57 do_pgr_many_to_one_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_vid,
63  bool directed,
64  bool only_cost,
65  General_path_element_t **return_tuples,
66  size_t *return_count,
67  char ** err_msg) {
68  std::ostringstream log;
69  try {
70  graphType gType = directed? DIRECTED: UNDIRECTED;
71  const auto initial_size = total_tuples;
72 
73  std::deque< Path >paths;
74  log << "Inserting vertices into a c++ vector structure\n";
75  std::set< int64_t > s_start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr);
76  std::vector< int64_t > start_vertices(s_start_vertices.begin(), s_start_vertices.end());
77 
78  if (directed) {
79  log << "Working with directed Graph\n";
80  Pgr_base_graph< DirectedGraph > digraph(gType, initial_size);
81  digraph.graph_insert_data(data_edges, total_tuples);
82  pgr_dijkstra(digraph, paths, start_vertices, end_vid, only_cost);
83  } else {
84  log << "Working with Undirected Graph\n";
85  Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size);
86  undigraph.graph_insert_data(data_edges, total_tuples);
87  pgr_dijkstra(undigraph, paths, start_vertices, end_vid, only_cost);
88  }
89 
90 
91  size_t count(0);
92  count = count_tuples(paths);
93 
94 
95  if (count == 0) {
96  (*return_tuples) = NULL;
97  (*return_count) = 0;
98  log <<
99  "No paths found between Starting and any of the Ending vertices\n";
100  *err_msg = strdup(log.str().c_str());
101  return;
102  }
103 
104  (*return_tuples) = get_memory(count, (*return_tuples));
105  log << "Converting a set of paths into the tuples\n";
106  (*return_count) = (collapse_paths(return_tuples, paths));
107 
108 
109 #ifndef DEBUG
110  *err_msg = strdup("OK");
111 #else
112  *err_msg = strdup(log.str().c_str());
113 #endif
114 
115  return;
116  } catch ( ... ) {
117  log << "Caught unknown expection!\n";
118  *err_msg = strdup(log.str().c_str());
119  return;
120  }
121 }
122 
123 
124 
125 
126