PGROUTING  3.2
coordinates_input.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: matrixRows_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 
26 
27 #include "c_types/column_info_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[3],
40  int64_t *default_id,
41  Coordinate_t *distance) {
42  if (column_found(info[0].colNumber)) {
43  distance->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
44  } else {
45  distance->id = *default_id;
46  ++(*default_id);
47  }
48  distance->x = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
49  distance->y = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
50 }
51 
58  char *sql,
59  Coordinate_t **coordinates,
60  size_t *total_coordinates) {
61  clock_t start_t = clock();
62 
63  const int tuple_limit = 1000000;
64 
65  size_t total_tuples = 0;
66 
67  Column_info_t info[3];
68 
69  int i;
70  for (i = 0; i < 3; ++i) {
71  info[i].colNumber = -1;
72  info[i].type = 0;
73  info[i].strict = true;
74  info[i].eType = ANY_NUMERICAL;
75  }
76  info[0].name = "id";
77  info[1].name = "x";
78  info[2].name = "y";
79 
80  info[0].eType = ANY_INTEGER;
81  info[0].strict = false;
82 
83 
84  void *SPIplan;
85  SPIplan = pgr_SPI_prepare(sql);
86 
87  Portal SPIportal;
88  SPIportal = pgr_SPI_cursor_open(SPIplan);
89 
90 
91  bool moredata = true;
92  (*total_coordinates) = total_tuples;
93 
94  int64_t default_id = 1;
95 
96  while (moredata == true) {
97  SPI_cursor_fetch(SPIportal, true, tuple_limit);
98  if (total_tuples == 0)
99  pgr_fetch_column_info(info, 3);
100 
101  size_t ntuples = SPI_processed;
102  total_tuples += ntuples;
103 
104  if (ntuples > 0) {
105  if ((*coordinates) == NULL)
106  (*coordinates) = (Coordinate_t *)
107  palloc0(total_tuples * sizeof(Coordinate_t));
108  else
109  (*coordinates) = (Coordinate_t *)
110  repalloc((*coordinates),
111  total_tuples * sizeof(Coordinate_t));
112 
113  if ((*coordinates) == NULL) {
114  elog(ERROR, "Out of memory");
115  }
116 
117  SPITupleTable *tuptable = SPI_tuptable;
118  TupleDesc tupdesc = SPI_tuptable->tupdesc;
119  PGR_DBG("Processing %ld coordinates tupĺes", ntuples);
120 
121  size_t t;
122  for (t = 0; t < ntuples; t++) {
123  HeapTuple tuple = tuptable->vals[t];
124  pgr_fetch_row(&tuple, &tupdesc, info,
125  &default_id,
126  &(*coordinates)[total_tuples - ntuples + t]);
127  }
128  SPI_freetuptable(tuptable);
129  } else {
130  moredata = false;
131  }
132  }
133 
134  SPI_cursor_close(SPIportal);
135 
136 
137  if (total_tuples == 0) {
138  (*total_coordinates) = 0;
139  PGR_DBG("NO coordinates");
140  return;
141  }
142 
143  (*total_coordinates) = total_tuples;
144  time_msg(" reading coordinates:", start_t, clock());
145 }
Column_info_t::colNumber
int colNumber
Definition: column_info_t.h:51
Coordinate_t::y
double y
Definition: coordinate_t.h:40
time_msg.h
pgr_fetch_row
static void pgr_fetch_row(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[3], int64_t *default_id, Coordinate_t *distance)
Definition: coordinates_input.c:36
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_coordinates
void pgr_get_coordinates(char *sql, Coordinate_t **coordinates, size_t *total_coordinates)
bigint id, float x, float y,
Definition: coordinates_input.c:57
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
coordinates_input.h
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
Coordinate_t
Definition: coordinate_t.h:37
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
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
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
Coordinate_t::id
int64_t id
Definition: coordinate_t.h:38
column_info_t.h
Coordinate_t::x
double x
Definition: coordinate_t.h:39
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
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