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
test_c_edges_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: testXYedges_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 Rohith Reddy
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 
37 #include <sstream>
38 #include <iomanip>
39 #include <vector>
40 #include "./test_c_edges_driver.h"
41 
42 #include "./../../common/src/pgr_types.h"
43 
44 #include "./../../dijkstra/src/pgr_dijkstra.hpp"
45 #if 0
46 #include "./../../common/src/pgr_base_graph.hpp"
47 #endif
48 #include "./../../contraction/src/pgr_contractionGraph.hpp"
49 #include "./../../common/src/pgr_assert.h"
50 
51 
52 /************************************************************
53  edges_sql TEXT
54  ***********************************************************/
55 bool
57  pgr_edge_t *data_edges,
58  size_t total_edges,
59  char ** log_msg,
60  char ** err_msg){
61  std::ostringstream log;
62  std::ostringstream err;
63  try {
64  pgassert(!(*log_msg));
65  pgassert(!(*err_msg));
66 
67  std::vector< pgr_edge_t > edges(data_edges, data_edges + total_edges);
68  std::vector < pgrouting::contraction::Vertex > vertices(pgrouting::contraction::extract_vertices(edges));
69 
70 
71  log << "Original: \n" <<
72  std::setprecision(9);
73  for (const auto edge: edges) {
74  log << "id = " << edge.id
75  << "\tsource = " << edge.source
76  << "\ttarget = " << edge.target
77  << "\tcost = " << edge.cost
78  << "\treverse_cost = " << edge.reverse_cost
79  << ")\n";
80  }
81  // TODO(Rohith) make the graph work with pgr_dijkstra
82  #if 0
83  {
84  log << "Testing Directed , insertion using vector\n";
86  log << " - Created graph:\n";
87  log << graph;
88 
89  log << " - Inserting Edges:\n";
90  graph.graph_insert_data(edges);
91  log << graph;
92 
93  log << " - Can do a dijKstra:\n";
94  Path path;
95  pgr_dijkstra(graph, path, 2, 3, true);
96 
97  }
98 
99  {
100  log << "Testing Directed , insertion using C array\n";
102  log << " - Created graph:\n";
103  log << graph;
104 
105  log << " - Inserting Edges:\n";
106  graph.graph_insert_data(data_edges, total_edges);
107  log << graph;
108 
109  log << " - Can do a dijKstra:\n";
110  Path path;
111  pgr_dijkstra(graph, path, 2, 3, true);
112 
113  }
114  {
115  log << "Testing Directed , creating with vertices, insertion using vector\n";
116  pgrouting::CHDirectedGraph graph(vertices, DIRECTED);
117  log << " - Created graph:\n";
118  log << graph;
119 
120  log << " - Inserting Edges:\n";
121  graph.graph_insert_data(edges);
122  log << graph;
123 
124  log << " - Can do a dijKstra:\n";
125  Path path;
126  pgr_dijkstra(graph, path, 2, 3, true);
127 
128  }
129  {
130  log << "Testing Directed , creating with vertices, insertion using C array\n";
131  pgrouting::CHDirectedGraph graph(vertices, DIRECTED);
132  log << " - Created graph:\n";
133  log << graph;
134 
135  log << " - Inserting Edges:\n";
136  graph.graph_insert_data(data_edges, total_edges);
137  log << graph;
138 
139  log << " - Can do a dijKstra:\n";
140  Path path;
141  pgr_dijkstra(graph, path, 2, 3, true);
142 
143  }
144 
145  {
146  log << "Testing Undirected , insertion using vector\n";
148  log << " - Created graph:\n";
149  log << graph;
150 
151  log << " - Inserting Edges:\n";
152  graph.graph_insert_data(edges);
153  log << graph;
154 
155  log << " - Can do a dijKstra:\n";
156  Path path;
157  pgr_dijkstra(graph, path, 2, 3, true);
158 
159  }
160  {
161  log << "Testing Undirected , insertion using C array\n";
163  log << " - Created graph:\n";
164  log << graph;
165 
166  log << " - Inserting Edges:\n";
167  graph.graph_insert_data(data_edges, total_edges);
168  log << graph;
169 
170  log << " - Can do a dijKstra:\n";
171  Path path;
172  pgr_dijkstra(graph, path, 2, 3, true);
173 
174  }
175  {
176  log << "Testing Undirected , insertion using C array\n";
178  log << " - Created graph:\n";
179  log << graph;
180 
181  log << " - Inserting Edges:\n";
182  graph.graph_insert_data(data_edges, total_edges);
183  log << graph;
184 
185  log << " - Can do a dijKstra:\n";
186  Path path;
187  pgr_dijkstra(graph, path, 2, 3, true);
188 
189  }
190  {
191  log << "Testing Undirected , creating with vertices, insertion using vector\n";
192  pgrouting::CHUndirectedGraph graph(vertices, UNDIRECTED);
193  log << " - Created graph:\n";
194  log << graph;
195 
196  log << " - Inserting Edges:\n";
197  graph.graph_insert_data(edges);
198  log << graph;
199 
200  log << " - Can do a dijKstra:\n";
201  Path path;
202  pgr_dijkstra(graph, path, 2, 3, true);
203 
204  }
205 
206  {
207  log << "Testing Identifiers, creating with vertices, insertion using vector\n";
208  log << " - Created graph:\n";
209  pgrouting::CHUndirectedGraph graph(vertices, UNDIRECTED);
210  log << " - Inserting Edges:\n";
211  graph.graph_insert_data(edges);
212  log << " - All vertices:\n";
213  Identifiers<int64_t> all_vertices, contracted_vertices, remaining_vertices;
214  for (const auto vertex: vertices) {
215  all_vertices.insert(graph.get_V(vertex.id));
216  //log << vertex;
217  }
218  log << " " << all_vertices;
219  log << "\n";
220  log << " - Contracted vertices:\n";
221  /*
222  1, 7, 8, 13, 14, 16
223  */
224  contracted_vertices.insert(graph.get_V(1));
225  contracted_vertices.insert(graph.get_V(7));
226  contracted_vertices.insert(graph.get_V(8));
227  contracted_vertices.insert(graph.get_V(13));
228  contracted_vertices.insert(graph.get_V(14));
229  contracted_vertices.insert(graph.get_V(16));
230  log << " " << contracted_vertices;
231  log << "\n";
232  log << " - Remaining vertices:\n";
233  remaining_vertices = all_vertices - contracted_vertices;
234  log << " " << remaining_vertices;
235  log << "\n";
236  }
237 
238  {
239  log << "Testing Vertex class, creating graph with vertices, insertion using vector\n";
240  log << " - Created graph:\n";
241  pgrouting::CHDirectedGraph graph(vertices, UNDIRECTED);
242  log << " - Inserting Edges:\n";
243  graph.graph_insert_data(edges);
244  int64_t vid1 = graph.get_V(1);
245  int64_t vid2 = graph.get_V(2);
246  int64_t vid5 = graph.get_V(5);
247  int64_t vid4 = graph.get_V(4);
248  int64_t vid7 = graph.get_V(7);
249  int64_t vid8 = graph.get_V(8);
250  int64_t vid10 = graph.get_V(10);
251  int64_t vid12 = graph.get_V(12);
252  int64_t vid13 = graph.get_V(13);
253  int64_t vid14 = graph.get_V(14);
254  int64_t vid15 = graph.get_V(15);
255  int64_t vid16 = graph.get_V(16);
256  int64_t vid17 = graph.get_V(17);
257 
258  log << " - id ----- V:\n";
259  log << " " << 1 << " ----- " << vid1 << "\n";
260  log << " " << 2 << " ----- " << vid2 << "\n";
261  log << " " << 4 << " ----- " << vid4 << "\n";
262  log << " " << 5 << " ----- " << vid5 << "\n";
263  log << " " << 7 << " ----- " << vid7 << "\n";
264  log << " " << 8 << " ----- " << vid8 << "\n";
265  log << " " << 10 << " ----- " << vid10 << "\n";
266  log << " " << 12 << " ----- " << vid12 << "\n";
267  log << " " << 13 << " ----- " << vid13 << "\n";
268  log << " " << 14 << " ----- " << vid14 << "\n";
269  log << " " << 15 << " ----- " << vid15 << "\n";
270  log << " " << 16 << " ----- " << vid16 << "\n";
271  log << " " << 17 << " ----- " << vid17 << "\n";
272  pgrouting::contraction::Vertex v1 = graph[vid1];
273  pgrouting::contraction::Vertex v2 = graph[vid2];
274  pgrouting::contraction::Vertex v4 = graph[vid4];
275  pgrouting::contraction::Vertex v5 = graph[vid5];
276  pgrouting::contraction::Vertex v7 = graph[vid7];
277  pgrouting::contraction::Vertex v8 = graph[vid8];
278  pgrouting::contraction::Vertex v10 = graph[vid10];
279  pgrouting::contraction::Vertex v12 = graph[vid12];
280  pgrouting::contraction::Vertex v13 = graph[vid13];
281  pgrouting::contraction::Vertex v14 = graph[vid14];
282  pgrouting::contraction::Vertex v15 = graph[vid15];
283  pgrouting::contraction::Vertex v16 = graph[vid16];
284  pgrouting::contraction::Vertex v17 = graph[vid17];
285 
286  log << " - Dead end contraction:\n";
287  // vertex 1 is contracted to vertex 2
288  v2.add_contracted_vertex(v1, vid1);
289  log << "Vertex 1 is contracted to Vertex 2:\n";
290  log << v1;
291  log << v2;
292  // vertex 7 is contracted to vertex 8
293  v8.add_contracted_vertex(v7, vid7);
294  log << "Vertex 7 is contracted to Vertex 8:\n";
295  log << v7;
296  log << v8;
297  // vertex 8 is contracted to vertex 5
298  v5.add_contracted_vertex(v8, vid8);
299  log << "Vertex 8 is contracted to Vertex 5:\n";
300  log << v8;
301  log << v5;
302  // vertex 13 is contracted to vertex 10
303  v10.add_contracted_vertex(v13, vid13);
304  log << "Vertex 13 is contracted to Vertex 10:\n";
305  log << v13;
306  log << v10;
307  // vertex 14 is contracted to vertex 15
308  v15.add_contracted_vertex(v14, vid14);
309  log << "Vertex 14 is contracted to Vertex 15:\n";
310  log << v14;
311  log << v15;
312  // vertex 16 is contracted to vertex 17
313  v17.add_contracted_vertex(v16, vid16);
314  log << "Vertex 16 is contracted to Vertex 17:\n";
315  log << v16;
316  log << v17;
317 
318 
319  log << " - Linear contraction:\n";
324  // vertex 2 is contracted to edge -1
325  e1.id = -1;
326  e1.source = 3;
327  e1.target = 5;
328  log << "Vertex 1 is contracted to edge -1:\n";
329  e1.add_contracted_vertex(v2, vid2);
330  log << e1;
331  // vertex 4 is contracted to edge -2
332  e2.id = -2;
333  e2.source = 3;
334  e2.target = 9;
335  log << "Vertex 4 is contracted to edge -2:\n";
336  e2.add_contracted_vertex(v4, vid4);
337  log << e2;
338  // vertex 10 is contracted to edge -3
339  e3.id = -3;
340  e3.source = 5;
341  e3.target = 11;
342  log << "Vertex 10 is contracted to edge -3:\n";
343  e3.add_contracted_vertex(v10, vid10);
344  log << e3;
345  // vertex 12 is contracted to edge -4
346  e4.id = -4;
347  e4.source = 9;
348  e4.target = 11;
349  log << "Vertex 12 is contracted to edge -4:\n";
350  e4.add_contracted_vertex(v12, vid12);
351  log << e4;
352  #if 0
353 
355  contractor.getDeadEndSet(graph);
356  log << " - Dead end vertices:\n";
357  contractor.print_dead_end_vertices(log);
358 
359  #endif
360  }
361  #endif
362 
363 
364  *err_msg = NULL;
365  *log_msg = strdup(log.str().c_str());
366  return true;
367 
368  } catch (AssertFailedException &except) {
369  log << except.what() << "\n";
370  *err_msg = strdup(log.str().c_str());
371  return false;
372  } catch (std::exception& except) {
373  log << except.what() << "\n";
374  *err_msg = strdup(log.str().c_str());
375  return false;
376  } catch(...) {
377  log << "Caught unknown exception!\n";
378  *err_msg = strdup(log.str().c_str());
379  return false;
380  }
381 }
void add_contracted_vertex(Vertex &v, int64_t vid)
Definition: ch_vertex.cpp:48
double cost
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
void getDeadEndSet(G &graph)
Stores ids of dead end vertices of the graph in a set Stores them in the set dead_end_vertices ...
void print_dead_end_vertices(std::ostringstream &stream)
Writes the string form of dead end vertices to the stream
bool do_pgr_test_c_edges(pgr_edge_t *data_edges, size_t total_edges, char **log_msg, char **err_msg)
V get_V(const T_V &vertex)
get the vertex descriptor of the vertex
void add_contracted_vertex(Vertex &v, int64_t vid)
Definition: ch_edge.cpp:95
void pgr_dijkstra(G &graph, Path &path, int64_t source, int64_t target, bool only_cost=false)
void insert(const Identifiers< T > &other)
Inserts a set of identifiers of type Identifiers<T> to this set.
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
edge_astar_t * edges
Definition: BDATester.cpp:46
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
void graph_insert_data(const T *edges, int64_t count)
Inserts count edges of type T into the graph.
double reverse_cost
std::vector< Vertex > extract_vertices(const std::vector< pgr_edge_t > &data_edges)
Definition: ch_vertex.cpp:84