Example #1
0
/*                          MODIFY AS NEEDED                                  */
static
void
process(
        char* edges_sql,
        int64_t start_vid,
        int64_t end_vid,
        bool directed,
        bool only_cost,
        General_path_element_t **result_tuples,
        size_t *result_count) {
    pgr_SPI_connect();

    PGR_DBG("Load data");
    pgr_edge_t *edges = NULL;

    if (start_vid == end_vid) {
        (*result_count) = 0;
        (*result_tuples) = NULL;
        pgr_SPI_finish();
        return;
    }

    size_t total_tuples = 0;
    pgr_get_data_5_columns(edges_sql, &edges, &total_tuples);

    if (total_tuples == 0) {
        PGR_DBG("No edges found");
        (*result_count) = 0;
        (*result_tuples) = NULL;
        pgr_SPI_finish();
        return;
    }
    PGR_DBG("Total %ld tuples in query:", total_tuples);

    PGR_DBG("Starting processing");
    clock_t start_t = clock();
    char *err_msg = NULL;
    do_pgr_one_to_one_dijkstra(
            edges,
            total_tuples,
            start_vid,
            end_vid,
            directed,
            only_cost,
            result_tuples,
            result_count,
            &err_msg);

    time_msg(" processing Dijkstra one to one", start_t, clock());
    PGR_DBG("Returning %ld tuples\n", *result_count);
    PGR_DBG("Returned message = %s\n", err_msg);

    free(err_msg);
    pfree(edges);
    pgr_SPI_finish();
}
/*                          MODIFY AS NEEDED                                   */
static
void
process(
        char* edges_sql,
        char* points_sql,
        int64_t start_pid,
        int64_t end_pid,
        bool directed,
        char *driving_side,
        bool details,
        bool only_cost,
        General_path_element_t **result_tuples,
        size_t *result_count) {

    driving_side[0] = tolower(driving_side[0]);
    PGR_DBG("driving side:%c",driving_side[0]);
    if (! ((driving_side[0] == 'r')
                || (driving_side[0] == 'l'))) {
        driving_side[0] = 'b'; 
    }
    PGR_DBG("estimated driving side:%c",driving_side[0]);

    pgr_SPI_connect();

    PGR_DBG("load the points");
    Point_on_edge_t *points = NULL;
    size_t total_points = 0;
    pgr_get_points(points_sql, &points, &total_points);

#ifdef DEBUG
    size_t i = 0;
    for (i = 0; i < total_points; i ++) {
       PGR_DBG("%ld\t%ld\t%f\t%c",points[i].pid, points[i].edge_id, points[i].fraction, points[i].side);
    }
#endif

    PGR_DBG("  -- change the query");
    char *edges_of_points_query = NULL;
    char *edges_no_points_query = NULL;
        get_new_queries(
                edges_sql, points_sql,
                &edges_of_points_query,
                &edges_no_points_query);

    PGR_DBG("edges_of_points_query:\n%s", edges_of_points_query);
    PGR_DBG("edges_no_points_query:\n%s", edges_no_points_query);

    PGR_DBG("load the edges that match the points");
    pgr_edge_t *edges_of_points = NULL;
    size_t total_edges_of_points = 0;
    pgr_get_data_5_columns(edges_of_points_query, &edges_of_points, &total_edges_of_points);

    PGR_DBG("Total %ld edges in query:", total_edges_of_points);
#ifdef DEBUG
    for (i = 0; i < total_edges_of_points; i ++) {
        PGR_DBG("%ld\t%ld\t%ld\t%f\t%f",
                edges_of_points[i].id,
                edges_of_points[i].source,
                edges_of_points[i].target,
                edges_of_points[i].cost,
                edges_of_points[i].reverse_cost);
    }
#endif



    PGR_DBG("load the edges that dont match the points");
    pgr_edge_t *edges = NULL;
    size_t total_edges = 0;
    pgr_get_data_5_columns(edges_no_points_query, &edges, &total_edges);

    PGR_DBG("Total %ld edges in query:", total_edges);
#ifdef DEBUG
    for (i = 0; i < total_edges; i ++) {
        PGR_DBG("%ld\t%ld\t%ld\t%f\t%f",
                edges[i].id,
                edges[i].source,
                edges[i].target,
                edges[i].cost,
                edges[i].reverse_cost);
    }
#endif

    PGR_DBG("freeing allocated memory not used anymore");
    free(edges_of_points_query);
    free(edges_no_points_query);

    if ( (total_edges + total_edges_of_points) == 0) {
        PGR_DBG("No edges found");
        (*result_count) = 0;
        (*result_tuples) = NULL;
        pgr_SPI_finish();
        return;
    }

    PGR_DBG("Starting processing");
    char *err_msg = NULL;
    clock_t start_t = clock();
    int errcode = do_pgr_withPoints(
            edges,
            total_edges,
            points,
            total_points,
            edges_of_points,
            total_edges_of_points,
            start_pid,
            end_pid,
            directed,
            driving_side[0],
            details,
            only_cost,
            result_tuples,
            result_count,
            &err_msg);
    time_msg(" processing withPoints one to one", start_t, clock());
    PGR_DBG("Returning %ld tuples\n", *result_count);
    PGR_DBG("Returned message = %s\n", err_msg);

    if (!err_msg) free(err_msg);
    pfree(edges);
    pgr_SPI_finish();

    if (errcode) {
        pgr_send_error(errcode);
    }

}
Example #3
0
static int compute_trsp(
        char* edges_sql,
        int dovertex,
        int start_id,
        double start_pos,
        int end_id,
        double end_pos,
        bool directed, 
        bool has_reverse_cost,
        char *restrict_sql,
        path_element_t **path,
        size_t *path_count) {
    pgr_SPI_connect();

    PGR_DBG("Load edges");
    pgr_edge_t *edges = NULL;
    size_t total_tuples = 0;
    pgr_get_data_5_columns(edges_sql, &edges, &total_tuples);
    PGR_DBG("Total %ld edges", total_tuples);

    PGR_DBG("Load restrictions");
    restrict_t *restricts = NULL;
    size_t total_restrict_tuples = 0;
    if (restrict_sql == NULL) {
        PGR_DBG("Sql for restrictions is null.");
    } else {
        pgr_get_restriction_data(restrict_sql, &restricts, &total_restrict_tuples);
    }
#ifdef DEBUG
    int t1;
    for (t1=0; t1<total_restrict_tuples; t1++) {
        PGR_DBG("restricts: %.2f, %ld, %ld, %ld, %ld, %ld, %ld", restricts[t1].to_cost,
                restricts[t1].target_id, restricts[t1].via[0], restricts[t1].via[1], restricts[t1].via[2], restricts[t1].via[3], restricts[t1].via[4]);
    }
#endif
    PGR_DBG("Total %ld restriction", total_restrict_tuples);



    int v_max_id=0;
    int v_min_id=INT_MAX;

    /* track if start and end are both in edge tuples */
    int s_count = 0;
    int t_count = 0;

    char *err_msg;
    int ret = -1;
    register int z;

    PGR_DBG("start turn_restrict_shortest_path\n");

    if (start_id == end_id) {
        PGR_DBG("Starting vertex and Ending Vertex are equal");
        *path = NULL;
        return 0;
    }


    //defining min and max vertex id

    PGR_DBG("Total %i edge tuples", total_tuples);

    for(z=0; z<total_tuples; z++) {
        if(edges[z].source<v_min_id)
            v_min_id=(int)edges[z].source;

        if(edges[z].source>v_max_id)
            v_max_id=(int)edges[z].source;

        if(edges[z].target<v_min_id)
            v_min_id=(int)edges[z].target;

        if(edges[z].target>v_max_id)
            v_max_id=(int)edges[z].target;      

        //PGR_DBG("%i <-> %i", v_min_id, v_max_id);

    }

    //::::::::::::::::::::::::::::::::::::  
    //:: reducing vertex id (renumbering)
    //::::::::::::::::::::::::::::::::::::
    for(z=0; z<total_tuples; z++) {
        //check if edges[] contains source and target
        if (dovertex) {
            if(edges[z].source == start_id || edges[z].target == start_id)
                ++s_count;
            if(edges[z].source == end_id || edges[z].target == end_id)
                ++t_count;
        }
        else {
            if(edges[z].id == start_id)
                ++s_count;
            if(edges[z].id == end_id)
                ++t_count;
        }

        edges[z].source-=v_min_id;
        edges[z].target-=v_min_id;
        edges[z].cost = edges[z].cost;
        //PGR_DBG("edgeID: %i SRc:%i - %i, cost: %f", edges[z].id,edges[z].source, edges[z].target,edges[z].cost);      

    }

    PGR_DBG("Min vertex id: %i , Max vid: %i",v_min_id,v_max_id);
    PGR_DBG("Total %ld edge tuples", total_tuples);

    if(s_count == 0) {
        elog(ERROR, "Start id was not found.");
        return -1;
    }

    if(t_count == 0) {
        elog(ERROR, "Target id was not found.");
        return -1;
    }

    if (dovertex) {
        start_id -= v_min_id;
        end_id   -= v_min_id;
    }


    if (dovertex) {
        PGR_DBG("Calling trsp_node_wrapper\n");
        ret = trsp_node_wrapper(edges, total_tuples, 
                restricts, total_restrict_tuples,
                start_id, end_id,
                directed, has_reverse_cost,
                path, path_count, &err_msg);
    }
    else {
        PGR_DBG("Calling trsp_edge_wrapper\n");
        ret = trsp_edge_wrapper(edges, total_tuples, 
                restricts, total_restrict_tuples,
                start_id, start_pos, end_id, end_pos,
                directed, has_reverse_cost,
                path, path_count, &err_msg);
    }

    PGR_DBG("Message received from inside:");
    PGR_DBG("%s",err_msg);

    //PGR_DBG("SIZE %i\n",*path_count);

    //::::::::::::::::::::::::::::::::
    //:: restoring original vertex id
    //::::::::::::::::::::::::::::::::
    for(z=0;z<*path_count;z++) {
        //PGR_DBG("vetex %i\n",(*path)[z].vertex_id);
        if (z || (*path)[z].vertex_id != -1)
            (*path)[z].vertex_id+=v_min_id;
    }

    PGR_DBG("ret = %i\n", ret);

    PGR_DBG("*path_count = %i\n", *path_count);

    if (ret < 0)
    {
        //elog(ERROR, "Error computing path: %s", err_msg);
        ereport(ERROR, (errcode(ERRCODE_E_R_E_CONTAINING_SQL_NOT_PERMITTED), 
                    errmsg("Error computing path: %s", err_msg)));
    } 

    pgr_SPI_finish();
    return 0;
}