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