Advertisement
Guest User

Untitled

a guest
Oct 21st, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. drop function if exists PgRoutingRoute( geometry, geometry );
  2.  
  3. create or replace function PgRoutingRoute(point_from geometry, point_to geometry)
  4. returns geometry as $$
  5. declare
  6. result geometry;
  7. begin
  8. create temporary table start_edge as (
  9. select *
  10. from routing.ways
  11. order by the_geom <-> point_from
  12. limit 1
  13. );
  14. create temporary table end_edge as (
  15. select *
  16. from routing.ways
  17. order by the_geom <-> point_to
  18. limit 1
  19. );
  20. create temporary table pgrouting_result as (
  21. select *
  22. from pgr_dijkstra(
  23. 'SELECT gid AS id, source, target, ST_Length(the_geom::geography)/greatest(fwd_speed_mps, 0.000001) as cost, -ST_Length(the_geom::geography)/least(bwd_speed_mps, -0.000001) as reverse_cost FROM speeded_ways where the_geom && (select ST_Expand(ST_Extent(ST_MakeLine('''
  24. || point_from :: text || ''',''' || point_to :: text || ''')), 0.01))' :: text,
  25. (
  26. select target
  27. from start_edge
  28. ),
  29. (
  30. select source
  31. from end_edge
  32. )
  33. )
  34. );
  35.  
  36. --TODO determine how to connect first and last segments. And do we really need them?
  37. /*
  38. if (select target from start_edge) = (select node from pgrouting_result order by seq limit 1) then
  39. start_weight = (select st_length(the_geom::geography)*(1.0 - ST_LineLocatePoint(the_geom, point_from))*3.6/maxspeed_forward from start_edge);
  40. else
  41. start_weight = (select st_length(the_geom::geography)*(ST_LineLocatePoint(the_geom, point_from))*3.6/maxspeed_backward from start_edge);
  42. end if;
  43.  
  44. end_weight = (select st_length(the_geom::geography)*(1.0 - ST_LineLocatePoint(the_geom, point_to))*3.6/maxspeed_forward from end_edge);
  45. */
  46. drop table if exists start_edge;
  47. drop table if exists end_edge;
  48.  
  49. result = (
  50. select ST_MakeLine(
  51. ST_AddMeasure(
  52. case when node=ways.source then
  53. ways.the_geom
  54. else
  55. ST_Reverse(ways.the_geom)
  56. end,
  57. (/*start_weight + end_weight +*/ maxcost - agg_cost),
  58. (/*start_weight + end_weight +*/ maxcost - agg_cost - pgrouting_result.cost)
  59. )
  60. )
  61. from (
  62. select sum(cost) as maxcost
  63. from pgrouting_result
  64. ) as maxcost,
  65. pgrouting_result
  66. left join routing.ways on pgrouting_result.edge = routing.ways.gid
  67. );
  68.  
  69. drop table if exists pgrouting_result;
  70.  
  71. return result;
  72.  
  73. end
  74. $$ language plpgsql strict;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement