PGROUTING  2.6
points_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: points_input.c
3 
4 Copyright (c) 2015 Celia Virginia Vergara Castillo
5 vicky_vergara@hotmail.com
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/points_input.h"
26 
27 #include "c_types/column_info_t.h"
28 
29 #include "c_common/debug_macro.h"
31 
32 
33 
34 static
36  HeapTuple *tuple,
37  TupleDesc *tupdesc,
38  Column_info_t info[4],
39  int64_t *default_pid,
40  char default_side,
41  Point_on_edge_t *point) {
42  if (column_found(info[0].colNumber)) {
43  point->pid = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
44  } else {
45  point->pid = *default_pid;
46  ++(*default_pid);
47  }
48 
49  point->edge_id = pgr_SPI_getBigInt(tuple, tupdesc, info[1]);
50  point->fraction = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
51 
52  if (column_found(info[3].colNumber)) {
53  point->side =
54  (char)pgr_SPI_getChar(tuple, tupdesc, info[3], false, default_side);
55  } else {
56  point->side = default_side;
57  }
58 }
59 
60 
61 // pid, edge_id, fraction, [side]
62 void
64  char *points_sql,
65  Point_on_edge_t **points,
66  size_t *total_points) {
67  const int tuple_limit = 1000;
68 
69  size_t ntuples;
70  size_t total_tuples;
71  Column_info_t info[4];
72 
73  int i;
74  for (i = 0; i < 4; ++i) {
75  info[i].colNumber = -1;
76  info[i].type = 0;
77  info[i].strict = true;
78  info[i].eType = ANY_INTEGER;
79  }
80 
81  info[0].name = "pid";
82  info[1].name = "edge_id";
83  info[2].name = "fraction";
84  info[3].name = "side";
85 
86  info[0].strict = false;
87  info[3].strict = false;
88  info[2].eType = ANY_NUMERICAL;
89  info[3].eType = CHAR1;
90 
91 
92  void *SPIplan;
93  SPIplan = pgr_SPI_prepare(points_sql);
94 
95  Portal SPIportal;
96  SPIportal = pgr_SPI_cursor_open(SPIplan);
97 
98  bool moredata = true;
99  (*total_points) = total_tuples = 0;
100 
101  int64_t default_pid = 1;
102  char default_side = 'b';
103 
104  while (moredata == true) {
105  SPI_cursor_fetch(SPIportal, true, tuple_limit);
106  if (total_tuples == 0) {
107  /* on the first tuple get the column information */
108  pgr_fetch_column_info(info, 4);
109  }
110 
111  ntuples = SPI_processed;
112  total_tuples += ntuples;
113 
114  if (ntuples > 0) {
115  if ((*points) == NULL)
116  (*points) = (Point_on_edge_t *)
117  palloc0(total_tuples * sizeof(Point_on_edge_t));
118  else
119  (*points) = (Point_on_edge_t *)
120  repalloc((*points), total_tuples * sizeof(Point_on_edge_t));
121 
122  if ((*points) == NULL) {
123  elog(ERROR, "Out of memory");
124  }
125 
126  SPITupleTable *tuptable = SPI_tuptable;
127  TupleDesc tupdesc = SPI_tuptable->tupdesc;
128  size_t t;
129 
130  PGR_DBG("processing %ld points tuples", ntuples);
131  for (t = 0; t < ntuples; t++) {
132  HeapTuple tuple = tuptable->vals[t];
133  fetch_point(&tuple, &tupdesc, info,
134  &default_pid, default_side,
135  &(*points)[total_tuples - ntuples + t]);
136  }
137  SPI_freetuptable(tuptable);
138  } else {
139  moredata = false;
140  }
141  }
142  SPI_cursor_close(SPIportal);
143 
144 
145  if (total_tuples == 0) {
146  (*total_points) = 0;
147  PGR_DBG("NO points");
148  return;
149  }
150 
151  (*total_points) = total_tuples;
152  PGR_DBG("Finish reading %ld points, %ld", total_tuples, (*total_points));
153 }
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in integer type.
char pgr_SPI_getChar(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info, bool strict, char default_value)
Function return the value of specified column in char type.
#define PGR_DBG(...)
Definition: debug_macro.h:34
uint64_t type
Definition: column_info_t.h:73
void pgr_get_points(char *points_sql, Point_on_edge_t **points, size_t *total_points)
pgr_get_points
Definition: points_input.c:63
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...
static void fetch_point(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[4], int64_t *default_pid, char default_side, Point_on_edge_t *point)
Definition: points_input.c:35
bool column_found(int colNumber)
Function will check whether the colNumber represent any specific column or NULL (SPI_ERROR_NOATTRIBUT...
Portal pgr_SPI_cursor_open(SPIPlanPtr SPIplan)
double pgr_SPI_getFloat8(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
Function returns the value of specified column in double type.
SPIPlanPtr pgr_SPI_prepare(char *sql)
expectType eType
Definition: column_info_t.h:76