pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 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 // #define DEBUG
26 #include "postgres.h"
27 #include "executor/spi.h"
28 #include "funcapi.h"
29 #include "utils/array.h"
30 #include "catalog/pg_type.h"
31 #if PGSQL_VERSION > 92
32 #include "access/htup_details.h"
33 #endif
34 
35 #include "fmgr.h"
36 #include "./../../common/src/debug_macro.h"
37 #include "./../../common/src/time_msg.h"
38 #include "./../../common/src/pgr_types.h"
39 #include "./../../common/src/postgres_connection.h"
40 #include "./../../common/src/edges_input.h"
41 #include "./../../common/src/arrays_input.h"
42 #include "./../../common/src/points_input.h"
43 #include "./../../withPoints/src/get_new_queries.h"
44 #include "./withPoints_dd_driver.h"
45 
46 
47 PGDLLEXPORT Datum many_withPointsDD(PG_FUNCTION_ARGS);
48 
49 static
50 void process(
51  char* edges_sql,
52  char* points_sql,
53  int64_t *start_pids_arr, size_t size_arr,
54  double distance,
55 
56  bool directed,
57  char *driving_side,
58  bool details,
59  bool equicost,
60 
61  General_path_element_t **result_tuples,
62  size_t *result_count ){
63 
64  driving_side[0] = (char) tolower(driving_side[0]);
65  PGR_DBG("driving side:%c",driving_side[0]);
66  if (! ((driving_side[0] == 'r')
67  || (driving_side[0] == 'l'))) {
68  driving_side[0] = 'b';
69  }
70 
71 
73  Point_on_edge_t *points = NULL;
74  size_t total_points = 0;
75  pgr_get_points(points_sql, &points, &total_points);
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;
87  pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points);
88 
89  pgr_edge_t *edges = NULL;
90  size_t total_edges = 0;
91  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
92 
93  PGR_DBG("freeing allocated memory not used anymore");
94  free(edges_of_points_query);
95  free(edges_no_points_query);
96 
97  if ((total_edges + total_edges_of_points) == 0) {
98  PGR_DBG("No edges found");
99  (*result_count) = 0;
100  (*result_tuples) = NULL;
101  pgr_SPI_finish();
102  return;
103  }
104 
105  PGR_DBG("Starting processing");
106  char *err_msg = NULL;
107  clock_t start_t = clock();
108  int errcode = do_pgr_many_withPointsDD(
109  edges, total_edges,
110  points, total_points,
111  edges_of_points, total_edges_of_points,
112  start_pids_arr, size_arr,
113  distance,
114 
115  directed,
116  driving_side[0],
117  details,
118  equicost,
119 
120  result_tuples,
121  result_count,
122  &err_msg);
123  time_msg(" processing withPointsDD many starts", start_t, clock());
124 
125  PGR_DBG("Returning %ld tuples\n", *result_count);
126  PGR_DBG("Returned message = %s\n", err_msg);
127 
128  if (!err_msg) free(err_msg);
129 
130  pfree(edges);
131  pfree(edges_of_points);
132  pfree(points);
133 
134  pgr_SPI_finish();
135 
136  if (errcode) {
137  pgr_send_error(errcode);
138  }
139 }
140 
141 
142 
144 PGDLLEXPORT Datum
145 many_withPointsDD(PG_FUNCTION_ARGS) {
146  FuncCallContext *funcctx;
147  uint32_t call_cntr;
148  uint32_t max_calls;
149  TupleDesc tuple_desc;
150 
151  /*******************************************************************************/
152  /* MODIFY AS NEEDED */
153  /* */
154  General_path_element_t *result_tuples = 0;
155  size_t result_count = 0;
156  /* */
157  /*******************************************************************************/
158 
159 
160  if (SRF_IS_FIRSTCALL()) {
161  MemoryContext oldcontext;
162  funcctx = SRF_FIRSTCALL_INIT();
163  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
164 
165 
166  /*******************************************************************************/
167  /* MODIFY AS NEEDED */
168  // CREATE OR REPLACE FUNCTION pgr_withPointsDD(
169  // edges_sql TEXT,
170  // points_sql TEXT,
171  // start_pids anyarray,
172  // distance FLOAT,
173  //
174  // directed BOOLEAN -- DEFAULT true,
175  // driving_side CHAR -- DEFAULT 'b',
176  // details BOOLEAN -- DEFAULT false,
177  // equicost BOOLEAN -- DEFAULT false,
178 
179  int64_t *sourcesArr;
180  size_t num;
181 
182  sourcesArr = (int64_t*) pgr_get_bigIntArray(&num, PG_GETARG_ARRAYTYPE_P(2));
183  PGR_DBG("sourcesArr size %ld ", num);
184 
185  PGR_DBG("Calling driving_many_to_dist_driver");
186  process(
187  pgr_text2char(PG_GETARG_TEXT_P(0)), // edges_sql
188  pgr_text2char(PG_GETARG_TEXT_P(1)), // points_sql
189  sourcesArr, num, // start_pids array
190  PG_GETARG_FLOAT8(3), // distance
191 
192  PG_GETARG_BOOL(4), // directed
193  pgr_text2char(PG_GETARG_TEXT_P(5)), // driving side
194  PG_GETARG_BOOL(6), // details
195  PG_GETARG_BOOL(7), // equicost
196  &result_tuples, &result_count);
197 
198  free(sourcesArr);
199 
200  /* total number of tuples to be returned */
201  funcctx->max_calls = (uint32_t)result_count;
202  funcctx->user_fctx = result_tuples;
203  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != 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 
211  MemoryContextSwitchTo(oldcontext);
212  }
213 
214  /* stuff done on every call of the function */
215  funcctx = SRF_PERCALL_SETUP();
216 
217  call_cntr = funcctx->call_cntr;
218  max_calls = funcctx->max_calls;
219  tuple_desc = funcctx->tuple_desc;
220  result_tuples = (General_path_element_t*) funcctx->user_fctx;
221 
222  /* do when there is more left to send */
223  if (call_cntr < max_calls) {
224  HeapTuple tuple;
225  Datum result;
226  Datum *values;
227  bool* nulls;
228 
229  values = palloc(6 * sizeof(Datum));
230  nulls = palloc(6 * sizeof(bool));
231  // id, start_v, node, edge, cost, tot_cost
232  nulls[0] = false;
233  nulls[1] = false;
234  nulls[2] = false;
235  nulls[3] = false;
236  nulls[4] = false;
237  nulls[5] = false;
238  values[0] = Int32GetDatum(call_cntr + 1);
239  values[1] = Int64GetDatum(result_tuples[call_cntr].start_id);
240  values[2] = Int64GetDatum(result_tuples[call_cntr].node);
241  values[3] = Int64GetDatum(result_tuples[call_cntr].edge);
242  values[4] = Float8GetDatum(result_tuples[call_cntr].cost);
243  values[5] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
244 
245  tuple = heap_form_tuple(tuple_desc, values, nulls);
246 
247  /* make the tuple into a datum */
248  result = HeapTupleGetDatum(tuple);
249 
250  /* clean up (this is not really necessary) */
251  pfree(values);
252  pfree(nulls);
253 
254  SRF_RETURN_NEXT(funcctx, result);
255  } else {
256  /* do when there is no more left */
257  if (result_tuples) free(result_tuples);
258  SRF_RETURN_DONE(funcctx);
259  }
260 }
261 
#define PGR_DBG(...)
Definition: debug_macro.h:33
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:535
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:38
int 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_pids_arr, 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 **err_msg)
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
void pgr_SPI_finish(void)
void pgr_send_error(int errcode)
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)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)
static void process(char *edges_sql, char *points_sql, int64_t *start_pids_arr, size_t size_arr, 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)