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
withPoints_ksp.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: withPoints_ksp.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/pgr_types.h"
42 #include "./../../common/src/time_msg.h"
43 #include "./../../common/src/postgres_connection.h"
44 #include "./../../common/src/edges_input.h"
45 #include "./../../common/src/points_input.h"
46 #include "./../../withPoints/src/get_new_queries.h"
48 #include "./../../common/src/debug_macro.h"
49 
50 PGDLLEXPORT Datum withPoints_ksp(PG_FUNCTION_ARGS);
51 
52 
53 /*******************************************************************************/
54 /* MODIFY AS NEEDED */
55 static
56 void
58  char* edges_sql,
59  char* points_sql,
60  int64_t start_pid,
61  int64_t end_pid,
62  int k,
63 
64  bool directed,
65  bool heap_paths,
66  char *driving_side,
67  bool details,
68 
69  General_path_element_t **result_tuples,
70  size_t *result_count) {
71 
72  driving_side[0] = (char) tolower(driving_side[0]);
73  PGR_DBG("driving side:%c",driving_side[0]);
74  if (! ((driving_side[0] == 'r')
75  || (driving_side[0] == 'l'))) {
76  driving_side[0] = 'b';
77  }
78 
80 
81  Point_on_edge_t *points = NULL;
82  size_t total_points = 0;
83  pgr_get_points(points_sql, &points, &total_points);
84 
85  char *edges_of_points_query = NULL;
86  char *edges_no_points_query = NULL;
88  edges_sql, points_sql,
89  &edges_of_points_query,
90  &edges_no_points_query);
91 
92 
93  pgr_edge_t *edges_of_points = NULL;
94  size_t total_edges_of_points = 0;
95  pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points);
96 
97  pgr_edge_t *edges = NULL;
98  size_t total_edges = 0;
99  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
100 
101  PGR_DBG("freeing allocated memory not used anymore");
102  free(edges_of_points_query);
103  free(edges_no_points_query);
104 
105  if ((total_edges + total_edges_of_points) == 0) {
106  PGR_DBG("No edges found");
107  (*result_count) = 0;
108  (*result_tuples) = NULL;
109  pgr_SPI_finish();
110  return;
111  }
112 
113  PGR_DBG("Starting processing");
114  char *err_msg = NULL;
115  clock_t start_t = clock();
116  int errcode = do_pgr_withPointsKsp(
117  edges,
118  total_edges,
119  points,
120  total_points,
121  edges_of_points,
122  total_edges_of_points,
123  start_pid,
124  end_pid,
125  k,
126 
127  directed,
128  heap_paths,
129  driving_side[0],
130  details,
131 
132  result_tuples,
133  result_count,
134  &err_msg);
135  time_msg(" processing withPointsKSP", start_t, clock());
136 
137  PGR_DBG("Returned message = %s\n", err_msg);
138 
139  if (!err_msg) free(err_msg);
140 
141  pfree(edges);
142  pfree(edges_of_points);
143  pfree(points);
144 
145  pgr_SPI_finish();
146 
147  if (errcode) {
148  pgr_send_error(errcode);
149  }
150 
151 }
152 
153 /* */
154 /*******************************************************************************/
155 
156 
157 
159 PGDLLEXPORT Datum
160 withPoints_ksp(PG_FUNCTION_ARGS) {
161  FuncCallContext *funcctx;
162  uint32_t call_cntr;
163  uint32_t max_calls;
164  TupleDesc tuple_desc;
165 
166  /*******************************************************************************/
167  /* MODIFY AS NEEDED */
168  /* */
169  General_path_element_t *result_tuples = 0;
170  size_t result_count = 0;
171  /* */
172  /*******************************************************************************/
173 
174  if (SRF_IS_FIRSTCALL()) {
175  MemoryContext oldcontext;
176  funcctx = SRF_FIRSTCALL_INIT();
177  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
178 
179 
180  /*******************************************************************************/
181  /* MODIFY AS NEEDED */
182  // CREATE OR REPLACE FUNCTION pgr_withPoint(
183  // edges_sql TEXT,
184  // points_sql TEXT,
185  // start_pid INTEGER,
186  // end_pid BIGINT,
187  // k BIGINT,
188  //
189  // directed BOOLEAN -- DEFAULT true,
190  // heap_paths BOOLEAN -- DEFAULT false,
191  // driving_side CHAR -- DEFAULT 'b',
192  // details BOOLEAN -- DEFAULT false,
193 
194  PGR_DBG("Calling process");
195  PGR_DBG("initial driving side:%s", pgr_text2char(PG_GETARG_TEXT_P(7)));
196  process(
197  pgr_text2char(PG_GETARG_TEXT_P(0)),
198  pgr_text2char(PG_GETARG_TEXT_P(1)),
199  PG_GETARG_INT64(2),
200  PG_GETARG_INT64(3),
201  PG_GETARG_INT32(4),
202  PG_GETARG_BOOL(5),
203  PG_GETARG_BOOL(6),
204  pgr_text2char(PG_GETARG_TEXT_P(7)),
205  PG_GETARG_BOOL(8),
206  &result_tuples,
207  &result_count);
208 
209  /* */
210  /*******************************************************************************/
211 
212  funcctx->max_calls = (uint32_t)result_count;
213  funcctx->user_fctx = result_tuples;
214  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
215  ereport(ERROR,
216  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
217  errmsg("function returning record called in context "
218  "that cannot accept type record")));
219 
220  funcctx->tuple_desc = tuple_desc;
221  MemoryContextSwitchTo(oldcontext);
222  }
223 
224  funcctx = SRF_PERCALL_SETUP();
225  call_cntr = funcctx->call_cntr;
226  max_calls = funcctx->max_calls;
227  tuple_desc = funcctx->tuple_desc;
228  result_tuples = (General_path_element_t*) funcctx->user_fctx;
229 
230  if (call_cntr < max_calls) {
231  HeapTuple tuple;
232  Datum result;
233  Datum *values;
234  bool* nulls;
235 
236  /*******************************************************************************/
237  /* MODIFY AS NEEDED */
238  values = palloc(7 * sizeof(Datum));
239  nulls = palloc(7 * sizeof(bool));
240 
241  size_t i;
242  for(i = 0; i < 7; ++i) {
243  nulls[i] = false;
244  }
245 
246  /*
247  OUT seq INTEGER, OUT path_id INTEGER, OUT path_seq INTEGER,
248  OUT node BIGINT, OUT edge BIGINT,
249  OUT cost FLOAT, OUT agg_cost FLOAT)
250  */
251 
252 
253  // postgres starts counting from 1
254  values[0] = Int32GetDatum(call_cntr + 1);
255  values[1] = Int32GetDatum((int)(result_tuples[call_cntr].start_id + 1));
256  values[2] = Int32GetDatum(result_tuples[call_cntr].seq);
257  values[3] = Int64GetDatum(result_tuples[call_cntr].node);
258  values[4] = Int64GetDatum(result_tuples[call_cntr].edge);
259  values[5] = Float8GetDatum(result_tuples[call_cntr].cost);
260  values[6] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
261  /*******************************************************************************/
262 
263  tuple =heap_form_tuple(tuple_desc, values, nulls);
264  result = HeapTupleGetDatum(tuple);
265  SRF_RETURN_NEXT(funcctx, result);
266  } else {
267  // cleanup
268  if (result_tuples) free(result_tuples);
269 
270  SRF_RETURN_DONE(funcctx);
271  }
272 }
273 
int do_pgr_withPointsKsp(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_vid, int k, bool directed, bool heap_paths, char driving_side, bool details, General_path_element_t **return_tuples, size_t *return_count, char **err_msg)
PG_FUNCTION_INFO_V1(withPoints_ksp)
#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
PGDLLEXPORT Datum withPoints_ksp(PG_FUNCTION_ARGS)
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
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)
static void process(char *edges_sql, char *points_sql, int64_t start_pid, int64_t end_pid, int k, bool directed, bool heap_paths, char *driving_side, bool details, General_path_element_t **result_tuples, size_t *result_count)