pgRouting  2.2
pgRouting extends the PostGIS / PostgreSQL geospatial database to provide geospatial routing functionality.
 All Classes Functions Variables Pages
one_to_many_withPoints_driver.cpp
1 /*PGR-GNU*****************************************************************
2 File: one_to_many_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 <cassert>
41 
42 // #define DEBUG
43 
44 #include "./pgr_dijkstra.hpp"
45 #include "./pgr_withPoints.hpp"
46 #include "./one_to_many_withPoints_driver.h"
47 
48 extern "C" {
49 #include "./../../common/src/pgr_types.h"
50 }
51 
52 #include "./../../common/src/memory_func.hpp"
53 
54 
55 // CREATE OR REPLACE FUNCTION pgr_withPoint(
56 // edges_sql TEXT,
57 // points_sql TEXT,
58 // start_pid BIGINT,
59 // end_pid BIGINT,
60 // directed BOOLEAN DEFAULT true
61 
62 
63 int
64 do_pgr_one_to_many_withPoints(
65  pgr_edge_t *edges, size_t total_edges,
66  Point_on_edge_t *points_p, size_t total_points,
67  pgr_edge_t *edges_of_points, size_t total_edges_of_points,
68  int64_t start_vid,
69  int64_t *end_pidsArr, size_t size_end_pidsArr,
70  char driving_side,
71  bool details,
72  bool directed,
73  bool only_cost,
74  General_path_element_t **return_tuples, size_t *return_count,
75  char ** err_msg){
76  std::ostringstream log;
77  try {
78  /*
79  * DOCUMENT:
80  * - Points are treated as the same point when the pid is the same
81  * therefore when two points have the same pid, but different edge/fraction
82  * an error is generated.
83  */
84  std::vector< Point_on_edge_t >
85  points(points_p, points_p + total_points);
86 
87  int errcode = check_points(points, log);
88  if (errcode) {
89  log << "Point(s) with same pid but different edge/fraction/side combination found";
90  *err_msg = strdup(log.str().c_str());
91  return errcode;
92  }
93 
94 
95 
96 
97  std::vector< pgr_edge_t >
98  edges_to_modify(edges_of_points, edges_of_points + total_edges_of_points);
99 
100  std::vector< pgr_edge_t > new_edges;
101 
102  log << "driving_side" << driving_side << "\n";
103  create_new_edges(
104  points,
105  edges_to_modify,
106  driving_side,
107  new_edges);
108 
109  log << "Inserting points into a c++ vector structure\n";
110  /*
111  * Eliminating duplicates
112  * & ordering the points
113  */
114  std::set< int64_t > s_end_vertices(end_pidsArr, end_pidsArr + size_end_pidsArr);
115 
116  std::vector< int64_t > end_vertices(s_end_vertices.begin(), s_end_vertices.end());
117 
118  log << "start_vid" << start_vid << "\n";
119  log << "end_vertices";
120 
121  for (const auto &vid : end_vertices) {
122  log << vid << "\n";
123  }
124 
125  graphType gType = directed? DIRECTED: UNDIRECTED;
126  const auto initial_size = total_edges;
127 
128  std::deque< Path > paths;
129 
130 
131  if (directed) {
132  log << "Working with directed Graph\n";
133  Pgr_base_graph< DirectedGraph > digraph(gType, initial_size);
134  digraph.graph_insert_data(edges, total_edges);
135  digraph.graph_insert_data(new_edges);
136  pgr_dijkstra(digraph, paths, start_vid, end_vertices, only_cost);
137  } else {
138  log << "Working with Undirected Graph\n";
139  Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size);
140  undigraph.graph_insert_data(edges, total_edges);
141  undigraph.graph_insert_data(new_edges);
142  pgr_dijkstra(undigraph, paths, start_vid, end_vertices, only_cost);
143  }
144 
145 #if 0
146  for (auto &path :paths) {
147  adjust_pids(points, path);
148  }
149 #endif
150  if (!details) {
151  for (auto &path :paths) {
152  eliminate_details(path, edges_to_modify);
153  }
154  }
155  /*
156  * order paths based on the end_pid
157  */
158  std::sort(paths.begin(), paths.end(), [](const Path &a,const Path &b) {
159  return a.end_id() < b.end_id();
160  });
161 
162  size_t count(0);
163  count = count_tuples(paths);
164 
165 
166  if (count == 0) {
167  (*return_tuples) = NULL;
168  (*return_count) = 0;
169  log <<
170  "No paths found between Starting and any of the Ending vertices\n";
171  *err_msg = strdup(log.str().c_str());
172  return 0;
173  }
174 
175  (*return_tuples) = get_memory(count, (*return_tuples));
176  log << "Converting a set of paths into the tuples\n";
177  (*return_count) = (collapse_paths(return_tuples, paths));
178 
179 
180 #ifndef DEBUG
181  {
182  std::ostringstream log;
183  log << "OK";
184  *err_msg = strdup(log.str().c_str());
185  }
186 
187 #else
188  *err_msg = strdup(log.str().c_str());
189 #endif
190  return 0;
191  } catch ( ... ) {
192  log << "Caught unknown expection!\n";
193  *err_msg = strdup(log.str().c_str());
194  return 1000;
195  }
196  return 0;
197 }
198