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
dijkstraVia.c
Go to the documentation of this file.
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 PGDLLEXPORT Datum dijkstraVia(PG_FUNCTION_ARGS);
52 
53 /*******************************************************************************/
54 /* MODIFY AS NEEDED */
55 static
56 void
57 process( char* edges_sql,
58  int64_t *via_vidsArr,
59  size_t size_via_vidsArr,
60  bool directed,
61  bool strict,
62  bool U_turn_on_edge,
63  Routes_t **result_tuples,
64  size_t *result_count) {
66 
67  PGR_DBG("Load data");
69  edges = NULL;
70  size_t total_tuples;
71  total_tuples = 0;
72  pgr_get_edges(edges_sql, &edges, &total_tuples);
73 
74  if (total_tuples == 0) {
75  PGR_DBG("No edges found");
76  (*result_count) = 0;
77  (*result_tuples) = NULL;
79  return;
80  }
81  PGR_DBG("Total %ld tuples in query:", total_tuples);
82  PGR_DBG("directed: %d", directed);
83  PGR_DBG("strict: %d", strict);
84  PGR_DBG("U_turn_on_edge: %d", U_turn_on_edge);
85 
86 
87  PGR_DBG("Starting processing");
88  clock_t start_t = clock();
89 
90  char *err_msg = (char *)"";
92  edges,
93  total_tuples,
94  via_vidsArr,
95  size_via_vidsArr,
96  directed,
97  strict,
98  U_turn_on_edge,
99  result_tuples,
100  result_count,
101  &err_msg);
102  time_msg(" processing Dijkstra Via", start_t, clock());
103  PGR_DBG("Returning %ld tuples\n", *result_count);
104  PGR_DBG("Returned message = %s\n", err_msg);
105 
106  free(err_msg);
107  pfree(edges);
108  pgr_SPI_finish();
109 }
110 /* */
111 /*******************************************************************************/
112 
114 PGDLLEXPORT Datum
115 dijkstraVia(PG_FUNCTION_ARGS) {
116  FuncCallContext *funcctx;
117  uint32_t call_cntr;
118  uint32_t max_calls;
119  TupleDesc tuple_desc;
120 
121  /*******************************************************************************/
122  /* MODIFY AS NEEDED */
123  /* */
124  Routes_t *result_tuples = 0;
125  size_t result_count = 0;
126  /* */
127  /*******************************************************************************/
128 
129  if (SRF_IS_FIRSTCALL()) {
130  MemoryContext oldcontext;
131  funcctx = SRF_FIRSTCALL_INIT();
132  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
133 
134 
135  /*******************************************************************************/
136  /* MODIFY AS NEEDED
137  * pgr_dijkstraVia(edges_sql text,
138  * vertices anyarray,
139  * directed boolean default true,
140  * strict boolean default false,
141  * U_turn_on_edge boolean default false,
142  *******************************************************************************/
143 
144  PGR_DBG("Initializing arrays");
145  int64_t* via_vidsArr;
146  size_t size_via_vidsArr;
147  via_vidsArr = (int64_t*) pgr_get_bigIntArray(&size_via_vidsArr, PG_GETARG_ARRAYTYPE_P(1));
148  PGR_DBG("Via VertexArr size %ld ", size_via_vidsArr);
149 
150  PGR_DBG("Calling process");
151  process(
152  pgr_text2char(PG_GETARG_TEXT_P(0)),
153  via_vidsArr, size_via_vidsArr,
154  PG_GETARG_BOOL(2),
155  PG_GETARG_BOOL(3),
156  PG_GETARG_BOOL(4),
157  &result_tuples,
158  &result_count);
159 
160  PGR_DBG("Cleaning arrays");
161  free(via_vidsArr);
162  /* */
163  /*******************************************************************************/
164 
165  funcctx->max_calls = (uint32_t)result_count;
166  funcctx->user_fctx = result_tuples;
167  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
168  ereport(ERROR,
169  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
170  errmsg("function returning record called in context "
171  "that cannot accept type record")));
172 
173  funcctx->tuple_desc = tuple_desc;
174  MemoryContextSwitchTo(oldcontext);
175  }
176 
177  funcctx = SRF_PERCALL_SETUP();
178  call_cntr = funcctx->call_cntr;
179  max_calls = funcctx->max_calls;
180  tuple_desc = funcctx->tuple_desc;
181  result_tuples = (Routes_t*) funcctx->user_fctx;
182 
183  if (call_cntr < max_calls) {
184  HeapTuple tuple;
185  Datum result;
186  Datum *values;
187  bool* nulls;
188 
189  /*******************************************************************************/
190  /* MODIFY AS NEEDED */
191  // 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)
192 
193  size_t numb_out = 10;
194  values = palloc(numb_out * sizeof(Datum));
195  nulls = palloc(numb_out * sizeof(bool));
196  size_t i;
197  for(i = 0; i< numb_out; ++i) {
198  nulls[i] = false;
199  }
200 
201  // postgres starts counting from 1
202  values[0] = Int32GetDatum(call_cntr + 1);
203  values[1] = Int32GetDatum(result_tuples[call_cntr].path_id);
204  values[2] = Int32GetDatum(result_tuples[call_cntr].path_seq + 1);
205  values[3] = Int64GetDatum(result_tuples[call_cntr].start_vid);
206  values[4] = Int64GetDatum(result_tuples[call_cntr].end_vid);
207  values[5] = Int64GetDatum(result_tuples[call_cntr].node);
208  values[6] = Int64GetDatum(result_tuples[call_cntr].edge);
209  values[7] = Float8GetDatum(result_tuples[call_cntr].cost);
210  values[8] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
211  values[9] = Float8GetDatum(result_tuples[call_cntr].route_agg_cost);
212 
213  /*******************************************************************************/
214 
215  tuple = heap_form_tuple(tuple_desc, values, nulls);
216  result = HeapTupleGetDatum(tuple);
217  SRF_RETURN_NEXT(funcctx, result);
218  } else {
219  // cleanup
220  if (result_tuples) free(result_tuples);
221 
222  SRF_RETURN_DONE(funcctx);
223  }
224 }
static void process(char *edges_sql, int64_t *via_vidsArr, size_t size_via_vidsArr, bool directed, bool strict, bool U_turn_on_edge, Routes_t **result_tuples, size_t *result_count)
Definition: dijkstraVia.c:57
#define PGR_DBG(...)
Definition: debug_macro.h:33
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:535
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Definition: arrays_input.c:38
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
PG_FUNCTION_INFO_V1(dijkstraVia)
PGDLLEXPORT Datum dijkstraVia(PG_FUNCTION_ARGS)
Definition: dijkstraVia.c:115
void pgr_SPI_finish(void)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
void do_pgr_dijkstraViaVertex(pgr_edge_t *data_edges, size_t total_tuples, int64_t *via_vidsArr, size_t size_via_vidsArr, bool directed, bool strict, bool U_turn_on_edge, Routes_t **return_tuples, size_t *return_count, char **err_msg)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)