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
astarOneToOne.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 
46 #include "./astarOneToOne_driver.h"
47 
48 PGDLLEXPORT Datum astarOneToOne(PG_FUNCTION_ARGS);
49 
50 
51 static
52 void
53 process(char* edges_sql,
54  int64_t start_vid,
55  int64_t end_vid,
56  bool directed,
57  int heuristic,
58  double factor,
59  double epsilon,
60  bool only_cost,
61  General_path_element_t **result_tuples,
62  size_t *result_count) {
63  if (heuristic > 5 || heuristic < 0) {
64  ereport(ERROR,
65  (errmsg("Unknown heuristic"),
66  errhint("Valid values: 0~5")));
67  }
68  if (factor <= 0) {
69  ereport(ERROR,
70  (errmsg("Factor value out of range"),
71  errhint("Valid values: positive non zero")));
72  }
73  if (epsilon < 1) {
74  ereport(ERROR,
75  (errmsg("Epsilon value out of range"),
76  errhint("Valid values: 1 or greater than 1")));
77  }
78 
79 
80 
82 
83  PGR_DBG("Load data");
84  Pgr_edge_xy_t *edges = NULL;
85  size_t total_edges = 0;
86 
87  pgr_get_edges_xy(edges_sql, &edges, &total_edges);
88  PGR_DBG("Total %ld edges in query:", total_edges);
89 
90  if (total_edges == 0) {
91  PGR_DBG("No edges found");
92  (*result_count) = 0;
93  (*result_tuples) = NULL;
95  return;
96  }
97 
98  PGR_DBG("Starting processing");
99  char *err_msg = NULL;
100  char *log_msg = NULL;
101  clock_t start_t = clock();
103  edges,
104  total_edges,
105  start_vid,
106  end_vid,
107  directed,
108  heuristic,
109  factor,
110  epsilon,
111  only_cost,
112  result_tuples,
113  result_count,
114  &log_msg,
115  &err_msg);
116  time_msg(" processing pgr_astar(one to one)", start_t, clock());
117 
118  PGR_DBG("Returning %ld tuples\n", *result_count);
119  PGR_DBG("LOG: %s\n", log_msg);
120  if (log_msg) free(log_msg);
121 
122  if (err_msg) {
123  if (*result_tuples) free(*result_tuples);
124  elog(ERROR, "%s", err_msg);
125  free(err_msg);
126  }
127 
128  pfree(edges);
129  pgr_SPI_finish();
130 }
131 
133 PGDLLEXPORT Datum
134 astarOneToOne(PG_FUNCTION_ARGS) {
135  FuncCallContext *funcctx;
136  uint32_t call_cntr;
137  uint32_t max_calls;
138  TupleDesc tuple_desc;
139 
140  General_path_element_t *result_tuples = 0;
141  size_t result_count = 0;
142 
143  if (SRF_IS_FIRSTCALL()) {
144  MemoryContext oldcontext;
145  funcctx = SRF_FIRSTCALL_INIT();
146  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
147 
148 
149  /**********************************************************************
150  edges_sql TEXT,
151  start_vid BIGINT,
152  end_vid BIGINT,
153  directed BOOLEAN DEFAULT true,
154  heuristic INTEGER DEFAULT 0,
155  factor FLOAT DEFAULT 1.0,
156  epsilon FLOAT DEFAULT 1.0,
157 
158  **********************************************************************/
159 
160  PGR_DBG("Calling process");
161  process(
162  pgr_text2char(PG_GETARG_TEXT_P(0)),
163  PG_GETARG_INT64(1),
164  PG_GETARG_INT64(2),
165  PG_GETARG_BOOL(3),
166  PG_GETARG_INT32(4),
167  PG_GETARG_FLOAT8(5),
168  PG_GETARG_FLOAT8(6),
169  PG_GETARG_BOOL(7),
170  &result_tuples,
171  &result_count);
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 = (General_path_element_t*) funcctx->user_fctx;
191 
192  if (call_cntr < max_calls) {
193  HeapTuple tuple;
194  Datum result;
195  Datum *values;
196  bool* nulls;
197 
198  /**********************************************************************
199  OUT seq INTEGER,
200  OUT path_seq INTEGER,
201  OUT node BIGINT,
202  OUT edge BIGINT,
203  OUT cost FLOAT,
204  OUT agg_cost FLOAT
205  *********************************************************************/
206 
207 
208  values = palloc(6 * sizeof(Datum));
209  nulls = palloc(6 * sizeof(bool));
210 
211  size_t i;
212  for (i = 0; i < 6; ++i) {
213  nulls[i] = false;
214  }
215 
216 
217  values[0] = Int32GetDatum(call_cntr + 1);
218  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
219  values[2] = Int64GetDatum(result_tuples[call_cntr].node);
220  values[3] = Int64GetDatum(result_tuples[call_cntr].edge);
221  values[4] = Float8GetDatum(result_tuples[call_cntr].cost);
222  values[5] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
223 
224 
225  tuple = heap_form_tuple(tuple_desc, values, nulls);
226  result = HeapTupleGetDatum(tuple);
227  SRF_RETURN_NEXT(funcctx, result);
228  } else {
229  // cleanup
230  if (result_tuples) free(result_tuples);
231 
232  SRF_RETURN_DONE(funcctx);
233  }
234 }
235 
#define PGR_DBG(...)
Definition: debug_macro.h:33
void do_pgr_astarOneToOne(Pgr_edge_xy_t *data_edges, size_t total_edges, int64_t start_vid, int64_t end_vid, 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)
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
void pgr_SPI_finish(void)
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
PG_FUNCTION_INFO_V1(astarOneToOne)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
PGDLLEXPORT Datum astarOneToOne(PG_FUNCTION_ARGS)
static void process(char *edges_sql, int64_t start_vid, int64_t end_vid, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
Definition: astarOneToOne.c:53
char * pgr_text2char(text *in)