pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
restrictions_input.c
1 /*PGR-GNU*****************************************************************
2 File: restrictions_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.h"
26 #include "executor/spi.h"
27 
28 #include "./debug_macro.h"
29 #include "./pgr_types.h"
30 #include "./time_msg.h"
31 #include "./postgres_connection.h"
32 #include "./get_check_data.h"
33 #include "./restrictions_input.h"
34 
35 
36 static
37 void fetch_restriction(
38  HeapTuple *tuple,
39  TupleDesc *tupdesc,
40  Column_info_t info[4],
41  Restrict_t *restriction) {
42  restriction->target_id = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
43  restriction->to_cost = pgr_SPI_getFloat8(tuple, tupdesc, info[1]);
44  char *str = DatumGetCString(SPI_getvalue(*tuple, *tupdesc, info[2].colNumber));
45 // TODO because its text, there is no garanee that the text read is correct
46 // move this code to c++ to tokenize the integers.
47 
48  int i = 0;
49  for(i = 0; i < MAX_RULE_LENGTH; ++i) restriction->via[i] = -1;
50 
51  if (str != NULL) {
52  char *token = NULL;
53  int i = 0;
54 
55  token = (char *)strtok(str," ,");
56 
57  while (token != NULL && i < MAX_RULE_LENGTH)
58  {
59  restriction->via[i] = atoi(token);
60  i++;
61  token = (char *)strtok(NULL, " ,");
62  }
63  }
64 }
65 
66 
67 void
68 pgr_get_restriction_data(
69  char *restrictions_sql,
70  Restrict_t **restrictions,
71  size_t *total_restrictions) {
72  const int tuple_limit = 1000000;
73  clock_t start_t = clock();
74 
75  PGR_DBG("pgr_get_restriction_data");
76  PGR_DBG("%s", restrictions_sql);
77 
78  Column_info_t info[3];
79 
80  int i;
81  for (i = 0; i < 3; ++i) {
82  info[i].colNumber = -1;
83  info[i].type = 0;
84  info[i].strict = true;
85  info[i].eType = ANY_INTEGER;
86  }
87  info[0].name = strdup("target_id");
88  info[1].name = strdup("to_cost");
89  info[2].name = strdup("via_path");
90 
91  info[1].eType = ANY_NUMERICAL;
92  info[2].eType = TEXT;
93 
94 
95  size_t ntuples;
96  size_t total_tuples;
97 
98  void *SPIplan;
99  SPIplan = pgr_SPI_prepare(restrictions_sql);
100  Portal SPIportal;
101  SPIportal = pgr_SPI_cursor_open(SPIplan);
102 
103  bool moredata = TRUE;
104  (*total_restrictions) = total_tuples = 0;
105 
106  /* on the first tuple get the column numbers */
107 
108  while (moredata == TRUE) {
109  SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
110  if (total_tuples == 0) {
111  pgr_fetch_column_info(info, 3);
112  }
113  ntuples = SPI_processed;
114  total_tuples += ntuples;
115  PGR_DBG("SPI_processed %ld", ntuples);
116  if (ntuples > 0) {
117  if ((*restrictions) == NULL)
118  (*restrictions) = (Restrict_t *)palloc0(total_tuples * sizeof(Restrict_t));
119  else
120  (*restrictions) = (Restrict_t *)repalloc((*restrictions), total_tuples * sizeof(Restrict_t));
121 
122  if ((*restrictions) == NULL) {
123  elog(ERROR, "Out of memory");
124  }
125 
126  size_t t;
127  SPITupleTable *tuptable = SPI_tuptable;
128  TupleDesc tupdesc = SPI_tuptable->tupdesc;
129  PGR_DBG("processing %ld", ntuples);
130  for (t = 0; t < ntuples; t++) {
131  HeapTuple tuple = tuptable->vals[t];
132  fetch_restriction(&tuple, &tupdesc, info,
133  &(*restrictions)[total_tuples - ntuples + t]);
134  }
135  SPI_freetuptable(tuptable);
136  } else {
137  moredata = FALSE;
138  }
139  }
140 
141  if (total_tuples == 0) {
142  (*total_restrictions) = 0;
143  PGR_DBG("NO restrictions");
144  return;
145  }
146 
147  (*total_restrictions) = total_tuples;
148  PGR_DBG("Finish reading %ld data, %ld", total_tuples, (*total_restrictions));
149  clock_t end_t = clock();
150  time_msg(" reading Restrictions", start_t, end_t);
151 }