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