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