PGROUTING  3.2
delauny_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: delauny_input.c
3 
4 Copyright (c) 2018 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 "c_common/delauny_input.h"
26 
27 #include "c_types/delauny_t.h"
28 
29 #include "c_common/debug_macro.h"
31 #include "c_common/time_msg.h"
32 
33 
34 
35 static
37  HeapTuple *tuple,
38  TupleDesc *tupdesc,
39  Column_info_t info[4],
40  Delauny_t *delauny) {
41  delauny->tid = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
42  delauny->pid = pgr_SPI_getBigInt(tuple, tupdesc, info[1]);
43  delauny->x = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
44  delauny->y = pgr_SPI_getFloat8(tuple, tupdesc, info[3]);
45 }
46 
54  char *sql,
55  Delauny_t **delauny,
56  size_t *total_delauny) {
57  clock_t start_t = clock();
58 
59  const int tuple_limit = 1000000;
60 
61  size_t total_tuples = 0;
62 
63  Column_info_t info[4];
64 
65  int i;
66  for (i = 0; i < 4; ++i) {
67  info[i].colNumber = -1;
68  info[i].type = 0;
69  info[i].strict = true;
70  info[i].eType = ANY_NUMERICAL;
71  }
72  info[0].name = "tid";
73  info[1].name = "pid";
74  info[2].name = "x";
75  info[3].name = "y";
76 
77  info[0].eType = ANY_INTEGER;
78  info[1].eType = ANY_INTEGER;
79 
80  void *SPIplan;
81  SPIplan = pgr_SPI_prepare(sql);
82 
83  Portal SPIportal;
84  SPIportal = pgr_SPI_cursor_open(SPIplan);
85 
86 
87  bool moredata = true;
88  (*total_delauny) = total_tuples;
89 
90  while (moredata == true) {
91  SPI_cursor_fetch(SPIportal, true, tuple_limit);
92  if (total_tuples == 0)
93  pgr_fetch_column_info(info, 4);
94 
95  size_t ntuples = SPI_processed;
96  total_tuples += ntuples;
97 
98  if (ntuples > 0) {
99  if ((*delauny) == NULL)
100  (*delauny) = (Delauny_t *)
101  palloc0(total_tuples * sizeof(Delauny_t));
102  else
103  (*delauny) = (Delauny_t *)
104  repalloc((*delauny),
105  total_tuples * sizeof(Delauny_t));
106 
107  if ((*delauny) == NULL) {
108  elog(ERROR, "Out of memory");
109  }
110 
111  SPITupleTable *tuptable = SPI_tuptable;
112  TupleDesc tupdesc = SPI_tuptable->tupdesc;
113 
114  size_t t;
115  for (t = 0; t < ntuples; t++) {
116  HeapTuple tuple = tuptable->vals[t];
117  pgr_fetch_row(&tuple, &tupdesc, info,
118  &(*delauny)[total_tuples - ntuples + t]);
119  }
120  SPI_freetuptable(tuptable);
121  } else {
122  moredata = false;
123  }
124  }
125 
126  SPI_cursor_close(SPIportal);
127 
128 
129  if (total_tuples == 0) {
130  (*total_delauny) = 0;
131  return;
132  }
133 
134  (*total_delauny) = total_tuples;
135  PGR_DBG("%ld", total_tuples);
136  time_msg(" calculating Delauny triangles:", start_t, clock());
137 }
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:51
time_msg.h
pgr_get_delauny
void pgr_get_delauny(char *sql, Delauny_t **delauny, size_t *total_delauny)
bigint tid, bigint pid, float x, float y,
Definition: delauny_input.c:53
Column_info_t::strict
bool strict
Definition: column_info_t.h:53
Delauny_t
Definition: delauny_t.h:37
pgr_fetch_row
static void pgr_fetch_row(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[4], Delauny_t *delauny)
Definition: delauny_input.c:36
pgr_SPI_prepare
SPIPlanPtr pgr_SPI_prepare(char *sql)
Definition: postgres_connection.c:94
pgr_SPI_getBigInt
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in integer type.
Definition: get_check_data.c:216
pgr_fetch_column_info
void pgr_fetch_column_info(Column_info_t info[], int info_size)
Function tells expected type of each column and then check the correspondence type of each column.
Definition: get_check_data.c:78
debug_macro.h
Column_info_t::name
char * name
Definition: column_info_t.h:54
pgr_SPI_cursor_open
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
Definition: postgres_connection.c:107
Delauny_t::x
double x
Definition: delauny_t.h:40
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
delauny_input.h
pgr_SPI_getFloat8
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in double type.
Definition: get_check_data.c:246
Column_info_t::eType
expectType eType
Definition: column_info_t.h:55
Delauny_t::y
double y
Definition: delauny_t.h:41
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
ANY_INTEGER
@ ANY_INTEGER
Definition: column_info_t.h:41
Delauny_t::tid
int64_t tid
Definition: delauny_t.h:38
ANY_NUMERICAL
@ ANY_NUMERICAL
Definition: column_info_t.h:42
get_check_data.h
Column_info_t::type
uint64_t type
Definition: column_info_t.h:52
Column_info_t
Definition: column_info_t.h:49
Delauny_t::pid
int64_t pid
Definition: delauny_t.h:39
delauny_t.h