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
astarOneToMany.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: astarOneToOne.c
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 #include "postgres.h"
31 #include "executor/spi.h"
32 #include "funcapi.h"
33 #include "utils/array.h"
34 #include "catalog/pg_type.h"
35 #if PGSQL_VERSION > 92
36 #include "access/htup_details.h"
37 #endif
38 #include "fmgr.h"
39 
40 #include "./../../common/src/debug_macro.h"
41 #include "./../../common/src/time_msg.h"
42 #include "./../../common/src/pgr_types.h"
43 #include "./../../common/src/postgres_connection.h"
44 #include "./../../common/src/edges_input.h"
45 #include "./../../common/src/arrays_input.h"
46 
48 
50 PGDLLEXPORT Datum
51 astarOneToMany(PG_FUNCTION_ARGS);
52 
53 
54 static
55 void
56 process(char* edges_sql,
57  int64_t start_vid,
58  int64_t *end_vidsArr, size_t size_end_vidsArr,
59  bool directed,
60  int heuristic,
61  double factor,
62  double epsilon,
63  bool only_cost,
64  General_path_element_t **result_tuples,
65  size_t *result_count) {
66  if (heuristic > 5 || heuristic < 0) {
67  free(end_vidsArr);
68  ereport(ERROR,
69  (errmsg("Unknown heuristic"),
70  errhint("Valid values: 0~5")));
71  }
72  if (factor <= 0) {
73  free(end_vidsArr);
74  ereport(ERROR,
75  (errmsg("Factor value out of range"),
76  errhint("Valid values: positive non zero")));
77  }
78  if (epsilon < 1) {
79  free(end_vidsArr);
80  ereport(ERROR,
81  (errmsg("Epsilon value out of range"),
82  errhint("Valid values: 1 or greater than 1")));
83  }
84 
85 
86 
88 
89  Pgr_edge_xy_t *edges = NULL;
90  size_t total_edges = 0;
91 
92  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
93 
94  if (total_edges == 0) {
95  PGR_DBG("No edges found");
96  (*result_count) = 0;
97  (*result_tuples) = NULL;
99  return;
100  }
101 
102  PGR_DBG("Starting processing");
103  char *err_msg = NULL;
104  char *log_msg = NULL;
105  clock_t start_t = clock();
107  edges,
108  total_edges,
109  start_vid,
110  end_vidsArr, size_end_vidsArr,
111  directed,
112  heuristic,
113  factor,
114  epsilon,
115  only_cost,
116  result_tuples,
117  result_count,
118  &log_msg,
119  &err_msg,
120  true);
121  time_msg(" processing pgr_astar(one to Many)", start_t, clock());
122  PGR_DBG("Returning %ld tuples\n", *result_count);
123  PGR_DBG("LOG: %s\n", log_msg);
124  if (log_msg) free(log_msg);
125 
126  if (err_msg) {
127  if (*result_tuples) free(*result_tuples);
128  elog(ERROR, "%s", err_msg);
129  free(err_msg);
130  }
131 
132  pfree(edges);
133  pgr_SPI_finish();
134 }
135 
136 PGDLLEXPORT Datum
137 astarOneToMany(PG_FUNCTION_ARGS) {
138  FuncCallContext *funcctx;
139  uint32_t call_cntr;
140  uint32_t max_calls;
141  TupleDesc tuple_desc;
142 
143  General_path_element_t *result_tuples = 0;
144  size_t result_count = 0;
145 
146  if (SRF_IS_FIRSTCALL()) {
147  MemoryContext oldcontext;
148  funcctx = SRF_FIRSTCALL_INIT();
149  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
150 
151 
152  /*****************************************************************
153  edges_sql TEXT,
154  start_vid BIGINT,
155  end_vids ARRAY[ANY_INTEGER], -- anyarray
156  directed BOOLEAN DEFAULT true,
157  heuristic INTEGER DEFAULT 0,
158  factor FLOAT DEFAULT 1.0,
159  epsilon FLOAT DEFAULT 1.0,
160 
161  ****************************************************************/
162 
163  int64_t* end_vidsArr;
164  size_t size_end_vidsArr;
165  end_vidsArr = (int64_t*)
166  pgr_get_bigIntArray(&size_end_vidsArr, PG_GETARG_ARRAYTYPE_P(2));
167 
168  process(
169  pgr_text2char(PG_GETARG_TEXT_P(0)),
170  PG_GETARG_INT64(1),
171  end_vidsArr, size_end_vidsArr,
172  PG_GETARG_BOOL(3),
173  PG_GETARG_INT32(4),
174  PG_GETARG_FLOAT8(5),
175  PG_GETARG_FLOAT8(6),
176  PG_GETARG_BOOL(7),
177  &result_tuples,
178  &result_count);
179 
180  free(end_vidsArr);
181 
182 
183  funcctx->max_calls = (uint32_t) result_count;
184  funcctx->user_fctx = result_tuples;
185  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
186  ereport(ERROR,
187  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
188  errmsg("function returning record called in context "
189  "that cannot accept type record")));
190 
191  funcctx->tuple_desc = tuple_desc;
192  MemoryContextSwitchTo(oldcontext);
193  }
194 
195  funcctx = SRF_PERCALL_SETUP();
196  call_cntr = funcctx->call_cntr;
197  max_calls = funcctx->max_calls;
198  tuple_desc = funcctx->tuple_desc;
199  result_tuples = (General_path_element_t*) funcctx->user_fctx;
200 
201  if (call_cntr < max_calls) {
202  HeapTuple tuple;
203  Datum result;
204  Datum *values;
205  bool* nulls;
206 
207  /*********************************************************************
208  OUT seq INTEGER,
209  OUT path_seq INTEGER,
210  OUT end_vid BIGINT,
211  OUT node BIGINT,
212  OUT edge BIGINT,
213  OUT cost FLOAT,
214  OUT agg_cost FLOAT
215  *********************************************************************/
216 
217 
218  values = palloc(7 * sizeof(Datum));
219  nulls = palloc(7 * sizeof(bool));
220 
221  size_t i;
222  for (i = 0; i < 7; ++i) {
223  nulls[i] = false;
224  }
225 
226 
227  values[0] = Int32GetDatum(call_cntr + 1);
228  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
229  values[2] = Int64GetDatum(result_tuples[call_cntr].end_id);
230  values[3] = Int64GetDatum(result_tuples[call_cntr].node);
231  values[4] = Int64GetDatum(result_tuples[call_cntr].edge);
232  values[5] = Float8GetDatum(result_tuples[call_cntr].cost);
233  values[6] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
234 
235 
236  tuple = heap_form_tuple(tuple_desc, values, nulls);
237  result = HeapTupleGetDatum(tuple);
238  SRF_RETURN_NEXT(funcctx, result);
239  } else {
240  if (result_tuples) free(result_tuples);
241  SRF_RETURN_DONE(funcctx);
242  }
243 }
244 
PGDLLEXPORT Datum astarOneToMany(PG_FUNCTION_ARGS)
#define PGR_DBG(...)
Definition: debug_macro.h:33
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(astarOneToMany)
void pgr_SPI_finish(void)
static void process(char *edges_sql, int64_t start_vid, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
void pgr_get_edges_xy(char *edges_sql, Pgr_edge_xy_t **edges, size_t *total_edges)
Edges with x, y vertices values.
Definition: edges_input.c:553
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)
void do_pgr_astarOneToMany(Pgr_edge_xy_t *edges, size_t total_edges, int64_t start_vid, int64_t *end_vidsArr, size_t size_end_vidsArr, 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, bool normal)