pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
one_to_one_withPoints_driver.cpp
1 /*PGR-GNU*****************************************************************
2 File: withPoints_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
6 Mail: project@pgrouting.org
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
10 Mail:
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28 ********************************************************************PGR-GNU*/
29 
30 
31 #ifdef __MINGW32__
32 #include <winsock2.h>
33 #include <windows.h>
34 #endif
35 
36 
37 #include <sstream>
38 #include <deque>
39 #include <vector>
40 #include "./pgr_dijkstra.hpp"
41 #include "./pgr_withPoints.hpp"
42 #include "./one_to_one_withPoints_driver.h"
43 
44 // #define DEBUG
45 
46 
47 #include "./../../common/src/memory_func.hpp"
48 
49 
50 // CREATE OR REPLACE FUNCTION pgr_withPoint(
51 // edges_sql TEXT,
52 // points_sql TEXT,
53 // start_pid BIGINT,
54 // end_pid BIGINT,
55 // directed BOOLEAN DEFAULT true
56 
57 
58 int
59 do_pgr_withPoints(
60  pgr_edge_t *edges, size_t total_edges,
61  Point_on_edge_t *points_p, size_t total_points,
62  pgr_edge_t *edges_of_points, size_t total_edges_of_points,
63  int64_t start_pid,
64  int64_t end_pid,
65  bool directed,
66  char driving_side,
67  bool details,
68  bool only_cost,
69  General_path_element_t **return_tuples,
70  size_t *return_count,
71  char ** err_msg){
72  std::ostringstream log;
73  try {
74  log << "Entering do_pgr_withPoints\n";
75  std::vector< Point_on_edge_t >
76  points(points_p, points_p + total_points);
77 
78  log << "total points" << points.size() << "\n";
79 
80  /*
81  * This test is easier in C++
82  */
83  int errcode = check_points(points, log);
84  if (errcode) {
85  log << "Point(s) with same pid but different edge/fraction/side combination found";
86  *err_msg = strdup(log.str().c_str());
87  return errcode;
88  }
89 
90  std::vector< pgr_edge_t >
91  edges_to_modify(edges_of_points, edges_of_points + total_edges_of_points);
92 
93  std::vector< pgr_edge_t > new_edges;
94  log << "start_pid" << start_pid << "\n";
95  log << "end_pid" << end_pid << "\n";
96  log << "driving_side" << driving_side << "\n";
97  create_new_edges(
98  points,
99  edges_to_modify,
100  driving_side,
101  new_edges,
102  log);
103 
104  int64_t start_vid(start_pid);
105  int64_t end_vid(end_pid);
106 #if 0
107  for (const auto point : points) {
108  if (point.pid == start_pid) {
109  start_vid = point.vertex_id;
110  }
111  if (point.pid == end_pid) {
112  end_vid = point.vertex_id;
113  }
114 
115  }
116 #endif
117  log << "start_vid" << start_vid << "\n";
118  log << "end_vid" << end_vid << "\n";
119  graphType gType = directed? DIRECTED: UNDIRECTED;
120  const auto initial_size = total_edges;
121 
122  Path path;
123 
124 
125  if (directed) {
126  log << "Working with directed Graph\n";
127  Pgr_base_graph< DirectedGraph > digraph(gType, initial_size);
128  digraph.graph_insert_data(edges, total_edges);
129  digraph.graph_insert_data(new_edges);
130  pgr_dijkstra(digraph, path, start_vid, end_vid, only_cost);
131  } else {
132  log << "Working with Undirected Graph\n";
133  Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size);
134  undigraph.graph_insert_data(edges, total_edges);
135  undigraph.graph_insert_data(new_edges);
136  pgr_dijkstra(undigraph, path, start_vid, end_vid, only_cost);
137  }
138 
139 
140  size_t count(path.size());
141  if (count == 0) {
142  (*return_tuples) = NULL;
143  (*return_count) = 0;
144  log <<
145  "No paths found between Starting and any of the Ending vertices\n";
146  *err_msg = strdup(log.str().c_str());
147  return 0;
148  }
149 
150  if (!details) {
151  eliminate_details(path, edges_to_modify);
152  }
153 
154  (*return_tuples) = get_memory(count, (*return_tuples));
155  size_t sequence = 0;
156  path.generate_postgres_data(return_tuples, sequence);
157  (*return_count) = sequence;
158 
159 #ifndef DEBUG
160  {
161  std::ostringstream log;
162  log << "OK";
163  *err_msg = strdup(log.str().c_str());
164  }
165 
166 #else
167  *err_msg = strdup(log.str().c_str());
168 #endif
169  return 0;
170  } catch ( ... ) {
171  log << "Caught unknown expection!\n";
172  *err_msg = strdup(log.str().c_str());
173  }
174  return 1000;
175 }
Definition: tsp.h:34