PGROUTING  2.4
 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 
25 #include "./../../common/src/postgres_connection.h"
26 #include "utils/array.h"
27 
28 #include "./../../common/src/debug_macro.h"
29 #include "./../../common/src/e_report.h"
30 #include "./../../common/src/time_msg.h"
31 #include "./../../common/src/pgr_types.h"
32 #include "./../../common/src/edges_input.h"
33 #include "./../../common/src/arrays_input.h"
34 #include "./../../common/src/points_input.h"
35 #include "./../../withPoints/src/get_new_queries.h"
36 #include "./withPoints_dd_driver.h"
37 
38 
39 PGDLLEXPORT Datum many_withPointsDD(PG_FUNCTION_ARGS);
41 
42 static
43 void process(
44  char* edges_sql,
45  char* points_sql,
46  ArrayType* starts,
47  double distance,
48 
49  bool directed,
50  char *driving_side,
51  bool details,
52  bool equicost,
53 
54  General_path_element_t **result_tuples,
55  size_t *result_count) {
56  driving_side[0] = estimate_drivingSide(driving_side[0]);
57  PGR_DBG("estimated driving side:%c", driving_side[0]);
58 
60 
61  size_t total_starts = 0;
62  int64_t* start_pidsArr = pgr_get_bigIntArray(&total_starts, starts);
63  PGR_DBG("sourcesArr size %ld ", total_starts);
64 
65  Point_on_edge_t *points = NULL;
66  size_t total_points = 0;
67  pgr_get_points(points_sql, &points, &total_points);
68 
69  char *edges_of_points_query = NULL;
70  char *edges_no_points_query = NULL;
72  edges_sql, points_sql,
73  &edges_of_points_query,
74  &edges_no_points_query);
75 
76 
77  pgr_edge_t *edges_of_points = NULL;
78  size_t total_edges_of_points = 0;
80  edges_of_points_query, &edges_of_points, &total_edges_of_points);
81 
82  pgr_edge_t *edges = NULL;
83  size_t total_edges = 0;
84  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
85 
86  PGR_DBG("freeing allocated memory not used anymore");
87  free(edges_of_points_query);
88  free(edges_no_points_query);
89 
90  if ((total_edges + total_edges_of_points) == 0) {
91  if (edges) pfree(edges);
92  if (edges_of_points) pfree(edges_of_points);
93  if (points) pfree(points);
95  return;
96  }
97 
98  PGR_DBG("Starting timer");
99  clock_t start_t = clock();
100  char* log_msg = NULL;
101  char* notice_msg = NULL;
102  char* err_msg = NULL;
104  edges, total_edges,
105  points, total_points,
106  edges_of_points, total_edges_of_points,
107  start_pidsArr, total_starts,
108  distance,
109 
110  directed,
111  driving_side[0],
112  details,
113  equicost,
114 
115  result_tuples,
116  result_count,
117  &log_msg,
118  &notice_msg,
119  &err_msg);
120  time_msg(" processing withPointsDD many starts", start_t, clock());
121 
122  if (err_msg && (*result_tuples)) {
123  pfree(*result_tuples);
124  (*result_count) = 0;
125  (*result_tuples) = NULL;
126  }
127 
128  pgr_global_report(log_msg, notice_msg, err_msg);
129 
130  if (log_msg) pfree(log_msg);
131  if (notice_msg) pfree(notice_msg);
132  if (err_msg) pfree(err_msg);
133  if (edges) pfree(edges);
134  if (edges_of_points) pfree(edges_of_points);
135  if (points) pfree(points);
136  if (start_pidsArr) pfree(start_pidsArr);
137  pgr_SPI_finish();
138 }
139 
140 
141 
142 PGDLLEXPORT Datum
143 many_withPointsDD(PG_FUNCTION_ARGS) {
144  FuncCallContext *funcctx;
145  TupleDesc tuple_desc;
146 
147  /**********************************************************************/
148  General_path_element_t *result_tuples = 0;
149  size_t result_count = 0;
150  /**********************************************************************/
151 
152 
153  if (SRF_IS_FIRSTCALL()) {
154  MemoryContext oldcontext;
155  funcctx = SRF_FIRSTCALL_INIT();
156  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
157 
158 
159  /**********************************************************************/
160  // CREATE OR REPLACE FUNCTION pgr_withPointsDD(
161  // edges_sql TEXT,
162  // points_sql TEXT,
163  // start_pids anyarray,
164  // distance FLOAT,
165  //
166  // directed BOOLEAN -- DEFAULT true,
167  // driving_side CHAR -- DEFAULT 'b',
168  // details BOOLEAN -- DEFAULT false,
169  // equicost BOOLEAN -- DEFAULT false,
170 
171 
172  PGR_DBG("Calling driving_many_to_dist_driver");
173  process(
174  text_to_cstring(PG_GETARG_TEXT_P(0)),
175  text_to_cstring(PG_GETARG_TEXT_P(1)),
176  PG_GETARG_ARRAYTYPE_P(2),
177  PG_GETARG_FLOAT8(3),
178 
179  PG_GETARG_BOOL(4),
180  text_to_cstring(PG_GETARG_TEXT_P(5)),
181  PG_GETARG_BOOL(6),
182  PG_GETARG_BOOL(7),
183  &result_tuples, &result_count);
184 
185  /**********************************************************************/
186 
187 #if PGSQL_VERSION > 95
188  funcctx->max_calls = result_count;
189 #else
190  funcctx->max_calls = (uint32_t)result_count;
191 #endif
192  funcctx->user_fctx = result_tuples;
193  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
194  != TYPEFUNC_COMPOSITE)
195  ereport(ERROR,
196  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
197  errmsg("function returning record called in context "
198  "that cannot accept type record")));
199 
200  funcctx->tuple_desc = tuple_desc;
201 
202  MemoryContextSwitchTo(oldcontext);
203  }
204 
205  funcctx = SRF_PERCALL_SETUP();
206 
207  tuple_desc = funcctx->tuple_desc;
208  result_tuples = (General_path_element_t*) funcctx->user_fctx;
209 
210  if (funcctx->call_cntr < funcctx->max_calls) {
211  HeapTuple tuple;
212  Datum result;
213  Datum *values;
214  bool* nulls;
215 
216  /**********************************************************************/
217  size_t numb = 6;
218  values = palloc(numb * sizeof(Datum));
219  nulls = palloc(numb * sizeof(bool));
220 
221  size_t i;
222  for (i = 0; i < numb; ++i) {
223  nulls[i] = false;
224  }
225 
226  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
227  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
228  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
229  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
230  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
231  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
232 
233  /**********************************************************************/
234 
235  tuple = heap_form_tuple(tuple_desc, values, nulls);
236  result = HeapTupleGetDatum(tuple);
237 
238  pfree(values);
239  pfree(nulls);
240 
241  SRF_RETURN_NEXT(funcctx, result);
242  } else {
243  SRF_RETURN_DONE(funcctx);
244  }
245 }
246 
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: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)
edge_astar_t * edges
Definition: BDATester.cpp:46
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)
char * err_msg
Definition: BDATester.cpp:50
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)