PGROUTING  2.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
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 
27 #include "./coordinates_input.h"
28 #include "./debug_macro.h"
29 #include "./pgr_types.h"
30 #include "./get_check_data.h"
31 #include "./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 ntuples;
66  size_t total_tuples = 0;
67 
68  Column_info_t info[3];
69 
70  int i;
71  for (i = 0; i < 3; ++i) {
72  info[i].colNumber = -1;
73  info[i].type = 0;
74  info[i].strict = true;
75  info[i].eType = ANY_NUMERICAL;
76  }
77  info[0].name = strdup("id");
78  info[1].name = strdup("x");
79  info[2].name = strdup("y");
80 
81  info[0].eType = ANY_INTEGER;
82  info[0].strict = false;
83 
84 
85  void *SPIplan;
86  SPIplan = pgr_SPI_prepare(sql);
87 
88  Portal SPIportal;
89  SPIportal = pgr_SPI_cursor_open(SPIplan);
90 
91 
92  bool moredata = TRUE;
93  (*total_coordinates) = total_tuples;
94 
95  int64_t default_id = 1;
96 
97  while (moredata == TRUE) {
98  SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
99  if (total_tuples == 0)
100  pgr_fetch_column_info(info, 3);
101 
102  ntuples = SPI_processed;
103  total_tuples += ntuples;
104 
105  if (ntuples > 0) {
106  if ((*coordinates) == NULL)
107  (*coordinates) = (Coordinate_t *)
108  palloc0(total_tuples * sizeof(Coordinate_t));
109  else
110  (*coordinates) = (Coordinate_t *)
111  repalloc((*coordinates),
112  total_tuples * sizeof(Coordinate_t));
113 
114  if ((*coordinates) == NULL) {
115  elog(ERROR, "Out of memory");
116  }
117 
118  SPITupleTable *tuptable = SPI_tuptable;
119  TupleDesc tupdesc = SPI_tuptable->tupdesc;
120  PGR_DBG("Processing %ld coordinates tupĺes", ntuples);
121 
122  size_t t;
123  for (t = 0; t < ntuples; t++) {
124  HeapTuple tuple = tuptable->vals[t];
125  pgr_fetch_row(&tuple, &tupdesc, info,
126  &default_id,
127  &(*coordinates)[total_tuples - ntuples + t]);
128  }
129  SPI_freetuptable(tuptable);
130  } else {
131  moredata = FALSE;
132  }
133  }
134 
135  SPI_cursor_close(SPIportal);
136 
137 
138  if (total_tuples == 0) {
139  (*total_coordinates) = 0;
140  PGR_DBG("NO coordinates");
141  return;
142  }
143 
144  (*total_coordinates) = total_tuples;
145  time_msg(" reading coordinates:", start_t, clock());
146 }
static void pgr_fetch_row(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info[3], int64_t *default_id, Coordinate_t *distance)
int64_t pgr_SPI_getBigInt(HeapTuple *tuple, TupleDesc *tupdesc, Column_info_t info)
double y
Definition: pgr_types.h:59
#define PGR_DBG(...)
Definition: debug_macro.h:34
uint64_t type
Definition: pgr_types.h:200
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
int64_t id
Definition: pgr_types.h:57
char * name
Definition: pgr_types.h:202
void pgr_fetch_column_info(Column_info_t info[], int info_size)
void pgr_get_coordinates(char *sql, Coordinate_t **coordinates, size_t *total_coordinates)
bigint id, float x, float y,
double x
Definition: pgr_types.h:58
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