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