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