void do_pgr_one_to_one_dijkstra( pgr_edge_t *data_edges, size_t total_tuples, int64_t start_vid, int64_t end_vid, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char ** err_msg) { std::ostringstream log; try { graphType gType = directed? DIRECTED: UNDIRECTED; Path path; if (directed) { log << "Working with directed Graph\n"; pgrouting::DirectedGraph digraph(gType); digraph.graph_insert_data(data_edges, total_tuples); pgr_dijkstra(digraph, path, start_vid, end_vid, only_cost); } else { log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); undigraph.graph_insert_data(data_edges, total_tuples); pgr_dijkstra(undigraph, path, start_vid, end_vid, only_cost); } size_t count(0); count = path.size(); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; log << "No paths found between Starting and any of the Ending vertices\n"; *err_msg = strdup(log.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); size_t sequence = 0; path.generate_postgres_data(return_tuples, sequence); (*return_count) = sequence; #ifndef DEBUG *err_msg = strdup("OK"); #else *err_msg = strdup(log.str().c_str()); #endif return; } catch ( ... ) { log << "Caught unknown exception!\n"; *err_msg = strdup(log.str().c_str()); return; } }
// CREATE OR REPLACE FUNCTION pgr_johnson(edges_sql TEXT, directed BOOLEAN, void do_pgr_johnson( pgr_edge_t *data_edges, size_t total_tuples, bool directed, Matrix_cell_t **return_tuples, size_t *return_count, char **err_msg) { std::ostringstream log; try { if (total_tuples == 1) { log << "Requiered: more than one tuple\n"; (*return_tuples) = NULL; (*return_count) = 0; *err_msg = strdup(log.str().c_str()); return; } graphType gType = directed? DIRECTED: UNDIRECTED; const auto initial_size = total_tuples; std::deque< Path >paths; if (directed) { log << "Working with directed Graph\n"; Pgr_base_graph< DirectedGraph > digraph(gType, initial_size); digraph.graph_insert_data(data_edges, total_tuples); pgr_johnson(digraph, *return_count, return_tuples); } else { log << "Working with Undirected Graph\n"; Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size); undigraph.graph_insert_data(data_edges, total_tuples); pgr_johnson(undigraph, *return_count, return_tuples); } if (*return_count == 0) { log << "NOTICE: No Vertices found??? wiered error\n"; *err_msg = strdup(log.str().c_str()); (*return_tuples) = NULL; (*return_count) = 0; return; } #ifndef DEBUG *err_msg = strdup("OK"); #else *err_msg = strdup(log.str().c_str()); #endif } catch ( ... ) { log << "Caught unknown expection!\n"; *err_msg = strdup(log.str().c_str()); } }
void do_pgr_driving_many_to_dist( pgr_edge_t *data_edges, size_t total_tuples, int64_t *start_vertex, size_t s_len, float8 distance, bool directedFlag, bool equiCostFlag, General_path_element_t **ret_path, size_t *path_count, char ** err_msg) { try { graphType gType = directedFlag? DIRECTED: UNDIRECTED; const auto initial_size = total_tuples; std::deque< Path >paths; std::set< int64_t > s_start_vertices(start_vertex, start_vertex + s_len); std::vector< int64_t > start_vertices(s_start_vertices.begin(), s_start_vertices.end()); if (directedFlag) { Pgr_base_graph< DirectedGraph > digraph(gType, initial_size); digraph.graph_insert_data(data_edges, total_tuples); pgr_drivingDistance(digraph, paths, start_vertices, distance, equiCostFlag); } else { Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size); undigraph.graph_insert_data(data_edges, total_tuples); pgr_drivingDistance(undigraph, paths, start_vertices, distance, equiCostFlag); } size_t count(count_tuples(paths)); if (count == 0) { *err_msg = strdup("NOTICE: No return values was found"); *ret_path = noResult(path_count, (*ret_path)); return; } *ret_path = get_memory(count, (*ret_path)); auto trueCount(collapse_paths(ret_path, paths)); *path_count = trueCount; #ifndef DEBUG *err_msg = strdup("OK"); #else *err_msg = strdup(log.str().c_str()); #endif return; } catch ( ... ) { *err_msg = strdup("Caught unknown expection!"); *ret_path = noResult(path_count, (*ret_path)); return; } }
void do_pgr_connectedComponents( pgr_edge_t *data_edges, size_t total_edges, pgr_components_rt **return_tuples, size_t *return_count, char ** log_msg, char ** notice_msg, char ** err_msg) { std::ostringstream log; std::ostringstream err; std::ostringstream notice; try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); pgassert(total_edges != 0); graphType gType = UNDIRECTED; std::vector<pgr_components_rt> results; log << "Working with Undirected Graph\n"; pgrouting::ComponentsUndiGraph undigraph(gType); undigraph.insert_edges(data_edges, total_edges); results = pgr_connectedComponents( undigraph); auto count = results.size(); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; notice << "No paths found between start_vid and end_vid vertices"; return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); for (size_t i = 0; i < count; i++) { *((*return_tuples) + i) = results[i]; } (*return_count) = count; pgassert(*err_msg == NULL); *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); *notice_msg = notice.str().empty()? *notice_msg : pgr_msg(notice.str().c_str()); } catch (AssertFailedException &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << "Caught unknown exception!"; *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } }
int do_pgr_ksp( pgr_edge_t *data_edges, size_t total_tuples, int64_t start_vertex, int64_t end_vertex, int k, bool directedFlag, bool heap_paths, General_path_element_t **ksp_path, size_t *path_count, char ** err_msg) { try { std::ostringstream log; graphType gType = directedFlag? DIRECTED: UNDIRECTED; const auto initial_size = total_tuples; std::deque< Path > paths; if (directedFlag) { Pgr_base_graph< DirectedGraph > digraph(gType, initial_size); Pgr_ksp< Pgr_base_graph< DirectedGraph > > fn_yen; digraph.graph_insert_data(data_edges, initial_size); paths = fn_yen.Yen(digraph, start_vertex, end_vertex, k, heap_paths); } else { Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size); Pgr_ksp< Pgr_base_graph< UndirectedGraph > > fn_yen; undigraph.graph_insert_data(data_edges, initial_size); paths = fn_yen.Yen(undigraph, start_vertex, end_vertex, k, heap_paths); } auto count(count_tuples(paths)); if (count == 0) { *err_msg = strdup( "NOTICE: No paths found between Starting and Ending vertices"); *ksp_path = noResult(path_count, (*ksp_path)); return 0; } // get the space required to store all the paths *ksp_path = NULL; *ksp_path = get_memory(count, (*ksp_path)); size_t sequence = 0; int route_id = 0; for (const auto &path : paths) { if (path.size() > 0) path.get_pg_ksp_path(ksp_path, sequence, route_id); ++route_id; } if (count != sequence) { *err_msg = NULL; return 2; } *path_count = count; #if 1 *err_msg = strdup("OK"); #else *err_msg = strdup(log.str().c_str()); #endif return EXIT_SUCCESS; } catch ( ... ) { *err_msg = strdup("Caught unknown expection!"); return -1; } }
void do_pgr_one_to_many_withPoints( pgr_edge_t *edges, size_t total_edges, Point_on_edge_t *points_p, size_t total_points, pgr_edge_t *edges_of_points, size_t total_edges_of_points, int64_t start_vid, int64_t *end_pidsArr, size_t size_end_pidsArr, char driving_side, bool details, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char ** log_msg, char ** err_msg) { std::ostringstream log; std::ostringstream err; try { pgassert(!(*return_tuples)); pgassert((*return_count) == 0); pgassert(!(*log_msg)); pgassert(!(*err_msg)); /* * DOCUMENT: * - Points are treated as the same point when the pid is the same * therefore when two points have the same pid, but different edge/fraction * an error is generated. */ std::vector< Point_on_edge_t > points(points_p, points_p + total_points); int errcode = check_points(points, log); if (errcode) { *log_msg = strdup(log.str().c_str()); err << "Unexpected point(s) with same pid but different edge/fraction/side combination found."; *err_msg = strdup(err.str().c_str()); return; } std::vector< pgr_edge_t > edges_to_modify(edges_of_points, edges_of_points + total_edges_of_points); std::vector< pgr_edge_t > new_edges; log << "driving_side" << driving_side << "\n"; create_new_edges( points, edges_to_modify, driving_side, new_edges); log << "Inserting points into a c++ vector structure\n"; /* * Eliminating duplicates * & ordering the points */ std::set< int64_t > s_end_vertices(end_pidsArr, end_pidsArr + size_end_pidsArr); std::vector< int64_t > end_vertices(s_end_vertices.begin(), s_end_vertices.end()); log << "start_vid" << start_vid << "\n"; log << "end_vertices"; for (const auto &vid : end_vertices) { log << vid << "\n"; } graphType gType = directed? DIRECTED: UNDIRECTED; std::deque< Path > paths; if (directed) { log << "Working with directed Graph\n"; pgrouting::DirectedGraph digraph( pgrouting::extract_vertices( pgrouting::extract_vertices(edges, total_edges), new_edges), gType); digraph.graph_insert_data(edges, total_edges); digraph.graph_insert_data(new_edges); pgr_dijkstra(digraph, paths, start_vid, end_vertices, only_cost); } else { log << "Working with Undirected Graph\n"; auto vertices(pgrouting::extract_vertices(edges, total_edges)); vertices = pgrouting::extract_vertices(vertices, new_edges); pgrouting::UndirectedGraph undigraph(vertices, gType); vertices.clear(); undigraph.graph_insert_data(edges, total_edges); undigraph.graph_insert_data(new_edges); pgr_dijkstra(undigraph, paths, start_vid, end_vertices, only_cost); } if (!details) { for (auto &path : paths) { eliminate_details(path, edges_to_modify); } } /* * order paths based on the end_pid */ std::sort(paths.begin(), paths.end(), [](const Path &a, const Path &b) { return a.end_id() < b.end_id(); }); size_t count(0); count = count_tuples(paths); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; log << "No paths found between Starting and any of the Ending vertices\n"; *log_msg = strdup(log.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); log << "Converting a set of paths into the tuples\n"; (*return_count) = (collapse_paths(return_tuples, paths)); *log_msg = strdup(log.str().c_str()); pgassert(!(*err_msg)); return; } catch (AssertFailedException &except) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; *log_msg = strdup(log.str().c_str()); err << except.what() << "\n"; *err_msg = strdup(err.str().c_str()); } catch (std::exception& except) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; *log_msg = strdup(log.str().c_str()); err << except.what() << "\n"; *err_msg = strdup(err.str().c_str()); } catch(...) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; *log_msg = strdup(log.str().c_str()); err << "Caught unknown exception!\n"; *err_msg = strdup(err.str().c_str()); } }
int main(int ac, char* av[]) { po::options_description od_desc("Allowed options"); get_options_description(od_desc); po::variables_map vm; po::store(po::parse_command_line(ac, av, od_desc), vm); if (vm.count("help")) { std::cout << od_desc << "\n"; return 0; } try{ po::notify(vm); } catch(...){ std::cout << od_desc << "\n"; return 0; } auto ret_val = process_command_line(vm, od_desc); if (ret_val != 2) return ret_val; auto db_dbase(vm["dbname"].as<std::string>()); db_dbase = "dbname = " + db_dbase; auto db_host(vm["host"].as<std::string>()); auto db_port(vm["port"].as<std::string>()); auto db_username(vm["username"].as<std::string>()); auto db_pwd(vm["password"].as<std::string>()); auto test(vm["test"].as<bool>()); //auto db_no_pwd(vm["no-password"].as<std::string>()); const char *conninfo = db_dbase.c_str(); PGconn *conn; PGresult *res; int rec_count, col_count; conn = PQconnectdb(conninfo); /* Check to see that the backend connection was successfully made */ if (PQstatus(conn) != CONNECTION_OK) { fprintf(stderr, "Connection to database failed: %s", PQerrorMessage(conn)); exit_nicely(conn); exit(0); } std::string data_sql; if (test) { data_sql = "select id, source, target, cost, -1 as reverse_cost from table1 order by id"; // data_sql = "select id, source, target, cost, reverse_cost from edge_table order by id"; } else { std::cout << "Input data query: "; std::getline (std::cin,data_sql); } std::cout << "\nThe data is from:" << data_sql <<"\n"; res = PQexec(conn, data_sql.c_str()); if (PQresultStatus(res) != PGRES_TUPLES_OK) { std::cout << "We did not get any data!\n"; exit_nicely(conn); exit(1); } rec_count = PQntuples(res); col_count = PQnfields(res); if (col_count > 5 || col_count < 4) { std::cout << "Max number of columns 5\n"; std::cout << "Min number of columns 4\n"; exit_nicely(conn); exit(1); } auto id_fnum = PQfnumber(res, "id"); auto source_fnum = PQfnumber(res, "source"); auto target_fnum = PQfnumber(res, "target"); auto cost_fnum = PQfnumber(res, "cost"); auto reverse_fnum = PQfnumber(res, "reverse_cost"); pgr_edge_t *data_edges; data_edges = (pgr_edge_t *) malloc(rec_count * sizeof(pgr_edge_t)); printf("We received %d records.\n", rec_count); puts("=========================="); std::string::size_type sz; std::string str; for (int row = 0; row < rec_count; ++row) { str = std::string(PQgetvalue(res, row, id_fnum)); data_edges[row].id = stol(str, &sz); str = std::string(PQgetvalue(res, row, source_fnum)); data_edges[row].source = stol(str, &sz); str = std::string(PQgetvalue(res, row, target_fnum)); data_edges[row].target = stol(str, &sz); str = std::string(PQgetvalue(res, row, cost_fnum)); data_edges[row].cost = stod(str, &sz); if (reverse_fnum != -1) { str = std::string(PQgetvalue(res, row, reverse_fnum)); data_edges[row].reverse_cost = stod(str, &sz); } #if 0 std::cout << "\tid: " << data_edges[row].id << "\t"; std::cout << "\tsource: " << data_edges[row].source << "\t"; std::cout << "\ttarget: " << data_edges[row].target << "\t"; std::cout << "\tcost: " << data_edges[row].cost << "\t"; if (reverse_fnum != -1) { std::cout << "\treverse: " << data_edges[row].reverse_cost << "\t"; } std::cout << "\n"; #endif } puts("=========================="); PQclear(res); PQfinish(conn); ////////////////////// END READING DATA FROM DATABASE /////////////////// std::string directed; std::cout << "Is the graph directed [N,n]? default[Y]"; std::getline(std::cin,directed); graphType gType = (directed.compare("N")==0 || directed.compare("n")==0)? UNDIRECTED: DIRECTED; bool directedFlag = (directed.compare("N")==0 || directed.compare("n")==0)? false: true; const int initial_size = rec_count; Pgr_base_graph< DirectedGraph > digraph(gType, initial_size); Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size); if (directedFlag) { process(digraph, data_edges, rec_count); } else { process(undigraph, data_edges, rec_count); } }
int do_pgr_driving_distance(pgr_edge_t *data_edges, int64_t total_tuples, int64_t start_vertex, float8 distance, bool directedFlag, pgr_path_element3_t **ret_path, int *path_count, char ** err_msg) { try { // in c code this should have been checked: // 1) start_vertex is in the data_edges DONE // 2) end_vertex is in the data_edges DONE // 3) start and end_vertex are different DONE std::ostringstream log; graphType gType = directedFlag? DIRECTED: UNDIRECTED; const int initial_size = 1; Path paths; typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, boost_vertex_t, boost_edge_t > UndirectedGraph; typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::bidirectionalS, boost_vertex_t, boost_edge_t > DirectedGraph; Pgr_dijkstra < DirectedGraph > digraph(gType, initial_size); Pgr_dijkstra < UndirectedGraph > undigraph(gType, initial_size); if (directedFlag) { digraph.initialize_graph(data_edges, total_tuples); digraph.dijkstra_dd(paths, start_vertex, distance); } else { undigraph.initialize_graph(data_edges, total_tuples); undigraph.dijkstra_dd(paths, start_vertex, distance); } if (paths.path.size() == 0) { *err_msg = strdup( "NOTICE: No driving distance node found"); *ret_path = noPathFound3(-1, path_count, (*ret_path)); return 0; } log << "NOTICE: Calculating the number of tuples \n"; int count = paths.path.size(); log << "NOTICE Count: " << count << " tuples\n"; // get the space required to store all the paths *ret_path = NULL; *ret_path = pgr_get_memory3(count, (*ret_path)); int sequence = 0; paths.ddPrint(ret_path, sequence, 0); *path_count = count; #if 1 *err_msg = strdup("OK"); #else *err_msg = strdup(log.str().c_str()); #endif return EXIT_SUCCESS; } catch ( ... ) { *err_msg = strdup("Caught unknown expection!"); return -1; } }
void do_pgr_bdAstar( Pgr_edge_xy_t *edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, int heuristic, double factor, double epsilon, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char ** log_msg, char ** notice_msg, char ** err_msg) { std::ostringstream log; std::ostringstream err; std::ostringstream notice; try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); pgassert(total_edges != 0); log << "Inserting vertices into a c++ vector structure"; std::vector<int64_t> start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr); std::vector< int64_t > end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr); graphType gType = directed? DIRECTED: UNDIRECTED; std::deque<Path> paths; log << "starting process\n"; if (directed) { log << "Working with directed Graph\n"; pgrouting::xyDirectedGraph digraph( pgrouting::extract_vertices(edges, total_edges), gType); digraph.insert_edges(edges, total_edges); paths = pgr_bdAstar(digraph, start_vertices, end_vertices, heuristic, factor, epsilon, log, only_cost); } else { log << "Working with Undirected Graph\n"; pgrouting::xyUndirectedGraph undigraph( pgrouting::extract_vertices(edges, total_edges), gType); undigraph.insert_edges(edges, total_edges); paths = pgr_bdAstar( undigraph, start_vertices, end_vertices, heuristic, factor, epsilon, log, only_cost); } size_t count(0); count = count_tuples(paths); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; notice << "No paths found"; *log_msg = pgr_msg(notice.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); log << "\nConverting a set of paths into the tuples"; (*return_count) = (collapse_paths(return_tuples, paths)); #if 0 auto count = path.size(); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; notice << "No paths found between start_vid and end_vid vertices"; } else { (*return_tuples) = pgr_alloc(count, (*return_tuples)); size_t sequence = 0; path.generate_postgres_data(return_tuples, sequence); (*return_count) = sequence; } #endif pgassert(*err_msg == NULL); *log_msg = log.str().empty()? nullptr : pgr_msg(log.str().c_str()); *notice_msg = notice.str().empty()? nullptr : pgr_msg(notice.str().c_str()); } catch (AssertFailedException &except) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch (std::exception& except) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch(...) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; err << "Caught unknown exception!"; *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } }
/************************************************************ MY_QUERY_LINE1 ***********************************************************/ void do_pgr_MY_FUNCTION_NAME( MY_EDGE_TYPE *data_edges, size_t total_edges, int64_t start_vid, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, MY_RETURN_VALUE_TYPE **return_tuples, size_t *return_count, char ** log_msg, char ** err_msg){ std::ostringstream err; std::ostringstream log; try { pgassert(!(*log_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); pgassert(total_edges != 0); /* depending on the functionality some tests can be done here * For example */ if (total_edges <= 1) { err << "Required: more than one edges\n"; (*return_tuples) = NULL; (*return_count) = 0; *err_msg = strdup(err.str().c_str()); return; } graphType gType = directed? DIRECTED: UNDIRECTED; std::deque< Path >paths; // samll logs log << "Inserting vertices into a c++ vector structure\n"; std::vector< int64_t > end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr); std::sort(end_vertices.begin(),end_vertices.end()); #ifndef NDEBUG // big logs with cycles wrap them so on release they wont consume time log << "end vids: "; for (const auto &vid : end_vertices) log << vid << ","; log << "\nstart vid:" << start_vid << "\n"; #endif if (directed) { // very detailed logging log << "Working with directed Graph\n"; pgRouting::DirectedGraph digraph(gType); log << "Working with directed Graph 1 \n"; digraph.graph_insert_data(data_edges, total_edges); #ifndef NDEBUG // a graph log is a big log log << digraph; #endif log << "Working with directed Graph 2\n"; pgr_dijkstra(digraph, paths, start_vid, end_vertices, false); log << "Working with directed Graph 3\n"; } else { // maybe the code is working so cleaner logging log << "Working with Undirected Graph\n"; pgRouting::UndirectedGraph undigraph(gType); undigraph.graph_insert_data(data_edges, total_edges); pgr_dijkstra(undigraph, paths, start_vid, end_vertices, false); } // use auto when possible auto count(count_tuples(paths)); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; log << "No paths found between Starting and any of the Ending vertices\n"; *log_msg = strdup(log.str().c_str()); return; } // get the space required to store all the paths (*return_tuples) = pgr_alloc(count, (*return_tuples)); log << "Converting a set of paths into the tuples\n"; (*return_count) = (collapse_paths(return_tuples, paths)); *err_msg = NULL; *log_msg = strdup(log.str().c_str()); } catch (AssertFailedException &exept) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; err << exept.what() << "\n"; *err_msg = strdup(log.str().c_str()); *log_msg = strdup(log.str().c_str()); } catch (std::exception& exept) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; err << exept.what() << "\n"; *err_msg = strdup(log.str().c_str()); *log_msg = strdup(log.str().c_str()); } catch(...) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; err << "Caught unknown exception!\n"; *err_msg = strdup(log.str().c_str()); *log_msg = strdup(log.str().c_str()); } }
int do_pgr_many_to_one_withPoints( pgr_edge_t *edges, size_t total_edges, Point_on_edge_t *points_p, size_t total_points, pgr_edge_t *edges_of_points, size_t total_edges_of_points, int64_t *start_pidsArr, size_t size_start_pidsArr, int64_t end_vid, char driving_side, bool details, bool directed, bool only_cost, General_path_element_t **return_tuples, size_t *return_count, char ** err_msg) { std::ostringstream log; try { std::vector< Point_on_edge_t > points(points_p, points_p + total_points); int errcode = check_points(points, log); if (errcode) { /* Point(s) with same pid but different edge/fraction/side combination found */ *err_msg = strdup(log.str().c_str()); return errcode; } std::vector< pgr_edge_t > edges_to_modify(edges_of_points, edges_of_points + total_edges_of_points); std::vector< pgr_edge_t > new_edges; create_new_edges( points, edges_to_modify, driving_side, new_edges); std::set< int64_t > s_start_vertices(start_pidsArr, start_pidsArr + size_start_pidsArr); std::vector< int64_t > start_vertices(s_start_vertices.begin(), s_start_vertices.end()); graphType gType = directed? DIRECTED: UNDIRECTED; std::deque< Path > paths; if (directed) { log << "Working with directed Graph\n"; pgrouting::DirectedGraph digraph(gType); digraph.graph_insert_data(edges, total_edges); digraph.graph_insert_data(new_edges); pgr_dijkstra(digraph, paths, start_vertices, end_vid, only_cost); } else { log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); undigraph.graph_insert_data(edges, total_edges); undigraph.graph_insert_data(new_edges); pgr_dijkstra(undigraph, paths, start_vertices, end_vid, only_cost); } #if 0 for (auto &path : paths) { adjust_pids(points, path); } #endif if (!details) { for (auto &path : paths) { eliminate_details(path, edges_to_modify); } } /* * order paths based on the start_pid */ std::sort(paths.begin(), paths.end(), [](const Path &a, const Path &b) { return a.start_id() < b.start_id(); }); size_t count(0); count = count_tuples(paths); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; log << "No paths found between Starting and any of the Ending vertices\n"; *err_msg = strdup(log.str().c_str()); return 0; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); log << "Converting a set of paths into the tuples\n"; (*return_count) = (collapse_paths(return_tuples, paths)); #ifndef NDEBUG { std::ostringstream log; log << "OK"; *err_msg = strdup(log.str().c_str()); } #else *err_msg = strdup(log.str().c_str()); #endif return 0; } catch ( ... ) { log << "Caught unknown exception!\n"; *err_msg = strdup(log.str().c_str()); return 1000; } return 0; }
void do_pgr_withPoints( pgr_edge_t *edges, size_t total_edges, Point_on_edge_t *points_p, size_t total_points, pgr_edge_t *edges_of_points, size_t total_edges_of_points, int64_t *start_pidsArr, size_t size_start_pidsArr, int64_t *end_pidsArr, size_t size_end_pidsArr, char driving_side, bool details, bool directed, bool only_cost, bool normal, General_path_element_t **return_tuples, size_t *return_count, char** log_msg, char** notice_msg, char** err_msg) { std::ostringstream log; std::ostringstream notice; std::ostringstream err; try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert((*return_count) == 0); pgassert(edges || edges_of_points); pgassert(points_p); pgassert(start_pidsArr); pgassert(end_pidsArr); std::vector< Point_on_edge_t > points(points_p, points_p + total_points); if (!normal) { for (auto &point : points) { if (point.side == 'r') { point.side = 'l'; } else if (point.side == 'l') { point.side = 'r'; } point.fraction = 1 - point.fraction; } if (driving_side == 'r') { driving_side = 'l'; } else if (driving_side == 'l') { driving_side = 'r'; } } int errcode = check_points(points, log); if (errcode) { *log_msg = strdup(log.str().c_str()); err << "Unexpected point(s) with same pid" << " but different edge/fraction/side combination found."; *err_msg = pgr_msg(err.str().c_str()); return; } std::vector< pgr_edge_t > edges_to_modify( edges_of_points, edges_of_points + total_edges_of_points); std::vector< pgr_edge_t > new_edges; create_new_edges( points, edges_to_modify, driving_side, new_edges, log); std::vector<int64_t> start_vertices(start_pidsArr, start_pidsArr + size_start_pidsArr); std::vector< int64_t > end_vertices(end_pidsArr, end_pidsArr + size_end_pidsArr); auto vertices(pgrouting::extract_vertices(edges, total_edges)); vertices = pgrouting::extract_vertices(vertices, new_edges); graphType gType = directed? DIRECTED: UNDIRECTED; std::deque< Path > paths; if (directed) { log << "Working with directed Graph\n"; pgrouting::DirectedGraph digraph(vertices, gType); digraph.insert_edges(edges, total_edges); digraph.insert_edges(new_edges); paths = pgr_dijkstra( digraph, start_vertices, end_vertices, only_cost, normal); } else { log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(vertices, gType); undigraph.insert_edges(edges, total_edges); undigraph.insert_edges(new_edges); paths = pgr_dijkstra( undigraph, start_vertices, end_vertices, only_cost, normal); } if (!details) { for (auto &path : paths) { eliminate_details(path, edges_to_modify); } } /* * order paths based on the start_pid, end_pid */ std::sort(paths.begin(), paths.end(), [](const Path &a, const Path &b) -> bool { if (b.start_id() != a.start_id()) { return a.start_id() < b.start_id(); } return a.end_id() < b.end_id(); }); size_t count(0); count = count_tuples(paths); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; #if 0 log << "No paths found"; *err_msg = pgr_msg(log.str().c_str()); #endif return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); log << "Converting a set of paths into the tuples\n"; (*return_count) = (collapse_paths(return_tuples, paths)); *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); *notice_msg = notice.str().empty()? *notice_msg : pgr_msg(notice.str().c_str()); } catch (AssertFailedException &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << "Caught unknown exception!"; *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } }
void do_pgr_dijkstraVia( pgr_edge_t* data_edges, size_t total_edges, int64_t* via_vidsArr, size_t size_via_vidsArr, bool directed, bool strict, bool U_turn_on_edge, Routes_t** return_tuples, size_t* return_count, char** log_msg, char** notice_msg, char** err_msg) { std::ostringstream log; std::ostringstream err; std::ostringstream notice; try { pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); graphType gType = directed? DIRECTED: UNDIRECTED; std::deque< Path >paths; log << "\nInserting vertices into a c++ vector structure"; std::vector< int64_t > via_vertices( via_vidsArr, via_vidsArr + size_via_vidsArr); if (directed) { log << "\nWorking with directed Graph"; pgrouting::DirectedGraph digraph(gType); digraph.insert_edges(data_edges, total_edges); pgr_dijkstraViaVertex( digraph, via_vertices, paths, strict, U_turn_on_edge, log); } else { log << "\nWorking with Undirected Graph"; pgrouting::UndirectedGraph undigraph(gType); undigraph.insert_edges(data_edges, total_edges); pgr_dijkstraViaVertex( undigraph, via_vertices, paths, strict, U_turn_on_edge, log); } size_t count(count_tuples(paths)); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; notice << "No paths found"; *log_msg = pgr_msg(notice.str().c_str()); return; } // get the space required to store all the paths (*return_tuples) = pgr_alloc(count, (*return_tuples)); log << "\nConverting a set of paths into the tuples"; (*return_count) = (get_route(return_tuples, paths)); (*return_tuples)[count - 1].edge = -2; *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); *notice_msg = notice.str().empty()? *notice_msg : pgr_msg(notice.str().c_str()); } catch (AssertFailedException &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << "Caught unknown exception!"; *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } }
int do_pgr_driving_many_to_dist(pgr_edge_t *data_edges, int64_t total_tuples, int64_t *start_vertex, int s_len, float8 distance, bool directedFlag, bool equiCostFlag, pgr_path_element3_t **ret_path, int *path_count, char ** err_msg) { try { // in c code this should this must have been checked: // 1) end_vertex is in the data_edges #if 0 // set to 1 if needed std::ostringstream log; #endif graphType gType = directedFlag? DIRECTED: UNDIRECTED; const int initial_size = 1; std::deque< Path >paths; typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::undirectedS, boost_vertex_t, boost_edge_t > UndirectedGraph; typedef boost::adjacency_list < boost::vecS, boost::vecS, boost::bidirectionalS, boost_vertex_t, boost_edge_t > DirectedGraph; Pgr_dijkstra < DirectedGraph > digraph(gType, initial_size); Pgr_dijkstra < UndirectedGraph > undigraph(gType, initial_size); std::vector< int64_t > start_vertices(start_vertex, start_vertex + s_len); if (directedFlag) { digraph.initialize_graph(data_edges, total_tuples); digraph.dijkstra_dd(paths, start_vertices, distance); } else { undigraph.initialize_graph(data_edges, total_tuples); undigraph.dijkstra_dd(paths, start_vertices, distance); } if (equiCostFlag == false) { int count(count_tuples(paths)); if (count == 0) { *err_msg = strdup("NOTICE: No return values was found"); *ret_path = noPathFound3(-1, path_count, (*ret_path)); return 0; } *ret_path = pgr_get_memory3(count, (*ret_path)); int trueCount(collapse_paths(ret_path, paths)); *path_count = trueCount; // assert (count == trueCount); } else { Path path = equi_cost(paths); size_t count(path.size()); if (count == 0) { *err_msg = strdup("NOTICE: No return values was found"); *ret_path = noPathFound3(-1, path_count, (*ret_path)); return 0; } int trueCount = 0; *ret_path = pgr_get_memory3(count, (*ret_path)); path.dpPrint(ret_path, trueCount); *path_count = trueCount; // assert (count == trueCount); } #if 1 *err_msg = strdup("OK"); #else *err_msg = strdup(log.str().c_str()); #endif return EXIT_SUCCESS; } catch ( ... ) { *err_msg = strdup("Caught unknown expection!"); return -1; } }
void do_pgr_floydWarshall( pgr_edge_t *data_edges, size_t total_tuples, bool directedFlag, // return values Matrix_cell_t **return_tuples, size_t *return_count, char ** log_msg, char ** err_msg) { // function starts std::ostringstream log; try { pgassert(!(*log_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); graphType gType = directedFlag? DIRECTED: UNDIRECTED; if (directedFlag) { log << "Processing Directed graph\n"; pgrouting::DirectedGraph digraph(gType); digraph.insert_edges(data_edges, total_tuples); log << digraph; pgr_floydWarshall(digraph, *return_count, return_tuples); } else { log << "Processing Undirected graph\n"; pgrouting::UndirectedGraph undigraph(gType); undigraph.insert_edges(data_edges, total_tuples); log << undigraph; pgr_floydWarshall(undigraph, *return_count, return_tuples); } if (*return_count == 0) { log << "NOTICE: No Vertices found??? wiered error\n"; *err_msg = strdup(log.str().c_str()); *return_tuples = NULL; *return_count = 0; return; } *log_msg = strdup(log.str().c_str()); return; } catch (AssertFailedException &except) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; log << except.what() << "\n"; *err_msg = strdup(log.str().c_str()); } catch (std::exception& except) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; log << except.what() << "\n"; *err_msg = strdup(log.str().c_str()); } catch(...) { if (*return_tuples) free(*return_tuples); (*return_count) = 0; log << "Caught unknown exception!\n"; *err_msg = strdup(log.str().c_str()); } }
void do_pgr_driving_distance( pgr_edge_t *data_edges, size_t total_edges, int64_t start_vertex, float8 distance, bool directedFlag, General_path_element_t **ret_path, size_t *path_count, char **err_msg) { std::ostringstream log; try { // if it already has values there will be a leak // call with pointing to NULL *ret_path = NULL; log << "NOTICE: Started processing pgr_drivingDistance for 1 start_vid\n"; // in c code this should have been checked: // 1) start_vertex is in the data_edges DONE graphType gType = directedFlag? DIRECTED: UNDIRECTED; const auto initial_size = total_edges; Path path; if (directedFlag) { log << "NOTICE: Processing Directed graph\n"; Pgr_base_graph< DirectedGraph > digraph(gType, initial_size); digraph.graph_insert_data(data_edges, total_edges); pgr_drivingDistance(digraph, path, start_vertex, distance); } else { log << "NOTICE: Processing Undirected graph\n"; Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size); undigraph.graph_insert_data(data_edges, total_edges); pgr_drivingDistance(undigraph, path, start_vertex, distance); } log << "Returning number of tuples" << path.size() << "\n"; if (path.empty()) { log << "NOTICE: it should have at least the one for it self"; *err_msg = strdup(log.str().c_str()); *ret_path = noResult(path_count, (*ret_path)); return; } log << "NOTICE: Calculating the number of tuples \n"; auto count = path.size(); log << "NOTICE Count: " << count << " tuples\n"; *ret_path = get_memory(count, (*ret_path)); size_t sequence = 0; path.get_pg_dd_path(ret_path, sequence); *path_count = count; #ifndef DEBUG *err_msg = strdup("OK"); #else *err_msg = strdup(log.str().c_str()); #endif return; } catch ( ... ) { log << "NOTICE: unknown exception cought"; *err_msg = strdup(log.str().c_str()); *ret_path = noResult(path_count, (*ret_path)); return; } }
// CREATE OR REPLACE FUNCTION pgr_dijkstra( // sql text, // start_vids anyarray, // end_vids anyarray, // directed boolean default true, void do_pgr_many_to_many_dijkstra( pgr_edge_t *data_edges, size_t total_edges, int64_t *start_vidsArr, size_t size_start_vidsArr, int64_t *end_vidsArr, size_t size_end_vidsArr, bool directed, bool only_cost, bool normal, General_path_element_t **return_tuples, size_t *return_count, char ** log_msg, char ** notice_msg, char ** err_msg) { std::ostringstream log; std::ostringstream err; std::ostringstream notice; try { pgassert(total_edges != 0); pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); graphType gType = directed? DIRECTED: UNDIRECTED; log << "Inserting vertices into a c++ vector structure"; std::vector<int64_t> start_vertices(start_vidsArr, start_vidsArr + size_start_vidsArr); std::vector< int64_t > end_vertices(end_vidsArr, end_vidsArr + size_end_vidsArr); std::deque< Path >paths; if (directed) { log << "\nWorking with directed Graph"; pgrouting::DirectedGraph digraph(gType); digraph.insert_edges(data_edges, total_edges); paths = pgr_dijkstra( digraph, start_vertices, end_vertices, only_cost, normal); } else { log << "\nWorking with Undirected Graph"; pgrouting::UndirectedGraph undigraph(gType); undigraph.insert_edges(data_edges, total_edges); paths = pgr_dijkstra( undigraph, start_vertices, end_vertices, only_cost, normal); } size_t count(0); count = count_tuples(paths); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; notice << "No paths found"; *log_msg = pgr_msg(notice.str().c_str()); return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); log << "\nConverting a set of paths into the tuples"; (*return_count) = (collapse_paths(return_tuples, paths)); *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); *notice_msg = notice.str().empty()? *notice_msg : pgr_msg(notice.str().c_str()); } catch (AssertFailedException &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << "Caught unknown exception!"; *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } }
void do_pgr_MY_FUNCTION_NAME( MY_EDGE_TYPE *data_edges, size_t total_edges, int64_t start_vid, int64_t end_vid, bool directed, bool only_cost, MY_RETURN_VALUE_TYPE **return_tuples, size_t *return_count, char ** log_msg, char ** notice_msg, char ** err_msg) { std::ostringstream log; std::ostringstream err; std::ostringstream notice; try { pgassert(!(*log_msg)); pgassert(!(*notice_msg)); pgassert(!(*err_msg)); pgassert(!(*return_tuples)); pgassert(*return_count == 0); pgassert(total_edges != 0); graphType gType = directed? DIRECTED: UNDIRECTED; Path path; if (directed) { log << "Working with directed Graph\n"; pgrouting::DirectedGraph digraph(gType); digraph.insert_edges(data_edges, total_edges); path = pgr_MY_FUNCTION_NAME(digraph, start_vid, end_vid, only_cost); } else { log << "Working with Undirected Graph\n"; pgrouting::UndirectedGraph undigraph(gType); undigraph.insert_edges(data_edges, total_edges); path = pgr_MY_FUNCTION_NAME( undigraph, start_vid, end_vid, only_cost); } auto count = path.size(); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; notice << "No paths found between start_vid and end_vid vertices"; return; } (*return_tuples) = pgr_alloc(count, (*return_tuples)); size_t sequence = 0; path.generate_postgres_data(return_tuples, sequence); (*return_count) = sequence; pgassert(*err_msg == NULL); *log_msg = log.str().empty()? *log_msg : pgr_msg(log.str().c_str()); *notice_msg = notice.str().empty()? *notice_msg : pgr_msg(notice.str().c_str()); } catch (AssertFailedException &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch (std::exception &except) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << except.what(); *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } catch(...) { (*return_tuples) = pgr_free(*return_tuples); (*return_count) = 0; err << "Caught unknown exception!"; *err_msg = pgr_msg(err.str().c_str()); *log_msg = pgr_msg(log.str().c_str()); } }
// CREATE OR REPLACE FUNCTION pgr_dijkstraViaVertices(sql text, vertices anyarray, directed boolean default true, void do_pgr_dijkstraViaVertex( pgr_edge_t *data_edges, size_t total_tuples, int64_t *via_vidsArr, size_t size_via_vidsArr, bool directed, bool strict, bool U_turn_on_edge, Routes_t **return_tuples, size_t *return_count, char ** err_msg){ std::ostringstream log; try { if (total_tuples == 1) { log << "Requiered: more than one tuple\n"; (*return_tuples) = NULL; (*return_count) = 0; *err_msg = strdup(log.str().c_str()); return; } graphType gType = directed? DIRECTED: UNDIRECTED; const auto initial_size = total_tuples; std::deque< Path >paths; log << "Inserting vertices into a c++ vector structure\n"; std::vector< int64_t > via_vertices(via_vidsArr, via_vidsArr + size_via_vidsArr); if (directed) { log << "Working with directed Graph\n"; Pgr_base_graph< DirectedGraph > digraph(gType, initial_size); digraph.graph_insert_data(data_edges, total_tuples); pgr_dijkstraViaVertex(digraph, via_vertices, paths, strict, U_turn_on_edge, log); } else { log << "Working with Undirected Graph\n"; Pgr_base_graph< UndirectedGraph > undigraph(gType, initial_size); undigraph.graph_insert_data(data_edges, total_tuples); pgr_dijkstraViaVertex(undigraph, via_vertices, paths, strict, U_turn_on_edge, log); } size_t count(count_tuples(paths)); if (count == 0) { (*return_tuples) = NULL; (*return_count) = 0; log << "No paths found between Starting and any of the Ending vertices\n"; *err_msg = strdup(log.str().c_str()); return; } // get the space required to store all the paths (*return_tuples) = get_memory(count, (*return_tuples)); log << "Converting a set of paths into the tuples\n"; (*return_count) = (get_route(return_tuples, paths)); (*return_tuples)[count - 1].edge = -2; #ifndef DEBUG *err_msg = strdup("OK"); #else *err_msg = strdup(log.str().c_str()); #endif } catch ( ... ) { log << "Caught unknown expection!\n"; *err_msg = strdup(log.str().c_str()); } }