pgRouting
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 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 // #define DEBUG
26 #include "./coordinates_input.h"
27 
28 #include "./debug_macro.h"
29 #include "./pgr_types.h"
30 #include "./postgres_connection.h"
31 #include "./get_check_data.h"
32 #include "./time_msg.h"
33 
34 
35 
36 static
38  HeapTuple *tuple,
39  TupleDesc *tupdesc,
40  Column_info_t info[3],
41  int64_t *default_id,
42  Coordinate_t *distance) {
43  if (column_found(info[0].colNumber)) {
44  distance->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
45  } else {
46  distance->id = *default_id;
47  ++(*default_id);
48  }
49  distance->x = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
50  distance->y = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
51 }
52 
59  char *sql,
60  Coordinate_t **coordinates,
61  size_t *total_coordinates) {
62  clock_t start_t = clock();
63 
64  const int tuple_limit = 1000000;
65 
66  size_t ntuples;
67  size_t total_tuples = 0;
68 
69  Column_info_t info[3];
70 
71  int i;
72  for (i = 0; i < 3; ++i) {
73  info[i].colNumber = -1;
74  info[i].type = 0;
75  info[i].strict = true;
76  info[i].eType = ANY_NUMERICAL;
77  }
78  info[0].name = strdup("id");
79  info[1].name = strdup("x");
80  info[2].name = strdup("y");
81 
82  info[0].eType = ANY_INTEGER;
83  info[0].strict = false;
84 
85 
86  void *SPIplan;
87  SPIplan = pgr_SPI_prepare(sql);
88 
89  Portal SPIportal;
90  SPIportal = pgr_SPI_cursor_open(SPIplan);
91 
92 
93  bool moredata = TRUE;
94  (*total_coordinates) = total_tuples;
95 
96  int64_t default_id = 1;
97 
98  while (moredata == TRUE) {
99  SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
100  if (total_tuples == 0)
101  pgr_fetch_column_info(info, 3);
102 
103  ntuples = SPI_processed;
104  total_tuples += ntuples;
105 
106  if (ntuples > 0) {
107  if ((*coordinates) == NULL)
108  (*coordinates) = (Coordinate_t *)
109  palloc0(total_tuples * sizeof(Coordinate_t));
110  else
111  (*coordinates) = (Coordinate_t *)
112  repalloc((*coordinates),
113  total_tuples * sizeof(Coordinate_t));
114 
115  if ((*coordinates) == NULL) {
116  elog(ERROR, "Out of memory");
117  }
118 
119  SPITupleTable *tuptable = SPI_tuptable;
120  TupleDesc tupdesc = SPI_tuptable->tupdesc;
121  PGR_DBG("Processing %ld coordinates tupĺes", ntuples);
122 
123  size_t t;
124  for (t = 0; t < ntuples; t++) {
125  HeapTuple tuple = tuptable->vals[t];
126  pgr_fetch_row(&tuple, &tupdesc, info,
127  &default_id,
128  &(*coordinates)[total_tuples - ntuples + t]);
129  }
130  SPI_freetuptable(tuptable);
131  } else {
132  moredata = FALSE;
133  }
134  }
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 }
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:45
#define PGR_DBG(...)
Definition: debug_macro.h:33
uint64_t type
Definition: pgr_types.h:186
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:31
int64_t id
Definition: pgr_types.h:43
char * name
Definition: pgr_types.h:188
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:44
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:189