PGROUTING  3.2
withPoints.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: withPoints.c
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
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 <stdbool.h>
32 #include "utils/array.h"
33 
34 #include "c_common/debug_macro.h"
35 #include "c_common/e_report.h"
36 #include "c_common/time_msg.h"
37 
38 #include "c_common/edges_input.h"
39 #include "c_common/arrays_input.h"
40 #include "c_common/points_input.h"
44 
45 PGDLLEXPORT Datum _pgr_withpoints(PG_FUNCTION_ARGS);
47 
48 
49 static
50 void
52  char* edges_sql,
53  char* points_sql,
54  char* combinations_sql,
55 
56  ArrayType *starts,
57  ArrayType *ends,
58 
59  bool directed,
60  char *driving_side,
61  bool details,
62  bool only_cost,
63  bool normal,
64 
65  General_path_element_t **result_tuples,
66  size_t *result_count) {
67  driving_side[0] = estimate_drivingSide(driving_side[0]);
68 
70 
71  size_t size_start_pidsArr = 0;
72  int64_t* start_pidsArr = NULL;
73 
74  size_t size_end_pidsArr = 0;
75  int64_t* end_pidsArr = NULL;
76 
77  pgr_combination_t *combinations = NULL;
78  size_t total_combinations = 0;
79 
80  Point_on_edge_t *points = NULL;
81  size_t total_points = 0;
82  pgr_get_points(points_sql, &points, &total_points);
83 
84 #ifndef NDEBUG
85  size_t i;
86  for (i = 0; i< total_points; i++) {
87  PGR_DBG("%ld ", points[i].pid);
88  }
89 #endif
90  char *edges_of_points_query = NULL;
91  char *edges_no_points_query = NULL;
93  edges_sql, points_sql,
94  &edges_of_points_query,
95  &edges_no_points_query);
96 
97 
98  pgr_edge_t *edges_of_points = NULL;
99  size_t total_edges_of_points = 0;
100 
101  pgr_edge_t *edges = NULL;
102  size_t total_edges = 0;
103 
104  if (normal) {
106  edges_of_points_query,
107  &edges_of_points,
108  &total_edges_of_points);
109  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
110 
111  if (starts && ends) {
112  start_pidsArr = (int64_t*)
113  pgr_get_bigIntArray(&size_start_pidsArr, starts);
114  end_pidsArr = (int64_t*)
115  pgr_get_bigIntArray(&size_end_pidsArr, ends);
116  } else if (combinations_sql) {
117  pgr_get_combinations(combinations_sql, &combinations, &total_combinations);
118  }
119  } else {
121  edges_of_points_query,
122  &edges_of_points,
123  &total_edges_of_points);
124  pgr_get_edges_reversed(edges_no_points_query, &edges, &total_edges);
125 
126  end_pidsArr = (int64_t*)
127  pgr_get_bigIntArray(&size_end_pidsArr, starts);
128  start_pidsArr = (int64_t*)
129  pgr_get_bigIntArray(&size_start_pidsArr, ends);
130  }
131 
132 
133  pfree(edges_of_points_query);
134  pfree(edges_no_points_query);
135  edges_of_points_query = NULL;
136  edges_no_points_query = NULL;
137 
138  if ((total_edges + total_edges_of_points) == 0) {
139  pgr_SPI_finish();
140  return;
141  }
142 
143  clock_t start_t = clock();
144  char* log_msg = NULL;
145  char* notice_msg = NULL;
146  char* err_msg = NULL;
147 
149  edges, total_edges,
150  points, total_points,
151  edges_of_points, total_edges_of_points,
152 
153  combinations, total_combinations,
154 
155  start_pidsArr, size_start_pidsArr,
156  end_pidsArr, size_end_pidsArr,
157 
158  driving_side[0],
159  details,
160  directed,
161  only_cost,
162  normal,
163 
164  result_tuples, result_count,
165  &log_msg,
166  &notice_msg,
167  &err_msg);
168 
169  if (only_cost) {
170  time_msg("processing pgr_withPointsCost", start_t, clock());
171  } else {
172  time_msg("processing pgr_withPoints", start_t, clock());
173  }
174 
175  if (err_msg && (*result_tuples)) {
176  pfree(*result_tuples);
177  (*result_count) = 0;
178  (*result_tuples) = NULL;
179  }
180 
181  pgr_global_report(log_msg, notice_msg, err_msg);
182 
183 #if 0
184  if (log_msg) pfree(log_msg);
185  if (notice_msg) pfree(notice_msg);
186  if (err_msg) pfree(err_msg);
187  if (edges) pfree(edges);
188  if (points) pfree(points);
189  if (edges_of_points) pfree(edges_of_points);
190  if (start_pidsArr) pfree(start_pidsArr);
191  if (end_pidsArr) pfree(end_pidsArr);
192 #endif
193  pgr_SPI_finish();
194 }
195 
196 
197 
198 
199 PGDLLEXPORT Datum
200 _pgr_withpoints(PG_FUNCTION_ARGS) {
201  FuncCallContext *funcctx;
202  TupleDesc tuple_desc;
203 
204  /**********************************************************************/
205  General_path_element_t *result_tuples = 0;
206  size_t result_count = 0;
207  /**********************************************************************/
208 
209  if (SRF_IS_FIRSTCALL()) {
210  MemoryContext oldcontext;
211  funcctx = SRF_FIRSTCALL_INIT();
212  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
213 
214 
215  if (PG_NARGS() == 9) {
216  /*
217  * many to many
218  */
219 
220  process(
221  text_to_cstring(PG_GETARG_TEXT_P(0)),
222  text_to_cstring(PG_GETARG_TEXT_P(1)),
223  NULL,
224  PG_GETARG_ARRAYTYPE_P(2),
225  PG_GETARG_ARRAYTYPE_P(3),
226  PG_GETARG_BOOL(4),
227  text_to_cstring(PG_GETARG_TEXT_P(5)),
228  PG_GETARG_BOOL(6),
229  PG_GETARG_BOOL(7),
230  PG_GETARG_BOOL(8),
231  &result_tuples,
232  &result_count);
233 
234  } else if (PG_NARGS() == 7) {
235  /*
236  * Combinations
237  */
238 
239  process(
240  text_to_cstring(PG_GETARG_TEXT_P(0)),
241  text_to_cstring(PG_GETARG_TEXT_P(1)),
242  text_to_cstring(PG_GETARG_TEXT_P(2)),
243  NULL,
244  NULL,
245  PG_GETARG_BOOL(3),
246  text_to_cstring(PG_GETARG_TEXT_P(4)),
247  PG_GETARG_BOOL(5),
248  PG_GETARG_BOOL(6),
249  true,
250  &result_tuples,
251  &result_count);
252  }
253 
254  /**********************************************************************/
255 
256 #if PGSQL_VERSION > 95
257  funcctx->max_calls = result_count;
258 #else
259  funcctx->max_calls = (uint32_t)result_count;
260 #endif
261  funcctx->user_fctx = result_tuples;
262  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
263  != TYPEFUNC_COMPOSITE)
264  ereport(ERROR,
265  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
266  errmsg("function returning record called in context "
267  "that cannot accept type record")));
268 
269  funcctx->tuple_desc = tuple_desc;
270  MemoryContextSwitchTo(oldcontext);
271  }
272 
273  funcctx = SRF_PERCALL_SETUP();
274  tuple_desc = funcctx->tuple_desc;
275  result_tuples = (General_path_element_t*) funcctx->user_fctx;
276 
277  if (funcctx->call_cntr < funcctx->max_calls) {
278  HeapTuple tuple;
279  Datum result;
280  Datum *values;
281  bool* nulls;
282 
283  /**********************************************************************/
284  // OUT seq BIGINT,
285  // OUT path_seq,
286  // OUT node BIGINT,
287  // OUT edge BIGINT,
288  // OUT cost FLOAT,
289  // OUT agg_cost FLOAT)
290 
291 
292  values = palloc(8 * sizeof(Datum));
293  nulls = palloc(8 * sizeof(bool));
294 
295  size_t i;
296  for (i = 0; i < 8; ++i) {
297  nulls[i] = false;
298  }
299 
300 
301  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
302  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
303  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
304  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
305  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
306  values[5] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
307  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
308  values[7] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
309  /**********************************************************************/
310 
311  tuple = heap_form_tuple(tuple_desc, values, nulls);
312  result = HeapTupleGetDatum(tuple);
313  SRF_RETURN_NEXT(funcctx, result);
314  } else {
315  SRF_RETURN_DONE(funcctx);
316  }
317 }
318 
combinations_input.h
General_path_element_t::edge
int64_t edge
Definition: general_path_element_t.h:42
time_msg.h
postgres_connection.h
estimate_drivingSide
char estimate_drivingSide(char driving_side)
Definition: get_new_queries.cpp:36
pgr_edge_t
Definition: pgr_edge_t.h:37
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
General_path_element_t::seq
int seq
Definition: general_path_element_t.h:38
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
pgr_get_edges_reversed
void pgr_get_edges_reversed(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
Definition: edges_input.c:722
pgr_get_points
void pgr_get_points(char *points_sql, Point_on_edge_t **points, size_t *total_points)
pgr_get_points
Definition: points_input.c:69
General_path_element_t::start_id
int64_t start_id
Definition: general_path_element_t.h:39
do_pgr_withPoints
void do_pgr_withPoints(pgr_edge_t *edges, size_t total_edges, Point_on_edge_t *points_p, size_t total_points, pgr_edge_t *edges_of_points, size_t total_edges_of_points, pgr_combination_t *combinations, size_t total_combinations, int64_t *start_pidsArr, size_t size_start_pidsArr, int64_t *end_pidsArr, size_t size_end_pidsArr, char driving_side, bool details, bool directed, bool only_cost, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: withPoints_driver.cpp:93
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_withpoints)
debug_macro.h
withPoints_driver.h
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
process
static void process(char *edges_sql, char *points_sql, char *combinations_sql, ArrayType *starts, ArrayType *ends, bool directed, char *driving_side, bool details, bool only_cost, bool normal, General_path_element_t **result_tuples, size_t *result_count)
Definition: withPoints.c:51
get_new_queries
void get_new_queries(char *edges_sql, char *points_sql, char **edges_of_points_query, char **edges_no_points_query)
Definition: get_new_queries.cpp:46
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
pgr_combination_t
Definition: pgr_combination_t.h:43
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
pgr_get_combinations
void pgr_get_combinations(char *combinations_sql, pgr_combination_t **combinations, size_t *total_combinations)
combinations_sql
Definition: combinations_input.c:147
Point_on_edge_t
Definition: point_on_edge_t.h:37
pgr_get_edges
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:711
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
get_new_queries.h
General_path_element_t::end_id
int64_t end_id
Definition: general_path_element_t.h:40
_pgr_withpoints
PGDLLEXPORT Datum _pgr_withpoints(PG_FUNCTION_ARGS)
Definition: withPoints.c:200
edges_input.h
points_input.h
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44