pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
xyd_tsp.c
1 /*PGR-GNU*****************************************************************
2 File: xyz_tsp.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: vicky_vergara@hotmail.com
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/pgr_types.h"
44 #include "./../../common/src/postgres_connection.h"
45 #include "./distances_input.h"
46 #include "./tsp_driver.h"
47 
48 // #include "./one_to_one_dijkstra_driver.h"
49 
50 
51 PG_FUNCTION_INFO_V1(xyd_tsp);
52 #ifndef _MSC_VER
53 Datum
54 #else // _MSC_VER
55 PGDLLEXPORT Datum
56 #endif
57 xyd_tsp(PG_FUNCTION_ARGS);
58 
59 /******************************************************************************/
60 /* MODIFY AS NEEDED */
61 static
62 void
63 process(
64  char* distances_sql,
65  int64_t start_vid,
66  int64_t end_vid,
67  General_path_element_t **result_tuples,
68  size_t *result_count) {
69  pgr_SPI_connect();
70 
71  PGR_DBG("Load data");
72  Matrix_cell_t *distances = NULL;
73  size_t total_distances = 0;
74  pgr_get_distances(distances_sql, &distances, &total_distances);
75 
76  if (total_distances == 0) {
77  PGR_DBG("No distances found");
78  (*result_count) = 0;
79  (*result_tuples) = NULL;
80  pgr_SPI_finish();
81  return;
82  }
83  PGR_DBG("Total %ld tuples in query:", total_distances);
84 
85  PGR_DBG("Starting processing");
86  char *err_msg = NULL;
87  do_pgr_tsp(
88  distances,
89  total_distances,
90  start_vid,
91  end_vid,
92  result_tuples,
93  result_count,
94  &err_msg);
95  PGR_DBG("Returning %ld tuples\n", *result_count);
96  PGR_DBG("Returned message = %s\n", err_msg);
97 
98  free(err_msg);
99  pfree(distances);
100  pgr_SPI_finish();
101 }
102 /* */
103 /******************************************************************************/
104 
105 #ifndef _MSC_VER
106 Datum
107 #else // _MSC_VER
108 PGDLLEXPORT Datum
109 #endif
110 xyd_tsp(PG_FUNCTION_ARGS) {
111  FuncCallContext *funcctx;
112  uint32_t call_cntr;
113  uint32_t max_calls;
114  TupleDesc tuple_desc;
115 
116  /**************************************************************************/
117  /* MODIFY AS NEEDED */
118  /* */
119  General_path_element_t *result_tuples = 0;
120  size_t result_count = 0;
121  /* */
122  /**************************************************************************/
123 
124  if (SRF_IS_FIRSTCALL()) {
125  MemoryContext oldcontext;
126  funcctx = SRF_FIRSTCALL_INIT();
127  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
128 
129 
130  /**********************************************************************/
131  /* MODIFY AS NEEDED */
132  // CREATE OR REPLACE FUNCTION pgr_dijkstra(
133  // sql text,
134  // start_vid BIGINT,
135  // end_vid BIGINT,
136  // directed BOOLEAN default true,
137 
138  PGR_DBG("Calling process");
139  process(
140  pgr_text2char(PG_GETARG_TEXT_P(0)),
141  PG_GETARG_INT64(1),
142  PG_GETARG_INT64(2),
143  &result_tuples,
144  &result_count);
145 
146  /* */
147  /**********************************************************************/
148 
149  funcctx->max_calls = (uint32_t) result_count;
150  funcctx->user_fctx = result_tuples;
151  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
152  != TYPEFUNC_COMPOSITE) {
153  ereport(ERROR,
154  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
155  errmsg("function returning record called in context "
156  "that cannot accept type record")));
157  }
158 
159  funcctx->tuple_desc = tuple_desc;
160  MemoryContextSwitchTo(oldcontext);
161  }
162 
163  funcctx = SRF_PERCALL_SETUP();
164  call_cntr = funcctx->call_cntr;
165  max_calls = funcctx->max_calls;
166  tuple_desc = funcctx->tuple_desc;
167  result_tuples = (General_path_element_t*) funcctx->user_fctx;
168 
169  if (call_cntr < max_calls) {
170  HeapTuple tuple;
171  Datum result;
172  Datum *values;
173  char* nulls;
174 
175  /**********************************************************************/
176  /* MODIFY AS NEEDED */
177  // OUT seq INTEGER,
178  // OUT node BIGINT,
179  // OUT cost FLOAT,
180  // OUT agg_cost FLOAT
181 
182  values = palloc(4 * sizeof(Datum));
183  nulls = palloc(4 * sizeof(char));
184 
185 
186  size_t i;
187  for (i = 0; i < 4; ++i) {
188  nulls[i] = ' ';
189  }
190 
191  // postgres starts counting from 1
192  values[0] = Int32GetDatum(call_cntr + 1);
193  values[1] = Int64GetDatum(result_tuples[call_cntr].node);
194  values[2] = Float8GetDatum(result_tuples[call_cntr].cost);
195  values[3] = Float8GetDatum(result_tuples[call_cntr].agg_cost);
196  /**********************************************************************/
197 
198  tuple = heap_formtuple(tuple_desc, values, nulls);
199  result = HeapTupleGetDatum(tuple);
200  SRF_RETURN_NEXT(funcctx, result);
201  } else {
202  // cleanup
203  if (result_tuples) free(result_tuples);
204 
205  SRF_RETURN_DONE(funcctx);
206  }
207 }
208