PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
one_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 "./../../common/src/postgres_connection.h"
31 #include "utils/array.h"
32 
33 #include "./../../common/src/debug_macro.h"
34 #include "./../../common/src/e_report.h"
35 #include "./../../common/src/time_msg.h"
36 #include "./../../common/src/pgr_types.h"
37 #include "./../../common/src/edges_input.h"
38 #include "./../../common/src/arrays_input.h"
39 #include "./../../common/src/points_input.h"
40 #include "./get_new_queries.h"
42 
43 PGDLLEXPORT Datum one_to_many_withPoints(PG_FUNCTION_ARGS);
44 
45 
46 static
47 void
49  char* edges_sql,
50  char* points_sql,
51  int64_t start_pid,
52  ArrayType *ends,
53  bool directed,
54  char *driving_side,
55  bool details,
56  bool only_cost,
57  General_path_element_t **result_tuples,
58  size_t *result_count) {
59  driving_side[0] = estimate_drivingSide(driving_side[0]);
60  PGR_DBG("estimated driving side:%c", driving_side[0]);
61 
63 
64  size_t size_end_pidsArr = 0;
65  int64_t* end_pidsArr =
66  pgr_get_bigIntArray(&size_end_pidsArr, ends);
67 
68  PGR_DBG("load the points");
69  size_t total_points = 0;
70  Point_on_edge_t *points = NULL;
71  pgr_get_points(points_sql, &points, &total_points);
72 
73  PGR_DBG(" -- change the query");
74  char *edges_of_points_query = NULL;
75  char *edges_no_points_query = NULL;
77  edges_sql, points_sql,
78  &edges_of_points_query,
79  &edges_no_points_query);
80 
81  PGR_DBG("edges_of_points_query:\n%s", edges_of_points_query);
82  PGR_DBG("edges_no_points_query:\n%s", edges_no_points_query);
83 
84  PGR_DBG("load the edges that match the points");
85  pgr_edge_t *edges_of_points = NULL;
86  size_t total_edges_of_points = 0;
88  edges_of_points_query, &edges_of_points, &total_edges_of_points);
89 
90  PGR_DBG("Total %ld edges in query:", total_edges_of_points);
91 
92 
93  PGR_DBG("load the edges that don't match the points");
94  pgr_edge_t *edges = NULL;
95  size_t total_edges = 0;
96  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
97 
98  PGR_DBG("Total %ld edges in query:", total_edges);
99 
100  free(edges_of_points_query);
101  free(edges_no_points_query);
102 
103  if ((total_edges + total_edges_of_points) == 0) {
104  PGR_DBG("No edges found");
105  pgr_SPI_finish();
106  return;
107  }
108 
109  PGR_DBG("Starting timer");
110  clock_t start_t = clock();
111  char* log_msg = NULL;
112  char* notice_msg = NULL;
113  char* err_msg = NULL;
115  edges, total_edges,
116  points, total_points,
117  edges_of_points, total_edges_of_points,
118  &start_pid, 1,
119  end_pidsArr, size_end_pidsArr,
120 
121  driving_side[0],
122  details,
123  directed,
124  only_cost,
125  true,
126 
127  result_tuples, result_count,
128 
129  &log_msg,
130  &notice_msg,
131  &err_msg);
132 
133  if (only_cost) {
134  time_msg("processing pgr_withPointsCost(one to many)",
135  start_t, clock());
136  } else {
137  time_msg("processing pgr_withPoints(one to many)", start_t, clock());
138  }
139 
140  if (err_msg && (*result_tuples)) {
141  pfree(*result_tuples);
142  (*result_count) = 0;
143  (*result_tuples) = NULL;
144  }
145 
146  pgr_global_report(log_msg, notice_msg, err_msg);
147 
148  if (log_msg) pfree(log_msg);
149  if (notice_msg) pfree(notice_msg);
150  if (err_msg) pfree(err_msg);
151  if (edges) pfree(edges);
152  if (points) pfree(points);
153  if (edges_of_points) pfree(edges_of_points);
154  if (end_pidsArr) pfree(end_pidsArr);
155  pgr_SPI_finish();
156 }
157 
158 
160 PGDLLEXPORT Datum
161 one_to_many_withPoints(PG_FUNCTION_ARGS) {
162  FuncCallContext *funcctx;
163  TupleDesc tuple_desc;
164 
165  /**********************************************************************/
166  General_path_element_t *result_tuples = NULL;
167  size_t result_count = 0;
168  /**********************************************************************/
169 
170  if (SRF_IS_FIRSTCALL()) {
171  MemoryContext oldcontext;
172  funcctx = SRF_FIRSTCALL_INIT();
173  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
174 
175 
176  /**********************************************************************/
177  // CREATE OR REPLACE FUNCTION pgr_withPoint(
178  // edges_sql TEXT,
179  // points_sql TEXT,
180  // start_pid BIGINT,
181  // end_pids ANYARRAY,
182  // directed BOOLEAN -- DEFAULT true,
183  // driving_side CHAR -- DEFAULT 'b',
184  // details BOOLEAN -- DEFAULT false,
185  // only_cost BOOLEAN DEFAULT false,
186 
187 
188 
189  process(
190  text_to_cstring(PG_GETARG_TEXT_P(0)),
191  text_to_cstring(PG_GETARG_TEXT_P(1)),
192  PG_GETARG_INT64(2),
193  PG_GETARG_ARRAYTYPE_P(3),
194  PG_GETARG_BOOL(4),
195  text_to_cstring(PG_GETARG_TEXT_P(5)),
196  PG_GETARG_BOOL(6),
197  PG_GETARG_BOOL(7),
198  &result_tuples,
199  &result_count);
200 
201  /**********************************************************************/
202 
203 #if PGSQL_VERSION > 95
204  funcctx->max_calls = result_count;
205 #else
206  funcctx->max_calls = (uint32_t)result_count;
207 #endif
208  funcctx->user_fctx = result_tuples;
209  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
210  != TYPEFUNC_COMPOSITE)
211  ereport(ERROR,
212  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
213  errmsg("function returning record called in context "
214  "that cannot accept type record")));
215 
216  funcctx->tuple_desc = tuple_desc;
217  MemoryContextSwitchTo(oldcontext);
218  }
219 
220  funcctx = SRF_PERCALL_SETUP();
221  tuple_desc = funcctx->tuple_desc;
222  result_tuples = (General_path_element_t*) funcctx->user_fctx;
223 
224  if (funcctx->call_cntr < funcctx->max_calls) {
225  HeapTuple tuple;
226  Datum result;
227  Datum *values;
228  bool* nulls;
229 
230  /**********************************************************************/
231  // OUT seq BIGINT,
232  // OUT path_seq,
233  // OUT node BIGINT,
234  // OUT edge BIGINT,
235  // OUT cost FLOAT,
236  // OUT agg_cost FLOAT)
237 
238 
239  values = palloc(7 * sizeof(Datum));
240  nulls = palloc(7 * sizeof(bool));
241 
242  size_t i;
243  for (i = 0; i < 7; ++i) {
244  nulls[i] = false;
245  }
246 
247 
248  // postgres starts counting from 1
249  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
250  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
251  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].end_id);
252  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
253  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
254  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
255  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
256 
257  /**********************************************************************/
258 
259  tuple = heap_form_tuple(tuple_desc, values, nulls);
260  result = HeapTupleGetDatum(tuple);
261  SRF_RETURN_NEXT(funcctx, result);
262  } else {
263  SRF_RETURN_DONE(funcctx);
264  }
265 }
266 
char estimate_drivingSide(char driving_side)
void 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, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
#define PGR_DBG(...)
Definition: debug_macro.h:34
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:539
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:127
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
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)
static void process(char *edges_sql, char *points_sql, int64_t start_pid, ArrayType *ends, bool directed, char *driving_side, bool details, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
PGDLLEXPORT Datum one_to_many_withPoints(PG_FUNCTION_ARGS)
char * err_msg
Definition: BDATester.cpp:50
PG_FUNCTION_INFO_V1(one_to_many_withPoints)