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
one_to_many_withPoints.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: one_to_many_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 
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 
39 
40 #include "fmgr.h"
41 #include "./../../common/src/debug_macro.h"
42 #include "./../../common/src/time_msg.h"
43 #include "./../../common/src/pgr_types.h"
44 #include "./../../common/src/postgres_connection.h"
45 #include "./../../common/src/edges_input.h"
46 #include "./../../common/src/arrays_input.h"
47 #include "./../../common/src/points_input.h"
48 #include "./get_new_queries.h"
50 
51 PGDLLEXPORT Datum one_to_many_withPoints(PG_FUNCTION_ARGS);
52 
53 
54 /*******************************************************************************/
55 /* MODIFY AS NEEDED */
56 static
57 void
59  char* edges_sql,
60  char* points_sql,
61  int64_t start_pid,
62  int64_t *end_pidsArr,
63  size_t size_end_pidsArr,
64  bool directed,
65  char *driving_side,
66  bool details,
67  bool only_cost,
68  General_path_element_t **result_tuples,
69  size_t *result_count) {
70  driving_side[0] = (char) tolower(driving_side[0]);
71  PGR_DBG("driving side:%c", driving_side[0]);
72  if (!((driving_side[0] == 'r')
73  || (driving_side[0] == 'l'))) {
74  driving_side[0] = 'b';
75  }
76  PGR_DBG("estimated driving side:%c", driving_side[0]);
78 
79  PGR_DBG("load the points");
80  Point_on_edge_t *points = NULL;
81  size_t total_points = 0;
82  pgr_get_points(points_sql, &points, &total_points);
83 
84 #if 0
85 #ifndef NDEBUG
86  size_t i = 0;
87  for (i = 0; i < total_points; i ++) {
88  PGR_DBG("%ld\t%ld\t%f\t%c", points[i].pid, points[i].edge_id, points[i].fraction, points[i].side);
89  }
90 #endif
91 #endif
92  /*
93  * TODO move this code to c++
94  */
95  PGR_DBG(" -- change the query");
96  char *edges_of_points_query = NULL;
97  char *edges_no_points_query = NULL;
99  edges_sql, points_sql,
100  &edges_of_points_query,
101  &edges_no_points_query);
102 
103  PGR_DBG("edges_of_points_query:\n%s", edges_of_points_query);
104  PGR_DBG("edges_no_points_query:\n%s", edges_no_points_query);
105 
106  PGR_DBG("load the edges that match the points");
107  pgr_edge_t *edges_of_points = NULL;
108  size_t total_edges_of_points = 0;
109  pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points);
110 
111  PGR_DBG("Total %ld edges in query:", total_edges_of_points);
112 #if 0
113 #ifndef NDEBUG
114  for (i = 0; i < total_edges_of_points; i ++) {
115  PGR_DBG("%ld\t%ld\t%ld\t%f\t%f",
116  edges_of_points[i].id,
117  edges_of_points[i].source,
118  edges_of_points[i].target,
119  edges_of_points[i].cost,
120  edges_of_points[i].reverse_cost);
121  }
122 #endif
123 #endif
124 
125 
126  PGR_DBG("load the edges that don't match the points");
127  pgr_edge_t *edges = NULL;
128  size_t total_edges = 0;
129  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
130 
131  PGR_DBG("Total %ld edges in query:", total_edges);
132 #if 0
133 #ifndef NDEBUG
134  for (i = 0; i < total_edges; i ++) {
135  PGR_DBG("%ld\t%ld\t%ld\t%f\t%f",
136  edges[i].id,
137  edges[i].source,
138  edges[i].target,
139  edges[i].cost,
140  edges[i].reverse_cost);
141  }
142 #endif
143  PGR_DBG("freeing allocated memory not used anymore");
144 #endif
145  free(edges_of_points_query);
146  free(edges_no_points_query);
147 
148  if ((total_edges + total_edges_of_points) == 0) {
149  PGR_DBG("No edges found");
150  (*result_count) = 0;
151  (*result_tuples) = NULL;
152  pgr_SPI_finish();
153  return;
154  }
155 
156  PGR_DBG("Starting processing");
157  char *err_msg = NULL;
158  char *log_msg = NULL;
159  clock_t start_t = clock();
161  edges, total_edges,
162  points, total_points,
163  edges_of_points, total_edges_of_points,
164  start_pid,
165  end_pidsArr, size_end_pidsArr,
166  driving_side[0],
167  details,
168  directed,
169  only_cost,
170  result_tuples,
171  result_count,
172  &log_msg,
173  &err_msg);
174  time_msg(" processing withPoints one to many", start_t, clock());
175  PGR_DBG("Returning %ld tuples\n", *result_count);
176  PGR_DBG("LOG: %s\n", log_msg);
177 
178  if (log_msg) free(log_msg);
179 
180  if (err_msg) {
181  if (*result_tuples) free(*result_tuples);
182  if (end_pidsArr) free(end_pidsArr);
183  elog(ERROR, "%s", err_msg);
184  free(err_msg);
185  }
186  pfree(edges);
187  pgr_SPI_finish();
188 }
189 
190 /* */
191 /*******************************************************************************/
192 
194 PGDLLEXPORT Datum
195 one_to_many_withPoints(PG_FUNCTION_ARGS) {
196  FuncCallContext *funcctx;
197  uint32_t call_cntr;
198  uint32_t max_calls;
199  TupleDesc tuple_desc;
200 
201  /*******************************************************************************/
202  /* MODIFY AS NEEDED */
203  /* */
204  General_path_element_t *result_tuples = 0;
205  size_t result_count = 0;
206  /* */
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  /*******************************************************************************/
216  /* MODIFY AS NEEDED */
217  // CREATE OR REPLACE FUNCTION pgr_withPoint(
218  // edges_sql TEXT,
219  // points_sql TEXT,
220  // start_pid BIGINT,
221  // end_pids ANYARRAY,
222  // directed BOOLEAN -- DEFAULT true,
223  // driving_side CHAR -- DEFAULT 'b',
224  // details BOOLEAN -- DEFAULT false,
225  // only_cost BOOLEAN DEFAULT false,
226 
227 
228  PGR_DBG("Initializing arrays");
229  int64_t* end_pidsArr;
230  size_t size_end_pidsArr;
231  end_pidsArr = (int64_t*)
232  pgr_get_bigIntArray(&size_end_pidsArr, PG_GETARG_ARRAYTYPE_P(3));
233 
234  process(
235  pgr_text2char(PG_GETARG_TEXT_P(0)),
236  pgr_text2char(PG_GETARG_TEXT_P(1)),
237  PG_GETARG_INT64(2),
238  end_pidsArr, size_end_pidsArr,
239  PG_GETARG_BOOL(4),
240  pgr_text2char(PG_GETARG_TEXT_P(5)),
241  PG_GETARG_BOOL(6),
242  PG_GETARG_BOOL(7),
243  &result_tuples,
244  &result_count);
245 
246  PGR_DBG("Cleaning arrays");
247  free(end_pidsArr);
248  /* */
249  /*******************************************************************************/
250 
251  funcctx->max_calls = (uint32_t)result_count;
252  funcctx->user_fctx = result_tuples;
253  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
254  ereport(ERROR,
255  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
256  errmsg("function returning record called in context "
257  "that cannot accept type record")));
258 
259  funcctx->tuple_desc = tuple_desc;
260  MemoryContextSwitchTo(oldcontext);
261  }
262 
263  funcctx = SRF_PERCALL_SETUP();
264  call_cntr = funcctx->call_cntr;
265  max_calls = funcctx->max_calls;
266  tuple_desc = funcctx->tuple_desc;
267  result_tuples = (General_path_element_t*) funcctx->user_fctx;
268 
269  if (call_cntr < max_calls) {
270  HeapTuple tuple;
271  Datum result;
272  Datum *values;
273  bool* nulls;
274 
275  /*******************************************************************************/
276  /* MODIFY AS NEEDED */
277  // OUT seq BIGINT,
278  // OUT path_seq,
279  // OUT node BIGINT,
280  // OUT edge BIGINT,
281  // OUT cost FLOAT,
282  // OUT agg_cost FLOAT)
283 
284 
285  values = palloc(7 * sizeof(Datum));
286  nulls = palloc(7 * sizeof(bool));
287 
288  size_t i;
289  for (i = 0; i < 7; ++i) {
290  nulls[i] = false;
291  }
292 
293 
294  // postgres starts counting from 1
295  values[0] = Int32GetDatum(call_cntr + 1);
296  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
297  values[2] = Int64GetDatum(result_tuples[call_cntr].end_id);
298  values[3] = Int64GetDatum(result_tuples[call_cntr].node);
299  values[4] = Int64GetDatum(result_tuples[call_cntr].edge);
300  values[5] = Float8GetDatum(result_tuples[call_cntr].cost);
301  values[6] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
302  /*******************************************************************************/
303 
304  tuple = heap_form_tuple(tuple_desc, values, nulls);
305  result = HeapTupleGetDatum(tuple);
306  SRF_RETURN_NEXT(funcctx, result);
307  } else {
308  // cleanup
309  if (result_tuples) free(result_tuples);
310 
311  SRF_RETURN_DONE(funcctx);
312  }
313 }
314 
static void process(char *edges_sql, char *points_sql, int64_t start_pid, int64_t *end_pidsArr, size_t size_end_pidsArr, bool directed, char *driving_side, bool details, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
#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
void pgr_get_points(char *points_sql, Point_on_edge_t **points, size_t *total_points)
pgr_get_points
Definition: points_input.c:62
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
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)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
PGDLLEXPORT Datum one_to_many_withPoints(PG_FUNCTION_ARGS)
char * err_msg
Definition: BDATester.cpp:50
PG_FUNCTION_INFO_V1(one_to_many_withPoints)
char * pgr_text2char(text *in)
void do_pgr_one_to_many_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_vid, int64_t *end_pidsArr, size_t size_end_pidsArr, char driving_side, bool details, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **err_msg)