pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
customers_input.c
1 /*PGR-GNU*****************************************************************
2 File: customers_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 "./../../common/src/debug_macro.h"
27 #include "./../../common/src/pgr_types.h"
28 #include "./../../common/src/postgres_connection.h"
29 #include "./../../common/src/get_check_data.h"
30 #include "./customers_input.h"
31 
32 
33 
34 
35 static
36 void pgr_fetch_customer(
37  HeapTuple *tuple,
38  TupleDesc *tupdesc,
39  Column_info_t info[9],
40  Customer *customer) {
41 
42  customer->id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
43  customer->x = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
44  customer->y = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
45  customer->demand = pgr_SPI_getFloat8(tuple, tupdesc, info[3]);
46  customer->Etime = pgr_SPI_getFloat8(tuple, tupdesc, info[4]);
47  customer->Ltime = pgr_SPI_getFloat8(tuple, tupdesc, info[5]);
48  customer->Stime = pgr_SPI_getFloat8(tuple, tupdesc, info[6]);
49  customer->Pindex = pgr_SPI_getBigInt(tuple, tupdesc, info[7]);
50  customer->Dindex = pgr_SPI_getBigInt(tuple, tupdesc, info[8]);
51  customer->Ddist = 0;
52 }
53 
54 void
55 pgr_get_customers(
56  char *sql,
57  Customer **customers,
58  size_t *total_customers) {
59  const int tuple_limit = 1000;
60 
61  size_t ntuples;
62  size_t total_tuples = 0;
63 
64  Column_info_t info[9];
65 
66  int i;
67  for (i = 0; i < 9; ++i) {
68  info[i].colNumber = -1;
69  info[i].type = -1;
70  info[i].strict = true;
71  info[i].eType = ANY_NUMERICAL;
72  }
85  info[0].name = strdup("id");
86  info[1].name = strdup("x");
87  info[2].name = strdup("y");
88  info[3].name = strdup("demand");
89  info[4].name = strdup("etime");
90  info[5].name = strdup("ltime");
91  info[6].name = strdup("stime");
92  info[7].name = strdup("pindex");
93  info[8].name = strdup("dindex");
94 
95  info[0].eType = ANY_INTEGER;
96  info[7].eType = ANY_INTEGER;
97  info[8].eType = ANY_INTEGER;
98 
99 
100  void *SPIplan;
101  SPIplan = pgr_SPI_prepare(sql);
102 
103  Portal SPIportal;
104  SPIportal = pgr_SPI_cursor_open(SPIplan);
105 
106 
107  bool moredata = TRUE;
108  (*total_customers) = total_tuples;
109 
110 
111  while (moredata == TRUE) {
112  SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
113  if (total_tuples == 0)
114  pgr_fetch_column_info(info, 9);
115 
116  ntuples = SPI_processed;
117  total_tuples += ntuples;
118 
119  if (ntuples > 0) {
120  if ((*customers) == NULL)
121  (*customers) = (Customer *)palloc0(total_tuples * sizeof(Customer));
122  else
123  (*customers) = (Customer *)repalloc((*customers), total_tuples * sizeof(Customer));
124 
125  if ((*customers) == NULL) {
126  elog(ERROR, "Out of memory");
127  }
128 
129  int t;
130  SPITupleTable *tuptable = SPI_tuptable;
131  TupleDesc tupdesc = SPI_tuptable->tupdesc;
132  PGR_DBG("processing %d customer tupĺes", ntuples);
133 
134  for (t = 0; t < ntuples; t++) {
135  HeapTuple tuple = tuptable->vals[t];
136  pgr_fetch_customer(&tuple, &tupdesc, info,
137  &(*customers)[total_tuples - ntuples + t]);
138  }
139  SPI_freetuptable(tuptable);
140  } else {
141  moredata = FALSE;
142  }
143  }
144 
145  if (total_tuples == 0) {
146  (*total_customers) = 0;
147  PGR_DBG("NO _customers");
148  return;
149  }
150 
151 
152  (*total_customers) = total_tuples;
153  PGR_DBG("Finish reading %ld customers, %ld", total_tuples, (*total_customers));
154 }
155