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