PGROUTING  3.2
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
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 <stdbool.h>
32 #include "utils/array.h"
33 
34 #include "c_common/time_msg.h"
35 #include "c_common/e_report.h"
36 
37 #include "c_common/edges_input.h"
38 #include "c_common/points_input.h"
39 
42 #include "c_common/debug_macro.h"
43 
44 PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS);
46 
47 
48 static
49 void
51  char* edges_sql,
52  char* points_sql,
53  int64_t start_pid,
54  int64_t end_pid,
55  int p_k,
56 
57  bool directed,
58  bool heap_paths,
59  char *driving_side,
60  bool details,
61 
62  General_path_element_t **result_tuples,
63  size_t *result_count) {
64  if (p_k < 0) {
65  return;
66  }
67 
68  size_t k = (size_t)p_k;
69 
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 
78 
79  Point_on_edge_t *points = NULL;
80  size_t total_points = 0;
81  pgr_get_points(points_sql, &points, &total_points);
82 
83  char *edges_of_points_query = NULL;
84  char *edges_no_points_query = NULL;
86  edges_sql, points_sql,
87  &edges_of_points_query,
88  &edges_no_points_query);
89 
90 
91  pgr_edge_t *edges_of_points = NULL;
92  size_t total_edges_of_points = 0;
93  pgr_get_edges(edges_of_points_query,
94  &edges_of_points,
95  &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  pfree(edges_of_points_query);
103  pfree(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  clock_t start_t = clock();
115 
116  char *log_msg = NULL;
117  char *notice_msg = NULL;
118  char *err_msg = NULL;
120  edges,
121  total_edges,
122  points,
123  total_points,
124  edges_of_points,
125  total_edges_of_points,
126  start_pid,
127  end_pid,
128  k,
129 
130  directed,
131  heap_paths,
132  driving_side[0],
133  details,
134 
135  result_tuples,
136  result_count,
137 
138  &log_msg,
139  &notice_msg,
140  &err_msg);
141  time_msg(" processing withPointsKSP", start_t, clock());
142 
143  if (err_msg && (*result_tuples)) {
144  pfree(*result_tuples);
145  (*result_tuples) = NULL;
146  (*result_count) = 0;
147  }
148 
149  pgr_global_report(log_msg, notice_msg, err_msg);
150 
151  if (log_msg) pfree(log_msg);
152  if (notice_msg) pfree(notice_msg);
153  if (err_msg) pfree(err_msg);
154 
155  pfree(edges);
156  pfree(edges_of_points);
157  pfree(points);
158 
159  pgr_SPI_finish();
160 }
161 
162 
163 
164 
165 PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS) {
166  FuncCallContext *funcctx;
167  TupleDesc tuple_desc;
168 
169  General_path_element_t *result_tuples = 0;
170  size_t result_count = 0;
171 
172  if (SRF_IS_FIRSTCALL()) {
173  MemoryContext oldcontext;
174  funcctx = SRF_FIRSTCALL_INIT();
175  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
176 
177 
178  /*
179  CREATE OR REPLACE FUNCTION pgr_withPoint(
180  edges_sql TEXT,
181  points_sql TEXT,
182  start_pid INTEGER,
183  end_pid BIGINT,
184  k BIGINT,
185 
186  directed BOOLEAN -- DEFAULT true,
187  heap_paths BOOLEAN -- DEFAULT false,
188  driving_side CHAR -- DEFAULT 'b',
189  details BOOLEAN -- DEFAULT false
190  */
191 
192  PGR_DBG("Calling process");
193  PGR_DBG("initial driving side:%s",
194  text_to_cstring(PG_GETARG_TEXT_P(7)));
195  process(
196  text_to_cstring(PG_GETARG_TEXT_P(0)),
197  text_to_cstring(PG_GETARG_TEXT_P(1)),
198  PG_GETARG_INT64(2),
199  PG_GETARG_INT64(3),
200  PG_GETARG_INT32(4),
201  PG_GETARG_BOOL(5),
202  PG_GETARG_BOOL(6),
203  text_to_cstring(PG_GETARG_TEXT_P(7)),
204  PG_GETARG_BOOL(8),
205  &result_tuples,
206  &result_count);
207 
208 
209 #if PGSQL_VERSION > 95
210  funcctx->max_calls = result_count;
211 #else
212  funcctx->max_calls = (uint32_t)result_count;
213 #endif
214  funcctx->user_fctx = result_tuples;
215  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
216  != TYPEFUNC_COMPOSITE)
217  ereport(ERROR,
218  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
219  errmsg("function returning record called in context "
220  "that cannot accept type record")));
221 
222  funcctx->tuple_desc = tuple_desc;
223  MemoryContextSwitchTo(oldcontext);
224  }
225 
226  funcctx = SRF_PERCALL_SETUP();
227  tuple_desc = funcctx->tuple_desc;
228  result_tuples = (General_path_element_t*) funcctx->user_fctx;
229 
230  if (funcctx->call_cntr < funcctx->max_calls) {
231  HeapTuple tuple;
232  Datum result;
233  Datum *values;
234  bool* nulls;
235 
236  values = palloc(7 * sizeof(Datum));
237  nulls = palloc(7 * sizeof(bool));
238 
239  size_t i;
240  for (i = 0; i < 7; ++i) {
241  nulls[i] = false;
242  }
243 
244  /*
245  OUT seq INTEGER, OUT path_id INTEGER, OUT path_seq INTEGER,
246  OUT node BIGINT, OUT edge BIGINT,
247  OUT cost FLOAT, OUT agg_cost FLOAT)
248  */
249 
250 
251  // postgres starts counting from 1
252  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
253  values[1] = Int32GetDatum((int)
254  (result_tuples[funcctx->call_cntr].start_id + 1));
255  values[2] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
256  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
257  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
258  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
259  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
260 
261  tuple = heap_form_tuple(tuple_desc, values, nulls);
262  result = HeapTupleGetDatum(tuple);
263  SRF_RETURN_NEXT(funcctx, result);
264  } else {
265  SRF_RETURN_DONE(funcctx);
266  }
267 }
268 
General_path_element_t::edge
int64_t edge
Definition: general_path_element_t.h:42
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_withpointsksp)
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
General_path_element_t::seq
int seq
Definition: general_path_element_t.h:38
withPoints_ksp_driver.h
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
process
static void process(char *edges_sql, char *points_sql, int64_t start_pid, int64_t end_pid, int p_k, bool directed, bool heap_paths, char *driving_side, bool details, General_path_element_t **result_tuples, size_t *result_count)
Definition: withPoints_ksp.c:50
pgr_get_points
void pgr_get_points(char *points_sql, Point_on_edge_t **points, size_t *total_points)
pgr_get_points
Definition: points_input.c:69
General_path_element_t::start_id
int64_t start_id
Definition: general_path_element_t.h:39
debug_macro.h
get_new_queries
void get_new_queries(char *edges_sql, char *points_sql, char **edges_of_points_query, char **edges_no_points_query)
Definition: get_new_queries.cpp:46
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
do_pgr_withPointsKsp
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_pid, int64_t end_pid, size_t k, bool directed, bool heap_paths, char driving_side, bool details, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: withPoints_ksp_driver.cpp:54
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
Point_on_edge_t
Definition: point_on_edge_t.h:37
pgr_get_edges
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:711
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
get_new_queries.h
edges_input.h
points_input.h
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
_pgr_withpointsksp
PGDLLEXPORT Datum _pgr_withpointsksp(PG_FUNCTION_ARGS)
Definition: withPoints_ksp.c:165
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44