PGROUTING  2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
many_to_dist_withPointsDD.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: many_to_dist_driving_distance.c
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 Mail: vicky_Vergara@hotmail.com
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 ********************************************************************PGR-GNU*/
24 
26 #include "utils/array.h"
27 
28 #include "c_common/debug_macro.h"
29 #include "c_common/e_report.h"
30 #include "c_common/time_msg.h"
31 
32 #include "c_common/edges_input.h"
33 #include "c_common/arrays_input.h"
34 #include "c_common/points_input.h"
35 
38 
39 
40 PGDLLEXPORT Datum many_withPointsDD(PG_FUNCTION_ARGS);
42 
43 static
44 void process(
45  char* edges_sql,
46  char* points_sql,
47  ArrayType* starts,
48  double distance,
49 
50  bool directed,
51  char *driving_side,
52  bool details,
53  bool equicost,
54 
55  General_path_element_t **result_tuples,
56  size_t *result_count) {
57  driving_side[0] = estimate_drivingSide(driving_side[0]);
58  PGR_DBG("estimated driving side:%c", driving_side[0]);
59 
61 
62  size_t total_starts = 0;
63  int64_t* start_pidsArr = pgr_get_bigIntArray(&total_starts, starts);
64  PGR_DBG("sourcesArr size %ld ", total_starts);
65 
66  Point_on_edge_t *points = NULL;
67  size_t total_points = 0;
68  pgr_get_points(points_sql, &points, &total_points);
69 
70  char *edges_of_points_query = NULL;
71  char *edges_no_points_query = NULL;
73  edges_sql, points_sql,
74  &edges_of_points_query,
75  &edges_no_points_query);
76 
77 
78  pgr_edge_t *edges_of_points = NULL;
79  size_t total_edges_of_points = 0;
81  edges_of_points_query, &edges_of_points, &total_edges_of_points);
82 
83  pgr_edge_t *edges = NULL;
84  size_t total_edges = 0;
85  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
86 
87  PGR_DBG("freeing allocated memory not used anymore");
88  free(edges_of_points_query);
89  free(edges_no_points_query);
90 
91  if ((total_edges + total_edges_of_points) == 0) {
92  if (edges) pfree(edges);
93  if (edges_of_points) pfree(edges_of_points);
94  if (points) pfree(points);
96  return;
97  }
98 
99  PGR_DBG("Starting timer");
100  clock_t start_t = clock();
101  char* log_msg = NULL;
102  char* notice_msg = NULL;
103  char* err_msg = NULL;
105  edges, total_edges,
106  points, total_points,
107  edges_of_points, total_edges_of_points,
108  start_pidsArr, total_starts,
109  distance,
110 
111  directed,
112  driving_side[0],
113  details,
114  equicost,
115 
116  result_tuples,
117  result_count,
118  &log_msg,
119  &notice_msg,
120  &err_msg);
121  time_msg(" processing withPointsDD many starts", start_t, clock());
122 
123  if (err_msg && (*result_tuples)) {
124  pfree(*result_tuples);
125  (*result_count) = 0;
126  (*result_tuples) = NULL;
127  }
128 
129  pgr_global_report(log_msg, notice_msg, err_msg);
130 
131  if (log_msg) pfree(log_msg);
132  if (notice_msg) pfree(notice_msg);
133  if (err_msg) pfree(err_msg);
134  if (edges) pfree(edges);
135  if (edges_of_points) pfree(edges_of_points);
136  if (points) pfree(points);
137  if (start_pidsArr) pfree(start_pidsArr);
138  pgr_SPI_finish();
139 }
140 
141 
142 
143 PGDLLEXPORT Datum
144 many_withPointsDD(PG_FUNCTION_ARGS) {
145  FuncCallContext *funcctx;
146  TupleDesc tuple_desc;
147 
148  /**********************************************************************/
149  General_path_element_t *result_tuples = 0;
150  size_t result_count = 0;
151  /**********************************************************************/
152 
153 
154  if (SRF_IS_FIRSTCALL()) {
155  MemoryContext oldcontext;
156  funcctx = SRF_FIRSTCALL_INIT();
157  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
158 
159 
160  /**********************************************************************/
161  // CREATE OR REPLACE FUNCTION pgr_withPointsDD(
162  // edges_sql TEXT,
163  // points_sql TEXT,
164  // start_pids anyarray,
165  // distance FLOAT,
166  //
167  // directed BOOLEAN -- DEFAULT true,
168  // driving_side CHAR -- DEFAULT 'b',
169  // details BOOLEAN -- DEFAULT false,
170  // equicost BOOLEAN -- DEFAULT false,
171 
172 
173  PGR_DBG("Calling driving_many_to_dist_driver");
174  process(
175  text_to_cstring(PG_GETARG_TEXT_P(0)),
176  text_to_cstring(PG_GETARG_TEXT_P(1)),
177  PG_GETARG_ARRAYTYPE_P(2),
178  PG_GETARG_FLOAT8(3),
179 
180  PG_GETARG_BOOL(4),
181  text_to_cstring(PG_GETARG_TEXT_P(5)),
182  PG_GETARG_BOOL(6),
183  PG_GETARG_BOOL(7),
184  &result_tuples, &result_count);
185 
186  /**********************************************************************/
187 
188 #if PGSQL_VERSION > 95
189  funcctx->max_calls = result_count;
190 #else
191  funcctx->max_calls = (uint32_t)result_count;
192 #endif
193  funcctx->user_fctx = result_tuples;
194  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
195  != TYPEFUNC_COMPOSITE)
196  ereport(ERROR,
197  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
198  errmsg("function returning record called in context "
199  "that cannot accept type record")));
200 
201  funcctx->tuple_desc = tuple_desc;
202 
203  MemoryContextSwitchTo(oldcontext);
204  }
205 
206  funcctx = SRF_PERCALL_SETUP();
207 
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  size_t numb = 6;
219  values = palloc(numb * sizeof(Datum));
220  nulls = palloc(numb * sizeof(bool));
221 
222  size_t i;
223  for (i = 0; i < numb; ++i) {
224  nulls[i] = false;
225  }
226 
227  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
228  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
229  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
230  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
231  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
232  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
233 
234  /**********************************************************************/
235 
236  tuple = heap_form_tuple(tuple_desc, values, nulls);
237  result = HeapTupleGetDatum(tuple);
238 
239  pfree(values);
240  pfree(nulls);
241 
242  SRF_RETURN_NEXT(funcctx, result);
243  } else {
244  SRF_RETURN_DONE(funcctx);
245  }
246 }
247 
static edge_t edges[22573]
char estimate_drivingSide(char driving_side)
#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:540
void pgr_get_points(char *points_sql, Point_on_edge_t **points, size_t *total_points)
pgr_get_points
Definition: points_input.c:63
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
enforces the input array to be NOT empty
Definition: arrays_input.c:124
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)
void pgr_SPI_connect(void)
PGDLLEXPORT Datum many_withPointsDD(PG_FUNCTION_ARGS)
void do_pgr_many_withPointsDD(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 s_len, double distance, bool directed, char driving_side, bool details, bool equiCost, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
static void process(char *edges_sql, char *points_sql, ArrayType *starts, double distance, bool directed, char *driving_side, bool details, bool equicost, General_path_element_t **result_tuples, size_t *result_count)
PG_FUNCTION_INFO_V1(many_withPointsDD)