pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
testit.c
1 /*PGR-GNU*****************************************************************
2 
3 Copyright (c) 2015 pgRouting developers
4 Mail: project@pgrouting.org
5 
6 ------
7 
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 
22 ********************************************************************PGR-GNU*/
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <limits.h>
26 #include <assert.h>
27 
28 typedef double float8;
29 typedef int bool;
30 
31 typedef struct edge
32 {
33  int id;
34  int source;
35  int target;
36  float8 cost;
37  float8 reverse_cost;
38 } edge_t;
39 
40 typedef struct path_element
41 {
42  int vertex_id;
43  int edge_id;
44  float8 cost;
46 
47 #ifdef __cplusplus
48 extern "C"
49 #endif
50 int boost_dijkstra(edge_t *edges, unsigned int count, int start_vertex,
51  int end_vertex, bool directed, bool has_reverse_cost,
52  path_element_t **path, int *path_count, char **err_msg);
53 
54 static edge_t edges[23] = {
55  {134034453,4401484,4401467,1491.35562366006,0.0},
56  {134034463,4401489,4401485,12.1046145920234,0.0},
57  {134034458,4401487,4401477,177.374061187772,0.0},
58  {134095663,4401465,4401476,2014.71852797927,0.0},
59  {134034459,4401485,4401487,201.968135582698,0.0},
60  {134095834,4401478,4401465,293.089423713387,0.0},
61  {134034461,4401488,4401489,418.687894948968,0.0},
62  {629678698,4415069,134860887,3776.78929640359,0.0},
63  {134034456,4401477,4401481,491.242305990214,0.0},
64  {134095832,4401482,4401487,76.1157555542275,0.0},
65  {134034465,4401490,4401489,1956.98967514724,0.0},
66  {134034454,4401483,4401486,1356.25190452873,0.0},
67  {134034462,4401487,4401478,17.2205341642897,0.0},
68  {134095833,4401477,4401478,2014.66722340654,0.0},
69  {134034455,4401485,4401483,53.5613132396201,0.0},
70  {134034467,4401488,4417647,2597.20106449741,0.0},
71  {134034452,4401483,4401467,350.071683838508,0.0},
72  {134034446,4401481,4401476,568.270689073724,0.0},
73  {134072383,4416226,4401482,322.141177736713,0.0},
74  {134034447,4401482,4401481,1522.8331095897,0.0},
75  {134034466,4401486,4401490,610.880612548267,0.0},
76  {134034468,4417647,4401486,507.803184036552,0.0},
77  {134034464,4401490,4401485,149.914370088613,0.0}
78 };
79 
80 int main(void) {
81  int total_tuples = 23;
82  int i;
83 
84  int ret;
85  char *err_msg = NULL;
86 
87  int start_vertex = 4401489;
88  int end_vertex = 4401483;
89 
90  int v_min_id = LONG_MAX;
91  int v_max_id = 0;
92 
93  int s_count = 0;
94  int t_count = 0;
95 
96  path_element_t *path = NULL;
97  int path_count = 0;
98 
99  for (i=0; i<total_tuples; i++) {
100  if(edges[i].source < v_min_id) v_min_id = edges[i].source;
101  if(edges[i].target < v_min_id) v_min_id = edges[i].target;
102  if(edges[i].source > v_max_id) v_max_id = edges[i].source;
103  if(edges[i].target > v_max_id) v_max_id = edges[i].target;
104  }
105  printf("%i <-> %i\n", v_min_id, v_max_id);
106 
107  for (i=0; i<total_tuples; i++) {
108  if (edges[i].source == start_vertex || edges[i].target == start_vertex)
109  s_count++;
110  if (edges[i].source == end_vertex || edges[i].target == end_vertex)
111  t_count++;
112  edges[i].source -= v_min_id;
113  edges[i].target -= v_min_id;
114  }
115 
116  assert(s_count);
117  assert(t_count);
118 
119  start_vertex -= v_min_id;
120  end_vertex -= v_min_id;
121 
122  ret = boost_dijkstra(edges, total_tuples, start_vertex, end_vertex,
123  0, 0, &path, &path_count, &err_msg);
124 
125  if (ret < 0) {
126  printf("Error computing path: %s\n", err_msg);
127  exit(1);
128  }
129 
130  printf(" seq: vertex_id, edge_id, cost\n");
131  for (i=0; i<path_count; i++) {
132  path[i].vertex_id += v_min_id;
133  printf("%6d: %10d, %10d, %10.4f\n", i, path[i].vertex_id, path[i].edge_id, path[i].cost);
134  }
135 
136  if (path) free(path);
137 
138  exit(0);
139 }