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