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