PGROUTING  3.2
many_to_dist_driving_distance.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: many_to_dist_driving_distance.c
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
6 
7 ------
8 
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13 
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23  ********************************************************************PGR-GNU*/
24 
25 #include <stdbool.h>
27 #include "utils/array.h"
28 
29 #include "c_common/debug_macro.h"
30 #include "c_common/e_report.h"
31 #include "c_common/time_msg.h"
32 
33 #include "c_common/edges_input.h"
34 #include "c_common/arrays_input.h"
36 
37 
38 PGDLLEXPORT Datum _pgr_drivingdistance(PG_FUNCTION_ARGS);
40 
41 
42 static
43 void process(
44  char* sql,
45  ArrayType *starts,
46  float8 distance,
47  bool directed,
48  bool equicost,
49  General_path_element_t **result_tuples,
50  size_t *result_count) {
52 
53  size_t size_start_vidsArr = 0;
54  int64_t* start_vidsArr = pgr_get_bigIntArray(&size_start_vidsArr, starts);
55 
56  pgr_edge_t *edges = NULL;
57  size_t total_tuples = 0;
58  pgr_get_edges(sql, &edges, &total_tuples);
59 
60  if (total_tuples == 0) {
61  return;
62  }
63 
64  PGR_DBG("Starting timer");
65  clock_t start_t = clock();
66  char* log_msg = NULL;
67  char* notice_msg = NULL;
68  char* err_msg = NULL;
70  edges, total_tuples,
71  start_vidsArr, size_start_vidsArr,
72  distance,
73  directed,
74  equicost,
75  result_tuples, result_count,
76  &log_msg,
77  &notice_msg,
78  &err_msg);
79 
80  time_msg("processing pgr_drivingDistance()",
81  start_t, clock());
82 
83  if (err_msg && (*result_tuples)) {
84  pfree(*result_tuples);
85  (*result_tuples) = NULL;
86  (*result_count) = 0;
87  }
88 
89  pgr_global_report(log_msg, notice_msg, err_msg);
90 
91  if (log_msg) pfree(log_msg);
92  if (notice_msg) pfree(notice_msg);
93  if (err_msg) pfree(err_msg);
94  if (edges) pfree(edges);
95  if (start_vidsArr) pfree(start_vidsArr);
96 
98 }
99 
100 
101 PGDLLEXPORT Datum
102 _pgr_drivingdistance(PG_FUNCTION_ARGS) {
103  FuncCallContext *funcctx;
104  TupleDesc tuple_desc;
105 
106  /**********************************************************************/
107  General_path_element_t* result_tuples = 0;
108  size_t result_count = 0;
109  /**********************************************************************/
110 
111  if (SRF_IS_FIRSTCALL()) {
112  MemoryContext oldcontext;
113 
114  funcctx = SRF_FIRSTCALL_INIT();
115  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
116 
117  /**********************************************************************/
118 
119  PGR_DBG("Calling driving_many_to_dist_driver");
120  process(
121  text_to_cstring(PG_GETARG_TEXT_P(0)),
122  PG_GETARG_ARRAYTYPE_P(1),
123  PG_GETARG_FLOAT8(2),
124  PG_GETARG_BOOL(3),
125  PG_GETARG_BOOL(4),
126  &result_tuples, &result_count);
127 
128  /**********************************************************************/
129 
130 
131 #if PGSQL_VERSION > 95
132  funcctx->max_calls = result_count;
133 #else
134  funcctx->max_calls = (uint32_t)result_count;
135 #endif
136  funcctx->user_fctx = result_tuples;
137  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
138  != TYPEFUNC_COMPOSITE)
139  ereport(ERROR,
140  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
141  errmsg("function returning record called in context "
142  "that cannot accept type record")));
143 
144  funcctx->tuple_desc = tuple_desc;
145 
146  MemoryContextSwitchTo(oldcontext);
147  }
148 
149  funcctx = SRF_PERCALL_SETUP();
150 
151  tuple_desc = funcctx->tuple_desc;
152  result_tuples = (General_path_element_t*) funcctx->user_fctx;
153 
154  if (funcctx->call_cntr < funcctx->max_calls) {
155  HeapTuple tuple;
156  Datum result;
157  Datum *values;
158  bool* nulls;
159 
160  /**********************************************************************/
161  size_t numb = 6;
162  values = palloc(numb * sizeof(Datum));
163  nulls = palloc(numb * sizeof(bool));
164 
165  size_t i;
166  for (i = 0; i < numb; ++i) {
167  nulls[i] = false;
168  }
169  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
170  values[1] = Int64GetDatum(result_tuples[funcctx->call_cntr].start_id);
171  values[2] = Int64GetDatum(result_tuples[funcctx->call_cntr].node);
172  values[3] = Int64GetDatum(result_tuples[funcctx->call_cntr].edge);
173  values[4] = Float8GetDatum(result_tuples[funcctx->call_cntr].cost);
174  values[5] = Float8GetDatum(result_tuples[funcctx->call_cntr].agg_cost);
175 
176  /**********************************************************************/
177  tuple = heap_form_tuple(tuple_desc, values, nulls);
178  result = HeapTupleGetDatum(tuple);
179 
180  pfree(values);
181  pfree(nulls);
182 
183  SRF_RETURN_NEXT(funcctx, result);
184  } else {
185  SRF_RETURN_DONE(funcctx);
186  }
187 }
188 
General_path_element_t::edge
int64_t edge
Definition: general_path_element_t.h:42
time_msg.h
postgres_connection.h
pgr_edge_t
Definition: pgr_edge_t.h:37
process
static void process(char *sql, ArrayType *starts, float8 distance, bool directed, bool equicost, General_path_element_t **result_tuples, size_t *result_count)
Definition: many_to_dist_driving_distance.c:43
do_pgr_driving_many_to_dist
void do_pgr_driving_many_to_dist(pgr_edge_t *data_edges, size_t total_edges, int64_t *start_vertex, size_t s_len, double distance, bool directedFlag, bool equiCostFlag, General_path_element_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: drivedist_driver.cpp:82
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:82
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
arrays_input.h
General_path_element_t::start_id
int64_t start_id
Definition: general_path_element_t.h:39
debug_macro.h
pgr_get_bigIntArray
int64_t * pgr_get_bigIntArray(size_t *arrlen, ArrayType *input)
Enforces the input array to be NOT empty.
Definition: arrays_input.c:146
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_pgr_drivingdistance)
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
General_path_element_t::node
int64_t node
Definition: general_path_element_t.h:41
_pgr_drivingdistance
PGDLLEXPORT Datum _pgr_drivingdistance(PG_FUNCTION_ARGS)
Definition: many_to_dist_driving_distance.c:102
pgr_get_edges
void pgr_get_edges(char *edges_sql, pgr_edge_t **edges, size_t *total_edges)
basic edge_sql
Definition: edges_input.c:711
General_path_element_t
Definition: general_path_element_t.h:37
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
edges_input.h
drivedist_driver.h
General_path_element_t::cost
double cost
Definition: general_path_element_t.h:43
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:93
General_path_element_t::agg_cost
double agg_cost
Definition: general_path_element_t.h:44