1

Using the OpenStreetMap data and osm2pgsql, I created a topology by using the pgr_createTopology() function. These tables are what I have:

                          List of relations
 Schema |                 Name                 |   Type   |  Owner   
--------+--------------------------------------+----------+----------
 public | geography_columns                    | view     | postgres
 public | geometry_columns                     | view     | postgres
 public | planet_osm_line                      | table    | postgres
 public | planet_osm_nodes                     | table    | postgres
 public | planet_osm_point                     | table    | postgres
 public | planet_osm_polygon                   | table    | postgres
 public | planet_osm_rels                      | table    | postgres
 public | planet_osm_roads                     | table    | postgres
 public | planet_osm_roads_vertices_pgr        | table    | postgres
 public | planet_osm_roads_vertices_pgr_id_seq | sequence | postgres
 public | planet_osm_ways                      | table    | postgres
 public | spatial_ref_sys                      | table    | postgres
(12 rows)

Now I want to run pgr_dijkstra() to find the shortest path.

Which table and columns should I use? Which column represents start_vid and end_vid?

1 Answer 1

2

Like explain in the documentation the pgr_dijkstra function need the following parameters :

  1. Edges SQL : A string that represents a SQL query for extract the edges from your edge_table.
  2. start_vid : An integer or an array of integers corresponding to the id (in the vertices tables ) of the start point(s).
  3. end_vid: An integer or an array of integers corresponding to the id (in the vertices tables ) of the end point(s).
  4. (optional) directed : A boolean which indicates if the graph is directed (in this case you need a reverse_cost column in your edge_table.

If your edge_table has the default names the query will look like this :

SELECT *
FROM pgr_dijkstra(
    'SELECT id, source, target, cost, reverse_cost FROM edge_table',
    2, 
    3,
    FALSE
);

NB: For the query to work, a positive cost must be assigned to each section (the function pgr_createTopology does not), for example with the following query :

UPDATE edge_table SET cost = St_length(geom);
2
  • Thanks. What should be my edge_table? Would it be planet_osm_roads? I tried select osm_id as id, source, target from planet_osm_roads; but where can I find geom or cost?
    – Chang
    Commented May 5, 2022 at 3:50
  • 1
    select * from pgr_dijkstra( 'select osm_id as id, source, target, St_length(way) as cost from planet_osm_roads', 10325, 10320, False); I tried this and it worked! Thanks a lot!!
    – Chang
    Commented May 5, 2022 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.