pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
boost_interface_drivedist.cpp
1 /*PGR-GNU*****************************************************************
2 File: boost_interface_drivedist.cpp
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 vicky_vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23  ********************************************************************PGR-GNU*/
24 
25 #ifdef __MINGW32__
26 #include <winsock2.h>
27 #include <windows.h>
28 #endif
29 
30 #include "unistd.h"
31 #include <sstream>
32 #include <deque>
33 #include <vector>
34 
35 #include "../../common/src/memory_func.hpp"
36 #include "./../../dijkstra/src/pgr_dijkstra.hpp"
37 #include "./boost_interface_drivedist.h"
38 
39 extern "C" {
40 #include "./../../common/src/pgr_types.h"
41 }
42 
43 
44 
45 void
46 do_pgr_driving_many_to_dist(
47  pgr_edge_t *data_edges, size_t total_tuples,
48  int64_t *start_vertex, size_t s_len,
49  float8 distance,
50  bool directedFlag,
51  bool equiCostFlag,
52  General_path_element_t **ret_path, size_t *path_count,
53  char ** err_msg) {
54  try {
55  graphType gType = directedFlag? DIRECTED: UNDIRECTED;
56  const auto initial_size = total_tuples;
57 
58  std::deque< Path >paths;
59  std::set< int64_t > s_start_vertices(start_vertex, start_vertex + s_len);
60  std::vector< int64_t > start_vertices(s_start_vertices.begin(), s_start_vertices.end());
61 
62  if (directedFlag) {
63  Pgr_base_graph< DirectedGraph > digraph(gType, initial_size);
64  digraph.graph_insert_data(data_edges, total_tuples);
65  pgr_drivingDistance(digraph, paths, start_vertices, distance, equiCostFlag);
66  } else {
67  Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size);
68  undigraph.graph_insert_data(data_edges, total_tuples);
69  pgr_drivingDistance(undigraph, paths, start_vertices, distance, equiCostFlag);
70  }
71 
72  size_t count(count_tuples(paths));
73 
74 
75  if (count == 0) {
76  *err_msg = strdup("NOTICE: No return values was found");
77  *ret_path = noResult(path_count, (*ret_path));
78  return;
79  }
80  *ret_path = get_memory(count, (*ret_path));
81  auto trueCount(collapse_paths(ret_path, paths));
82  *path_count = trueCount;
83 
84 
85 #ifndef DEBUG
86  *err_msg = strdup("OK");
87 #else
88  *err_msg = strdup(log.str().c_str());
89 #endif
90  return;
91 
92  } catch ( ... ) {
93  *err_msg = strdup("Caught unknown expection!");
94  *ret_path = noResult(path_count, (*ret_path));
95  return;
96  }
97 }
98 
99 
100 
101 
102 
103 
104 
105 void
106 do_pgr_driving_distance(
107  pgr_edge_t *data_edges, size_t total_edges,
108  int64_t start_vertex,
109  float8 distance,
110  bool directedFlag,
111  General_path_element_t **ret_path, size_t *path_count,
112  char **err_msg) {
113  std::ostringstream log;
114  try {
115  // if it already has values there will be a leak
116  // call with pointing to NULL
117  *ret_path = NULL;
118 
119  log << "NOTICE: Started processing pgr_drivingDistance for 1 start_vid\n";
120  // in c code this should have been checked:
121  // 1) start_vertex is in the data_edges DONE
122 
123  graphType gType = directedFlag? DIRECTED: UNDIRECTED;
124  const auto initial_size = total_edges;
125 
126  Path path;
127 
128 
129  if (directedFlag) {
130  log << "NOTICE: Processing Directed graph\n";
131  Pgr_base_graph< DirectedGraph > digraph(gType, initial_size);
132  digraph.graph_insert_data(data_edges, total_edges);
133  pgr_drivingDistance(digraph, path, start_vertex, distance);
134  } else {
135  log << "NOTICE: Processing Undirected graph\n";
136  Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size);
137  undigraph.graph_insert_data(data_edges, total_edges);
138  pgr_drivingDistance(undigraph, path, start_vertex, distance);
139  }
140 
141  log << "Returning number of tuples" << path.size() << "\n";
142  if (path.empty()) {
143  log << "NOTICE: it shoud have at least the one for it self";
144  *err_msg = strdup(log.str().c_str());
145  *ret_path = noResult(path_count, (*ret_path));
146  return;
147  }
148 
149  log << "NOTICE: Calculating the number of tuples \n";
150  auto count = path.size();
151 
152  log << "NOTICE Count: " << count << " tuples\n";
153 
154  *ret_path = get_memory(count, (*ret_path));
155 
156  size_t sequence = 0;
157  path.get_pg_dd_path(ret_path, sequence);
158  *path_count = count;
159 
160 #ifndef DEBUG
161  *err_msg = strdup("OK");
162 #else
163  *err_msg = strdup(log.str().c_str());
164 #endif
165 
166  return;
167  } catch ( ... ) {
168  log << "NOTICE: unknown exception cought";
169  *err_msg = strdup(log.str().c_str());
170  *ret_path = noResult(path_count, (*ret_path));
171  return;
172  }
173 }
174