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