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