I'm trying to get the overal time and distance from a routing in pgRouting, using pgr_dijkstra, but I have basically no idea on how to achieve it. So far I managed to figure out that the return from pgr_dijkstra is in cost units and I summed them up. I assume that calculating distance and time is actually done per segment in the route. Does someone have an example on how to do this?
SELECT SUM(cost)
FROM (SELECT cost
FROM pgr_dijkstra('SELECT u_gid as id,
u_source AS source,
u_target AS target,
cost AS cost,
reverse_cost
FROM ways', 78771, 26263, true, true)) AS r_cost;
The statement above will give me the sum of all the unit costs...
cost is the time or distance or whatever you use for the cost values. If cost on each edge is the length of the edge then cost is distance. If however you set cost to the traversal time for the edge, then cost is the time. If you make cost the length of the edges, and have an average speed associated with each edge, then you can join the results back to your edge table can compute the times.
SELECT cost as distance, cost/speed as time
FROM pgr_dijkstra('SELECT u_gid as id,
u_source AS source,
u_target AS target,
cost AS cost,
reverse_cost
FROM ways', 78771, 26263, true, true)) a,
ways b,
where a.id=b.u_gid;