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