pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
dijkstraVia.c
1 /*PGR-GNU*****************************************************************
2 File: dijkstraViaVertex.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 
7 Function's developer:
8 Copyright (c) 2015 Celia Virginia Vergara Castillo
9 
10 ------
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 
26 ********************************************************************PGR-GNU*/
27 
28 #include "postgres.h"
29 #include "executor/spi.h"
30 #include "funcapi.h"
31 #include "utils/array.h"
32 #include "catalog/pg_type.h"
33 #if PGSQL_VERSION > 92
34 #include "access/htup_details.h"
35 #endif
36 
37 /*
38  Uncomment when needed
39 */
40 // #define DEBUG
41 
42 #include "fmgr.h"
43 #include "./../../common/src/debug_macro.h"
44 #include "./../../common/src/time_msg.h"
45 #include "./../../common/src/pgr_types.h"
46 #include "./../../common/src/postgres_connection.h"
47 #include "./../../common/src/edges_input.h"
48 #include "./../../common/src/arrays_input.h"
49 #include "./dijkstraVia_driver.h"
50 
51 PG_FUNCTION_INFO_V1(dijkstraVia);
52 #ifndef _MSC_VER
53 Datum
54 #else // _MSC_VER
55 PGDLLEXPORT Datum
56 #endif
57 dijkstraVia(PG_FUNCTION_ARGS);
58 
59 /*******************************************************************************/
60 /* MODIFY AS NEEDED */
61 static
62 void
63 process( char* edges_sql,
64  int64_t *via_vidsArr,
65  size_t size_via_vidsArr,
66  bool directed,
67  bool strict,
68  bool U_turn_on_edge,
69  Routes_t **result_tuples,
70  size_t *result_count) {
71  pgr_SPI_connect();
72 
73  PGR_DBG("Load data");
74  pgr_edge_t *edges;
75  edges = NULL;
76  size_t total_tuples;
77  total_tuples = 0;
78  pgr_get_data_5_columns(edges_sql, &edges, &total_tuples);
79 
80  if (total_tuples == 0) {
81  PGR_DBG("No edges found");
82  (*result_count) = 0;
83  (*result_tuples) = NULL;
84  pgr_SPI_finish();
85  return;
86  }
87  PGR_DBG("Total %ld tuples in query:", total_tuples);
88  PGR_DBG("directed: %d", directed);
89  PGR_DBG("strict: %d", strict);
90  PGR_DBG("U_turn_on_edge: %d", U_turn_on_edge);
91 
92 
93  PGR_DBG("Starting processing");
94  clock_t start_t = clock();
95 
96  char *err_msg = (char *)"";
97  do_pgr_dijkstraViaVertex(
98  edges,
99  total_tuples,
100  via_vidsArr,
101  size_via_vidsArr,
102  directed,
103  strict,
104  U_turn_on_edge,
105  result_tuples,
106  result_count,
107  &err_msg);
108  time_msg(" processing Dijkstra Via", start_t, clock());
109  PGR_DBG("Returning %ld tuples\n", *result_count);
110  PGR_DBG("Returned message = %s\n", err_msg);
111 
112  free(err_msg);
113  pfree(edges);
114  pgr_SPI_finish();
115 }
116 /* */
117 /*******************************************************************************/
118 
119 #ifndef _MSC_VER
120 Datum
121 #else // _MSC_VER
122 PGDLLEXPORT Datum
123 #endif
124 dijkstraVia(PG_FUNCTION_ARGS) {
125  FuncCallContext *funcctx;
126  uint32_t call_cntr;
127  uint32_t max_calls;
128  TupleDesc tuple_desc;
129 
130  /*******************************************************************************/
131  /* MODIFY AS NEEDED */
132  /* */
133  Routes_t *result_tuples = 0;
134  size_t result_count = 0;
135  /* */
136  /*******************************************************************************/
137 
138  if (SRF_IS_FIRSTCALL()) {
139  MemoryContext oldcontext;
140  funcctx = SRF_FIRSTCALL_INIT();
141  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
142 
143 
144  /*******************************************************************************/
145  /* MODIFY AS NEEDED
146  * pgr_dijkstraVia(edges_sql text,
147  * vertices anyarray,
148  * directed boolean default true,
149  * strict boolean default false,
150  * U_turn_on_edge boolean default false,
151  *******************************************************************************/
152 
153  PGR_DBG("Initializing arrays");
154  int64_t* via_vidsArr;
155  size_t size_via_vidsArr;
156  via_vidsArr = (int64_t*) pgr_get_bigIntArray(&size_via_vidsArr, PG_GETARG_ARRAYTYPE_P(1));
157  PGR_DBG("Via VertexArr size %ld ", size_via_vidsArr);
158 
159  PGR_DBG("Calling process");
160  process(
161  pgr_text2char(PG_GETARG_TEXT_P(0)),
162  via_vidsArr, size_via_vidsArr,
163  PG_GETARG_BOOL(2),
164  PG_GETARG_BOOL(3),
165  PG_GETARG_BOOL(4),
166  &result_tuples,
167  &result_count);
168 
169  PGR_DBG("Cleaning arrays");
170  free(via_vidsArr);
171  /* */
172  /*******************************************************************************/
173 
174  funcctx->max_calls = (uint32_t)result_count;
175  funcctx->user_fctx = result_tuples;
176  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
177  ereport(ERROR,
178  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
179  errmsg("function returning record called in context "
180  "that cannot accept type record")));
181 
182  funcctx->tuple_desc = tuple_desc;
183  MemoryContextSwitchTo(oldcontext);
184  }
185 
186  funcctx = SRF_PERCALL_SETUP();
187  call_cntr = funcctx->call_cntr;
188  max_calls = funcctx->max_calls;
189  tuple_desc = funcctx->tuple_desc;
190  result_tuples = (Routes_t*) funcctx->user_fctx;
191 
192  if (call_cntr < max_calls) {
193  HeapTuple tuple;
194  Datum result;
195  Datum *values;
196  char* nulls;
197 
198  /*******************************************************************************/
199  /* MODIFY AS NEEDED */
200  // OUT seq BIGINT, OUT path_id BIGINT, OUT path_seq BIGINT, OUT start_vid BIGINT, OUT end_vid BIGINT, OUT node BIGINT, OUT edge BIGINT, OUT cost FLOAT, OUT agg_cost FLOAT, OUT route_agg_cost FLOAT)
201 
202  size_t numb_out = 10;
203  values = palloc(numb_out * sizeof(Datum));
204  nulls = palloc(numb_out * sizeof(char));
205  size_t i;
206  for(i = 0; i< numb_out; ++i) {
207  nulls[i] = ' ';
208  }
209 
210  // postgres starts counting from 1
211  values[0] = Int32GetDatum(call_cntr + 1);
212  values[1] = Int32GetDatum(result_tuples[call_cntr].path_id);
213  values[2] = Int32GetDatum(result_tuples[call_cntr].path_seq + 1);
214  values[3] = Int64GetDatum(result_tuples[call_cntr].start_vid);
215  values[4] = Int64GetDatum(result_tuples[call_cntr].end_vid);
216  values[5] = Int64GetDatum(result_tuples[call_cntr].node);
217  values[6] = Int64GetDatum(result_tuples[call_cntr].edge);
218  values[7] = Float8GetDatum(result_tuples[call_cntr].cost);
219  values[8] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
220  values[9] = Float8GetDatum(result_tuples[call_cntr].route_agg_cost);
221 
222  /*******************************************************************************/
223 
224  tuple = heap_formtuple(tuple_desc, values, nulls);
225  result = HeapTupleGetDatum(tuple);
226  SRF_RETURN_NEXT(funcctx, result);
227  } else {
228  // cleanup
229  if (result_tuples) free(result_tuples);
230 
231  SRF_RETURN_DONE(funcctx);
232  }
233 }