PGROUTING  2.4
 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 "./../../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 many_to_one_withPoints(PG_FUNCTION_ARGS);
45 
46 
47 static
48 void
50  char* edges_sql,
51  char* points_sql,
52  ArrayType *starts,
53  int64_t end_pid,
54  bool directed,
55  char* driving_side,
56  bool details,
57  bool only_cost,
58  General_path_element_t **result_tuples,
59  size_t *result_count) {
60  driving_side[0] = estimate_drivingSide(driving_side[0]);
61  PGR_DBG("estimated driving side:%c", driving_side[0]);
62 
64 
65  size_t size_start_pidsArr = 0;
66  int64_t* start_pidsArr =
67  pgr_get_bigIntArray(&size_start_pidsArr, starts);
68 
69  Point_on_edge_t *points = NULL;
70  size_t total_points = 0;
71  pgr_get_points(points_sql, &points, &total_points);
72 
73 
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 
82  pgr_edge_t *edges_of_points = NULL;
83  size_t total_edges_of_points = 0;
85  edges_of_points_query, &edges_of_points, &total_edges_of_points);
86 
87 
88  pgr_edge_t *edges = NULL;
89  size_t total_edges = 0;
90  pgr_get_edges_reversed(edges_no_points_query, &edges, &total_edges);
91 
92  free(edges_of_points_query);
93  free(edges_no_points_query);
94 
95  if ((total_edges + total_edges_of_points) == 0) {
96  (*result_count) = 0;
97  (*result_tuples) = NULL;
99  return;
100  }
101 
102  PGR_DBG("Starting timer");
103  clock_t start_t = clock();
104  char* log_msg = NULL;
105  char* notice_msg = NULL;
106  char* err_msg = NULL;
108  edges, total_edges,
109  points, total_points,
110  edges_of_points, total_edges_of_points,
111  &end_pid, 1,
112  start_pidsArr, size_start_pidsArr,
113  driving_side[0],
114  details,
115  directed,
116  only_cost,
117  false,
118  result_tuples,
119  result_count,
120  &log_msg,
121  &notice_msg,
122  &err_msg);
123 
124  if (only_cost) {
125  time_msg("processing pgr_withPointsCost(many to one)",
126  start_t, clock());
127  } else {
128  time_msg("processing pgr_withPoints(many to one)", start_t, clock());
129  }
130 
131  if (err_msg && (*result_tuples)) {
132  pfree(*result_tuples);
133  (*result_count) = 0;
134  (*result_tuples) = NULL;
135  }
136 
137  pgr_global_report(log_msg, notice_msg, err_msg);
138 
139  if (log_msg) pfree(log_msg);
140  if (notice_msg) pfree(notice_msg);
141  if (err_msg) pfree(err_msg);
142  if (edges) pfree(edges);
143  if (start_pidsArr) pfree(start_pidsArr);
144 
145  pgr_SPI_finish();
146 }
147 
148 
149 PGDLLEXPORT Datum
150 many_to_one_withPoints(PG_FUNCTION_ARGS) {
151  FuncCallContext *funcctx;
152  TupleDesc tuple_desc;
153 
154  /**********************************************************************/
155  General_path_element_t *result_tuples = NULL;
156  size_t result_count = 0;
157  /**********************************************************************/
158 
159  if (SRF_IS_FIRSTCALL()) {
160  MemoryContext oldcontext;
161  funcctx = SRF_FIRSTCALL_INIT();
162  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
163 
164 
165  /**********************************************************************/
166  // CREATE OR REPLACE FUNCTION pgr_withPoint(
167  // edges_sql TEXT,
168  // points_sql TEXT,
169  // start_pid BIGINT,
170  // end_pids ANYARRAY,
171  // driving_side CHAR -- DEFAULT 'b',
172  // details BOOLEAN -- DEFAULT false,
173  // directed BOOLEAN -- DEFAULT true,
174  // only_cost BOOLEAN DEFAULT false,
175 
176  process(
177  text_to_cstring(PG_GETARG_TEXT_P(0)),
178  text_to_cstring(PG_GETARG_TEXT_P(1)),
179  PG_GETARG_ARRAYTYPE_P(2),
180  PG_GETARG_INT64(3),
181  PG_GETARG_BOOL(4),
182  text_to_cstring(PG_GETARG_TEXT_P(5)),
183  PG_GETARG_BOOL(6),
184  PG_GETARG_BOOL(7),
185  &result_tuples,
186  &result_count);
187 
188  /**********************************************************************/
189 
190 #if PGSQL_VERSION > 95
191  funcctx->max_calls = result_count;
192 #else
193  funcctx->max_calls = (uint32_t)result_count;
194 #endif
195  funcctx->user_fctx = result_tuples;
196  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
197  != TYPEFUNC_COMPOSITE)
198  ereport(ERROR,
199  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
200  errmsg("function returning record called in context "
201  "that cannot accept type record")));
202 
203  funcctx->tuple_desc = tuple_desc;
204  MemoryContextSwitchTo(oldcontext);
205  }
206 
207  funcctx = SRF_PERCALL_SETUP();
208  tuple_desc = funcctx->tuple_desc;
209  result_tuples = (General_path_element_t*) funcctx->user_fctx;
210 
211  if (funcctx->call_cntr < funcctx->max_calls) {
212  HeapTuple tuple;
213  Datum result;
214  Datum *values;
215  bool* nulls;
216 
217  /**********************************************************************/
218  // OUT seq BIGINT,
219  // OUT path_seq,
220  // OUT node BIGINT,
221  // OUT edge BIGINT,
222  // OUT cost FLOAT,
223  // OUT agg_cost FLOAT)
224 
225 
226  values = palloc(7 * sizeof(Datum));
227  nulls = palloc(7 * sizeof(bool));
228 
229  size_t i;
230  for (i = 0; i < 7; ++i) {
231  nulls[i] = false;
232  }
233 
234 
235  // postgres starts counting from 1
236  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
237  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
238  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
239  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
240  values[4] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
241  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
242  values[6] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
243 
244  /**********************************************************************/
245 
246  tuple = heap_form_tuple(tuple_desc, values, nulls);
247  result = HeapTupleGetDatum(tuple);
248  SRF_RETURN_NEXT(funcctx, result);
249  } else {
250  SRF_RETURN_DONE(funcctx);
251  }
252 }
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_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
PGDLLEXPORT Datum many_to_one_withPoints(PG_FUNCTION_ARGS)
PG_FUNCTION_INFO_V1(many_to_one_withPoints)
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)
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
static void process(char *edges_sql, char *points_sql, ArrayType *starts, int64_t end_pid, bool directed, char *driving_side, bool details, bool only_cost, General_path_element_t **result_tuples, size_t *result_count)
char * err_msg
Definition: BDATester.cpp:50
void pgr_get_edges_reversed(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
Definition: edges_input.c:550