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
many_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 many_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 
62  int64_t *start_pidsArr,
63  size_t size_start_pidsArr,
64 
65  int64_t *end_pidsArr,
66  size_t size_end_pidsArr,
67 
68  bool directed,
69  char *driving_side,
70  bool details,
71  bool only_cost,
72  General_path_element_t **result_tuples,
73  size_t *result_count) {
74 
76 
77  Point_on_edge_t *points = NULL;
78  size_t total_points = 0;
79  pgr_get_points(points_sql, &points, &total_points);
80 
81 
82  char *edges_of_points_query = NULL;
83  char *edges_no_points_query = NULL;
85  edges_sql, points_sql,
86  &edges_of_points_query,
87  &edges_no_points_query);
88 
89 
90  pgr_edge_t *edges_of_points = NULL;
91  size_t total_edges_of_points = 0;
92  pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points);
93 
94 
95  pgr_edge_t *edges = NULL;
96  size_t total_edges = 0;
97  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
98 
99  free(edges_of_points_query);
100  free(edges_no_points_query);
101 
102  if ((total_edges + total_edges_of_points) == 0) {
103  (*result_count) = 0;
104  (*result_tuples) = NULL;
105  pgr_SPI_finish();
106  return;
107  }
108 
109  char *err_msg = NULL;
110  char *log_msg = NULL;
111  clock_t start_t = clock();
113  edges, total_edges,
114  points, total_points,
115  edges_of_points, total_edges_of_points,
116  start_pidsArr, size_start_pidsArr,
117  end_pidsArr, size_end_pidsArr,
118  driving_side[0],
119  details,
120  directed,
121  only_cost,
122  result_tuples,
123  result_count,
124  &err_msg);
125  time_msg("Processing withPoints many to many", start_t, clock());
126  PGR_DBG("Returning %ld tuples\n", *result_count);
127  PGR_DBG("LOG: %s\n", err_msg);
128  if (log_msg) free(log_msg);
129 
130  if (err_msg) {
131  free(start_pidsArr);
132  free(end_pidsArr);
133  free(*result_tuples);
134  elog(ERROR, "%s", err_msg);
135  free(err_msg);
136  }
137  pfree(edges);
138  pgr_SPI_finish();
139 }
140 
141 /* */
142 /*******************************************************************************/
143 
145 PGDLLEXPORT Datum
146 many_to_many_withPoints(PG_FUNCTION_ARGS) {
147  FuncCallContext *funcctx;
148  uint32_t call_cntr;
149  uint32_t max_calls;
150  TupleDesc tuple_desc;
151 
152  /*******************************************************************************/
153  /* MODIFY AS NEEDED */
154  /* */
155  General_path_element_t *result_tuples = 0;
156  size_t result_count = 0;
157  /* */
158  /*******************************************************************************/
159 
160  if (SRF_IS_FIRSTCALL()) {
161  MemoryContext oldcontext;
162  funcctx = SRF_FIRSTCALL_INIT();
163  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
164 
165 
166  /*******************************************************************************/
167  /* MODIFY AS NEEDED */
168  // CREATE OR REPLACE FUNCTION pgr_withPoint(
169  // edges_sql TEXT,
170  // points_sql TEXT,
171  // start_pid BIGINT,
172  // end_pids ANYARRAY,
173  // driving_side CHAR -- DEFAULT 'b',
174  // details BOOLEAN -- DEFAULT false,
175  // directed BOOLEAN -- DEFAULT true,
176  // only_cost BOOLEAN DEFAULT false,
177 
178 
179  int64_t* start_pidsArr;
180  size_t size_start_pidsArr;
181  start_pidsArr = (int64_t*)
182  pgr_get_bigIntArray(&size_start_pidsArr, PG_GETARG_ARRAYTYPE_P(2));
183 
184  int64_t* end_pidsArr;
185  size_t size_end_pidsArr;
186  end_pidsArr = (int64_t*)
187  pgr_get_bigIntArray(&size_end_pidsArr, PG_GETARG_ARRAYTYPE_P(3));
188 
189  process(
190  pgr_text2char(PG_GETARG_TEXT_P(0)),
191  pgr_text2char(PG_GETARG_TEXT_P(1)),
192  start_pidsArr, size_start_pidsArr,
193  end_pidsArr, size_end_pidsArr,
194  PG_GETARG_BOOL(4),
195  pgr_text2char(PG_GETARG_TEXT_P(5)),
196  PG_GETARG_BOOL(6),
197  PG_GETARG_BOOL(7),
198  &result_tuples,
199  &result_count);
200 
201  if (start_pidsArr) free(start_pidsArr);
202  if (end_pidsArr) free(end_pidsArr);
203  /* */
204  /*******************************************************************************/
205 
206  funcctx->max_calls = (uint32_t)result_count;
207  funcctx->user_fctx = result_tuples;
208  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
209  ereport(ERROR,
210  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
211  errmsg("function returning record called in context "
212  "that cannot accept type record")));
213 
214  funcctx->tuple_desc = tuple_desc;
215  MemoryContextSwitchTo(oldcontext);
216  }
217 
218  funcctx = SRF_PERCALL_SETUP();
219  call_cntr = funcctx->call_cntr;
220  max_calls = funcctx->max_calls;
221  tuple_desc = funcctx->tuple_desc;
222  result_tuples = (General_path_element_t*) funcctx->user_fctx;
223 
224  if (call_cntr < max_calls) {
225  HeapTuple tuple;
226  Datum result;
227  Datum *values;
228  bool* nulls;
229 
230  /*******************************************************************************/
231  /* MODIFY AS NEEDED */
232  // OUT seq BIGINT,
233  // OUT path_seq,
234  // OUT node BIGINT,
235  // OUT edge BIGINT,
236  // OUT cost FLOAT,
237  // OUT agg_cost FLOAT)
238 
239 
240  values = palloc(8 * sizeof(Datum));
241  nulls = palloc(8 * sizeof(bool));
242 
243  size_t i;
244  for (i = 0; i < 8; ++i) {
245  nulls[i] = false;
246  }
247 
248 
249  // postgres starts counting from 1
250  values[0] = Int32GetDatum(call_cntr + 1);
251  values[1] = Int32GetDatum(result_tuples[call_cntr].seq);
252  values[2] = Int64GetDatum(result_tuples[call_cntr].start_id);
253  values[3] = Int64GetDatum(result_tuples[call_cntr].end_id);
254  values[4] = Int64GetDatum(result_tuples[call_cntr].node);
255  values[5] = Int64GetDatum(result_tuples[call_cntr].edge);
256  values[6] = Float8GetDatum(result_tuples[call_cntr].cost);
257  values[7] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
258  /*******************************************************************************/
259 
260  tuple = heap_form_tuple(tuple_desc, values, nulls);
261  result = HeapTupleGetDatum(tuple);
262  SRF_RETURN_NEXT(funcctx, result);
263  } else {
264  // cleanup
265  if (result_tuples) free(result_tuples);
266 
267  SRF_RETURN_DONE(funcctx);
268  }
269 }
270 
static void process(char *edges_sql, char *points_sql, int64_t *start_pidsArr, size_t size_start_pidsArr, 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
int do_pgr_many_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_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, General_path_element_t **return_tuples, size_t *return_count, char **err_msg)
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:535
PG_FUNCTION_INFO_V1(many_to_many_withPoints)
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)
PGDLLEXPORT Datum many_to_many_withPoints(PG_FUNCTION_ARGS)
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)