PGROUTING  2.4
 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 "./../../common/src/postgres_connection.h"
31 #include "utils/array.h"
32 
33 #include "./../../common/src/debug_macro.h"
34 #include "./../../common/src/e_report.h"
35 #include "./../../common/src/time_msg.h"
36 #include "./../../common/src/pgr_types.h"
37 #include "./../../common/src/edges_input.h"
38 #include "./../../common/src/points_input.h"
39 #include "./get_new_queries.h"
41 
42 PGDLLEXPORT Datum one_to_one_withPoints(PG_FUNCTION_ARGS);
44 
45 static
46 void
48  char* edges_sql,
49  char* points_sql,
50  int64_t start_pid,
51  int64_t end_pid,
52  bool directed,
53  char *driving_side,
54  bool details,
55  bool only_cost,
56  General_path_element_t **result_tuples,
57  size_t *result_count) {
58  driving_side[0] = estimate_drivingSide(driving_side[0]);
59  PGR_DBG("estimated driving side:%c", driving_side[0]);
60 
62 
63  PGR_DBG("load the points");
64  Point_on_edge_t *points = NULL;
65  size_t total_points = 0;
66  pgr_get_points(points_sql, &points, &total_points);
67 
68  PGR_DBG(" -- change the query");
69  char *edges_of_points_query = NULL;
70  char *edges_no_points_query = NULL;
72  edges_sql, points_sql,
73  &edges_of_points_query,
74  &edges_no_points_query);
75 
76  PGR_DBG("edges_of_points_query:\n%s", edges_of_points_query);
77  PGR_DBG("edges_no_points_query:\n%s", edges_no_points_query);
78 
79  PGR_DBG("load the edges that match the points");
80  pgr_edge_t *edges_of_points = NULL;
81  size_t total_edges_of_points = 0;
83  edges_of_points_query, &edges_of_points, &total_edges_of_points);
84 
85  PGR_DBG("Total %ld edges in query:", total_edges_of_points);
86 
87 
88  PGR_DBG("load the edges that don't match the points");
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("Total %ld edges in query:", total_edges);
94 
95  PGR_DBG("freeing allocated memory not used anymore");
96  free(edges_of_points_query);
97  free(edges_no_points_query);
98 
99  if ((total_edges + total_edges_of_points) == 0) {
100  PGR_DBG("No edges found");
101  pgr_SPI_finish();
102  return;
103  }
104 
105  PGR_DBG("Starting timer");
106  clock_t start_t = clock();
107  char* log_msg = NULL;
108  char* notice_msg = NULL;
109  char* err_msg = NULL;
111  edges, total_edges,
112  points, total_points,
113  edges_of_points, total_edges_of_points,
114  &start_pid, 1,
115  &end_pid, 1,
116  driving_side[0],
117  details,
118  directed,
119  only_cost,
120  true,
121  result_tuples, result_count,
122  &log_msg,
123  &notice_msg,
124  &err_msg);
125 
126  if (only_cost) {
127  time_msg("processing pgr_withPointsCost(one to one)", start_t, clock());
128  } else {
129  time_msg("processing pgr_withPoints(one to one)", start_t, clock());
130  }
131 
132  if (err_msg && (*result_tuples)) {
133  pfree(*result_tuples);
134  (*result_count) = 0;
135  (*result_tuples) = NULL;
136  }
137 
138  pgr_global_report(log_msg, notice_msg, err_msg);
139 
140  if (log_msg) pfree(log_msg);
141  if (notice_msg) pfree(notice_msg);
142  if (err_msg) pfree(err_msg);
143  if (edges) pfree(edges);
144  if (points) pfree(points);
145  if (edges_of_points) pfree(edges_of_points);
146 
147  pgr_SPI_finish();
148 }
149 
150 
151 PGDLLEXPORT Datum
152 one_to_one_withPoints(PG_FUNCTION_ARGS) {
153  FuncCallContext *funcctx;
154  TupleDesc tuple_desc;
155 
156  /**********************************************************************/
157  General_path_element_t *result_tuples = NULL;
158  size_t result_count = 0;
159  /**********************************************************************/
160 
161  if (SRF_IS_FIRSTCALL()) {
162  MemoryContext oldcontext;
163  funcctx = SRF_FIRSTCALL_INIT();
164  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
165 
166 
167  /**********************************************************************/
168  // CREATE OR REPLACE FUNCTION pgr_withPoint(
169  // edges_sql TEXT,
170  // points_sql TEXT,
171  // start_pid BIGINT,
172  // end_pid BIGINT,
173  // directed BOOLEAN -- DEFAULT true,
174  // driving_side CHAR -- DEFAULT 'b',
175  // details BOOLEAN -- DEFAULT true,
176  // only_cost BOOLEAN DEFAULT false,
177 
178  process(
179  text_to_cstring(PG_GETARG_TEXT_P(0)),
180  text_to_cstring(PG_GETARG_TEXT_P(1)),
181  PG_GETARG_INT64(2),
182  PG_GETARG_INT64(3),
183  PG_GETARG_BOOL(4),
184  text_to_cstring(PG_GETARG_TEXT_P(5)),
185  PG_GETARG_BOOL(6),
186  PG_GETARG_BOOL(7),
187  &result_tuples,
188  &result_count);
189  /**********************************************************************/
190 
191 #if PGSQL_VERSION > 95
192  funcctx->max_calls = result_count;
193 #else
194  funcctx->max_calls = (uint32_t)result_count;
195 #endif
196  funcctx->user_fctx = result_tuples;
197  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
198  != TYPEFUNC_COMPOSITE)
199  ereport(ERROR,
200  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
201  errmsg("function returning record called in context "
202  "that cannot accept type record")));
203 
204  funcctx->tuple_desc = tuple_desc;
205  MemoryContextSwitchTo(oldcontext);
206  }
207 
208  funcctx = SRF_PERCALL_SETUP();
209  tuple_desc = funcctx->tuple_desc;
210  result_tuples = (General_path_element_t*) funcctx->user_fctx;
211 
212  if (funcctx->call_cntr < funcctx->max_calls) {
213  HeapTuple tuple;
214  Datum result;
215  Datum *values;
216  bool * nulls;
217 
218  /**********************************************************************/
219  // OUT seq BIGINT,
220  // OUT path_seq,
221  // OUT node BIGINT,
222  // OUT edge BIGINT,
223  // OUT cost FLOAT,
224  // OUT agg_cost FLOAT)
225 
226 
227  values = palloc(6 * sizeof(Datum));
228  nulls = palloc(6 * sizeof(bool));
229 
230  size_t i;
231  for (i = 0; i < 6; ++i) {
232  nulls[i] = false;
233  }
234 
235  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
236  values[1] = Int32GetDatum(result_tuples[funcctx->call_cntr].seq);
237  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
238  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
239  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
240  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
241 
242  /**********************************************************************/
243 
244  tuple = heap_form_tuple(tuple_desc, values, nulls);
245  result = HeapTupleGetDatum(tuple);
246  SRF_RETURN_NEXT(funcctx, result);
247  } else {
248  SRF_RETURN_DONE(funcctx);
249  }
250 }
251 
char estimate_drivingSide(char driving_side)
void do_pgr_many_to_many_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_pidsArr, size_t size_start_pidsArr, int64_t *end_pidsArr, size_t size_end_pidsArr, char driving_side, bool details, bool directed, bool only_cost, bool normal, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
#define PGR_DBG(...)
Definition: debug_macro.h:34
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:539
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:32
PGDLLEXPORT Datum one_to_one_withPoints(PG_FUNCTION_ARGS)
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
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 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