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