pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
points_input.c
1 /*PGR-GNU*****************************************************************
2 File: points_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 "./debug_macro.h"
27 #include "pgr_types.h"
28 #include "postgres_connection.h"
29 #include "get_check_data.h"
30 #include "points_input.h"
31 
32 
33 static
34 void fetch_point(
35  HeapTuple *tuple,
36  TupleDesc *tupdesc,
37  Column_info_t info[4],
38  int64_t *default_pid,
39  char default_side,
41  if (column_found(info[0].colNumber)) {
42  point->pid = pgr_SPI_getBigInt(tuple, tupdesc, info[0]);
43  } else {
44  point->pid = *default_pid;
45  ++(*default_pid);
46  }
47 
48  point->edge_id = pgr_SPI_getBigInt(tuple, tupdesc, info[1]);
49  point->fraction = pgr_SPI_getFloat8(tuple, tupdesc, info[2]);
50 
51  if (column_found(info[3].colNumber)) {
52  point->side =
53  (char)pgr_SPI_getChar(tuple, tupdesc, info[3], false, default_side);
54  } else {
55  point->side = default_side;
56  }
57 }
58 
59 
60 // pid, edge_id, fraction, [side]
61 void
62 pgr_get_points(
63  char *points_sql,
64  Point_on_edge_t **points,
65  size_t *total_points) {
66  const int tuple_limit = 1000;
67 
68  size_t ntuples;
69  size_t total_tuples;
70  Column_info_t info[4];
71 
72  int i;
73  for (i = 0; i < 4; ++i) {
74  info[i].colNumber = -1;
75  info[i].type = 0;
76  info[i].strict = true;
77  info[i].eType = ANY_INTEGER;
78  }
79 
80  info[0].name = strdup("pid");
81  info[1].name = strdup("edge_id");
82  info[2].name = strdup("fraction");
83  info[3].name = strdup("side");
84 
85  info[0].strict = false;
86  info[3].strict = false;
87  info[2].eType = ANY_NUMERICAL;
88  info[3].eType = CHAR1;
89 
90 
91  void *SPIplan;
92  SPIplan = pgr_SPI_prepare(points_sql);
93 
94  Portal SPIportal;
95  SPIportal = pgr_SPI_cursor_open(SPIplan);
96 
97  bool moredata = TRUE;
98  (*total_points) = total_tuples = 0;
99 
100  int64_t default_pid = 0;
101  char default_side = 'b';
102 
103  while (moredata == TRUE) {
104  SPI_cursor_fetch(SPIportal, TRUE, tuple_limit);
105  if (total_tuples == 0) {
106  /* on the first tuple get the column information */
107  pgr_fetch_column_info(info, 4);
108  }
109 
110  ntuples = SPI_processed;
111  total_tuples += ntuples;
112 
113  if (ntuples > 0) {
114  if ((*points) == NULL)
115  (*points) = (Point_on_edge_t *)palloc0(total_tuples * sizeof(Point_on_edge_t));
116  else
117  (*points) = (Point_on_edge_t *)repalloc((*points), total_tuples * sizeof(Point_on_edge_t));
118 
119  if ((*points) == NULL) {
120  elog(ERROR, "Out of memory");
121  }
122 
123  SPITupleTable *tuptable = SPI_tuptable;
124  TupleDesc tupdesc = SPI_tuptable->tupdesc;
125  size_t t;
126 
127  PGR_DBG("processing %ld points tuples", ntuples);
128  for (t = 0; t < ntuples; t++) {
129  HeapTuple tuple = tuptable->vals[t];
130  fetch_point(&tuple, &tupdesc, info,
131  &default_pid, default_side,
132  &(*points)[total_tuples - ntuples + t]);
133  }
134  SPI_freetuptable(tuptable);
135  } else {
136  moredata = FALSE;
137  }
138  }
139  SPI_cursor_close(SPIportal);
140 
141 
142  if (total_tuples == 0) {
143  (*total_points) = 0;
144  PGR_DBG("NO points");
145  return;
146  }
147 
148  (*total_points) = total_tuples;
149  PGR_DBG("Finish reading %ld points, %ld", total_tuples, (*total_points));
150 }
Definition: tsp.h:34