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
withPoints_dd.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: withPoints_dd.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 "postgres.h"
31 #include "executor/spi.h"
32 #include "funcapi.h"
33 #include "utils/array.h"
34 #include "catalog/pg_type.h"
35 #if PGSQL_VERSION > 92
36 #include "access/htup_details.h"
37 #endif
38 
39 
40 #include "fmgr.h"
41 #include "./../../common/src/pgr_types.h"
42 #include "./../../common/src/time_msg.h"
43 #include "./../../common/src/postgres_connection.h"
44 #include "./../../common/src/edges_input.h"
45 #include "./../../common/src/points_input.h"
46 #include "./../../withPoints/src/get_new_queries.h"
47 #include "./withPoints_dd_driver.h"
48 // #define DEBUG
49 #include "./../../common/src/debug_macro.h"
50 
51 PGDLLEXPORT Datum withPoints_dd(PG_FUNCTION_ARGS);
52 
53 
54 /*******************************************************************************/
55 /* MODIFY AS NEEDED */
56 static
57 void
59  char* edges_sql,
60  char* points_sql,
61  int64_t start_pid,
62  double distance,
63 
64  bool directed,
65  char *driving_side,
66  bool details,
67 
68  General_path_element_t **result_tuples,
69  size_t *result_count) {
70 
71  driving_side[0] = (char) tolower(driving_side[0]);
72  PGR_DBG("driving side:%c",driving_side[0]);
73  if (! ((driving_side[0] == 'r')
74  || (driving_side[0] == 'l'))) {
75  driving_side[0] = 'b';
76  }
77 
79 
80  Point_on_edge_t *points = NULL;
81  size_t total_points = 0;
82  pgr_get_points(points_sql, &points, &total_points);
83 
84  char *edges_of_points_query = NULL;
85  char *edges_no_points_query = NULL;
87  edges_sql, points_sql,
88  &edges_of_points_query,
89  &edges_no_points_query);
90 
91 
92  pgr_edge_t *edges_of_points = NULL;
93  size_t total_edges_of_points = 0;
94  pgr_get_edges(edges_of_points_query, &edges_of_points, &total_edges_of_points);
95 
96  pgr_edge_t *edges = NULL;
97  size_t total_edges = 0;
98  pgr_get_edges(edges_no_points_query, &edges, &total_edges);
99 
100  PGR_DBG("freeing allocated memory not used anymore");
101  free(edges_of_points_query);
102  free(edges_no_points_query);
103 
104  if ((total_edges + total_edges_of_points) == 0) {
105  PGR_DBG("No edges found");
106  (*result_count) = 0;
107  (*result_tuples) = NULL;
108  pgr_SPI_finish();
109  return;
110  }
111 
112  PGR_DBG("Starting processing");
113  char *err_msg = NULL;
114  clock_t start_t = clock();
115  int errcode = do_pgr_withPointsDD(
116  edges, total_edges,
117  points, total_points,
118  edges_of_points, total_edges_of_points,
119  start_pid,
120  distance,
121 
122  driving_side[0],
123  details,
124  directed,
125 
126  result_tuples,
127  result_count,
128  &err_msg);
129  time_msg(" processing withPointsDD one start", start_t, clock());
130  PGR_DBG("Returning %ld tuples\n", *result_count);
131  PGR_DBG("Returned message = %s\n", err_msg);
132 
133  if (!err_msg) free(err_msg);
134 
135  pfree(edges);
136  pfree(edges_of_points);
137  pfree(points);
138 
139  pgr_SPI_finish();
140 
141  if (errcode) {
142  pgr_send_error(errcode);
143  }
144 
145 }
146 
147 /* */
148 /*******************************************************************************/
149 
150 
151 
153 PGDLLEXPORT Datum
154 withPoints_dd(PG_FUNCTION_ARGS) {
155  FuncCallContext *funcctx;
156  uint32_t call_cntr;
157  uint32_t max_calls;
158  TupleDesc tuple_desc;
159 
160  /*******************************************************************************/
161  /* MODIFY AS NEEDED */
162  /* */
163  General_path_element_t *result_tuples = 0;
164  size_t result_count = 0;
165  /* */
166  /*******************************************************************************/
167 
168  if (SRF_IS_FIRSTCALL()) {
169  MemoryContext oldcontext;
170  funcctx = SRF_FIRSTCALL_INIT();
171  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
172 
173 
174  /*******************************************************************************/
175  /* MODIFY AS NEEDED */
176  // CREATE OR REPLACE FUNCTION pgr_withPoint(
177  // edges_sql TEXT,
178  // points_sql TEXT,
179  // start_pid BIGINT,
180  // distance FLOAT,
181  //
182  // directed BOOLEAN -- DEFAULT true,
183  // driving_side CHAR -- DEFAULT 'b',
184  // details BOOLEAN -- DEFAULT false,
185 
186  PGR_DBG("Calling process");
187  process(
188  pgr_text2char(PG_GETARG_TEXT_P(0)),
189  pgr_text2char(PG_GETARG_TEXT_P(1)),
190  PG_GETARG_INT64(2),
191  PG_GETARG_FLOAT8(3),
192 
193  PG_GETARG_BOOL(4),
194  pgr_text2char(PG_GETARG_TEXT_P(5)),
195  PG_GETARG_BOOL(6),
196  &result_tuples,
197  &result_count);
198 
199  /* */
200  /*******************************************************************************/
201 
202  funcctx->max_calls = (uint32_t) result_count;
203  funcctx->user_fctx = result_tuples;
204  if (get_call_result_type(fcinfo, NULL, &tuple_desc) != TYPEFUNC_COMPOSITE)
205  ereport(ERROR,
206  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
207  errmsg("function returning record called in context "
208  "that cannot accept type record")));
209 
210  funcctx->tuple_desc = tuple_desc;
211  MemoryContextSwitchTo(oldcontext);
212  }
213 
214  funcctx = SRF_PERCALL_SETUP();
215  call_cntr = funcctx->call_cntr;
216  max_calls = funcctx->max_calls;
217  tuple_desc = funcctx->tuple_desc;
218  result_tuples = (General_path_element_t*) funcctx->user_fctx;
219 
220  if (call_cntr < max_calls) {
221  HeapTuple tuple;
222  Datum result;
223  Datum *values;
224  bool* nulls;
225 
226  /*******************************************************************************/
227  /* MODIFY AS NEEDED */
228  // OUT seq BIGINT,
229  // OUT node BIGINT,
230  // OUT edge BIGINT,
231  // OUT cost FLOAT,
232  // OUT agg_cost FLOAT)
233 
234 
235  values = palloc(5 * sizeof(Datum));
236  nulls = palloc(5 * sizeof(bool));
237 
238  size_t i;
239  for(i = 0; i < 6; ++i) {
240  nulls[i] = false;
241  }
242 
243 
244  // postgres starts counting from 1
245  values[0] = Int32GetDatum(call_cntr + 1);
246  values[1] = Int64GetDatum(result_tuples[call_cntr].node);
247  values[2] = Int64GetDatum(result_tuples[call_cntr].edge);
248  values[3] = Float8GetDatum(result_tuples[call_cntr].cost);
249  values[4] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
250  /*******************************************************************************/
251 
252  tuple = heap_form_tuple(tuple_desc, values, nulls);
253  result = HeapTupleGetDatum(tuple);
254  SRF_RETURN_NEXT(funcctx, result);
255  } else {
256  // cleanup
257  if (result_tuples) free(result_tuples);
258 
259  SRF_RETURN_DONE(funcctx);
260  }
261 }
262 
#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
PG_FUNCTION_INFO_V1(withPoints_dd)
void pgr_get_points(char *points_sql, Point_on_edge_t **points, size_t *total_points)
pgr_get_points
Definition: points_input.c:62
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
void pgr_SPI_finish(void)
int do_pgr_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_vid, double distance, char driving_side, bool details, bool directed, General_path_element_t **return_tuples, size_t *return_count, char **err_msg)
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)
static void process(char *edges_sql, char *points_sql, int64_t start_pid, double distance, bool directed, char *driving_side, bool details, General_path_element_t **result_tuples, size_t *result_count)
Definition: withPoints_dd.c:58
edge_astar_t * edges
Definition: BDATester.cpp:46
void pgr_SPI_connect(void)
char * err_msg
Definition: BDATester.cpp:50
char * pgr_text2char(text *in)
PGDLLEXPORT Datum withPoints_dd(PG_FUNCTION_ARGS)