pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
astarOneToOne_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: astarOneToOne_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 #if defined(__MINGW32__) || defined(_MSC_VER)
32 #include <winsock2.h>
33 #include <windows.h>
34 #endif
35 
36 #include <sstream>
37 #include <deque>
38 #include <algorithm>
39 #include <vector>
40 #include "./astarOneToOne_driver.h"
41 
42 #include "./../../common/src/pgr_types.h"
43 
44 #include "./pgr_astar.hpp"
45 #include "./../../common/src/pgr_assert.h"
46 #include "./../../common/src/pgr_alloc.hpp"
47 
48 template < class G >
49 void
51  G &graph,
52  Path &path,
53  int64_t source,
54  int64_t target,
55  int heuristic,
56  double factor,
57  double epsilon,
58  bool only_cost = false) {
59  Pgr_astar< G > fn_astar;
60  fn_astar.astar(graph, path, source, target,
61  heuristic, factor, epsilon, only_cost);
62 }
63 
64 
65 /************************************************************
66  edges_sql TEXT,
67  vertex_table TEXT,
68  start_vid BIGINT,
69  end_vid BIGINT directed BOOLEAN DEFAULT true,
70  ***********************************************************/
72  Pgr_edge_xy_t *data_edges,
73  size_t total_edges,
74  int64_t start_vid,
75  int64_t end_vid,
76  bool directed,
77  int heuristic,
78  double factor,
79  double epsilon,
80  bool only_cost,
81  General_path_element_t **return_tuples,
82  size_t *return_count,
83  char ** log_msg,
84  char ** err_msg) {
85  std::ostringstream err;
86  std::ostringstream log;
87  try {
88  pgassert(!(*log_msg));
89  pgassert(!(*err_msg));
90  pgassert(!(*return_tuples));
91  pgassert(*return_count == 0);
92  pgassert(total_edges != 0);
93 
94  if (total_edges <= 1) {
95  log << "Required: more than one edge\n";
96  (*return_tuples) = NULL;
97  (*return_count) = 0;
98  *err_msg = strdup(log.str().c_str());
99  return;
100  }
101 
102 
103  graphType gType = directed? DIRECTED: UNDIRECTED;
104 
105  Path path;
106 
107  if (directed) {
108  log << "Working with directed Graph\n";
109  pgrouting::xyDirectedGraph digraph(gType);
110  log << "Working with directed Graph 1 \n";
111  digraph.graph_insert_data(data_edges, total_edges);
112 #ifdef DEBUG
113  log << digraph;
114 #endif
115  log << "Working with directed Graph 2\n";
116  pgr_astar(digraph, path, start_vid, end_vid,
117  heuristic, factor, epsilon, only_cost);
118  log << "Working with directed Graph 3\n";
119  } else {
120  log << "Working with Undirected Graph\n";
121  pgrouting::xyUndirectedGraph undigraph(gType);
122  undigraph.graph_insert_data(data_edges, total_edges);
123 #ifdef DEBUG
124  log << undigraph;
125 #endif
126  pgr_astar(undigraph, path, start_vid, end_vid,
127  heuristic, factor, epsilon, only_cost);
128  }
129 
130  auto count = path.size();
131 
132  if (count == 0) {
133  (*return_tuples) = NULL;
134  (*return_count) = 0;
135  log <<
136  "No paths found\n";
137  *log_msg = strdup(log.str().c_str());
138  return;
139  }
140 
141  size_t sequence = 0;
142  (*return_tuples) = pgr_alloc(count, (*return_tuples));
143  path.generate_postgres_data(return_tuples, sequence);
144  (*return_count) = sequence;
145 
146  *err_msg = NULL;
147  *log_msg = strdup(log.str().c_str());
148  } catch (AssertFailedException &except) {
149  if (*return_tuples) free(*return_tuples);
150  (*return_count) = 0;
151  err << except.what() << "\n";
152  *err_msg = strdup(err.str().c_str());
153  *log_msg = strdup(log.str().c_str());
154  } catch (std::exception& except) {
155  if (*return_tuples) free(*return_tuples);
156  (*return_count) = 0;
157  err << except.what() << "\n";
158  *err_msg = strdup(err.str().c_str());
159  *log_msg = strdup(log.str().c_str());
160  } catch(...) {
161  if (*return_tuples) free(*return_tuples);
162  (*return_count) = 0;
163  err << "Caught unknown exception!\n";
164  *err_msg = strdup(err.str().c_str());
165  *log_msg = strdup(log.str().c_str());
166  }
167 }
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void do_pgr_astarOneToOne(Pgr_edge_xy_t *data_edges, size_t total_edges, int64_t start_vid, int64_t end_vid, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **err_msg)
void generate_postgres_data(General_path_element_t **postgres_data, size_t &sequence) const
void astar(G &graph, Path &path, int64_t start_vertex, int64_t end_vertex, int heuristic, double factor, double epsilon, bool only_cost=false)
one to one
Definition: pgr_astar.hpp:263
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
void pgr_astar(G &graph, Path &path, int64_t source, int64_t target, int heuristic, double factor, double epsilon, bool only_cost=false)
graphType
Definition: pgr_types.h:192
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:52
path_element_t * path
Definition: BDATester.cpp:49
virtual const char * what() const
Definition: pgr_assert.cpp:62
char * err_msg
Definition: BDATester.cpp:50
size_t size() const
void graph_insert_data(const T *edges, int64_t count)
Inserts count edges of type T into the graph.