-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_pgr_route_normal
69 lines (60 loc) · 1.87 KB
/
function_pgr_route_normal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
--DROP FUNCTION routing.get_route(geometry, geometry);
CREATE OR REPLACE FUNCTION routing.get_route(
IN originpoint geometry,
IN destinationpoint geometry,
OUT seq integer,
OUT gid integer,
OUT name text,
OUT heading double precision,
OUT cost double precision,
OUT geom geometry
) RETURNS SETOF record AS
$BODY$
DECLARE
query text;
sub_query text;
originid integer;
destinationid integer;
rec record;
point integer;
seq integer;
BEGIN
EXECUTE 'SELECT routing.get_waypoint($1)' INTO originid USING originpoint;
EXECUTE 'SELECT routing.get_waypoint($1)' INTO destinationid USING destinationpoint;
RAISE NOTICE 'Origin ID: %', originid;
RAISE NOTICE 'Destination ID: %', destinationid;
sub_query := 'SELECT gid as id, source::int, target::int, length::float AS cost ' ||
'FROM routing.ways_with_info ';
query := 'SELECT id2, the_geom, ST_Reverse(the_geom) AS flip_geom, name, cost, source, target ' ||
'FROM public.pgr_dijkstra(' || quote_literal(sub_query) || ', ' || originid || ', ' || destinationid || ', true, false), routing.ways ' ||
'WHERE id2 = gid ' ||
'ORDER BY seq';
RAISE INFO ': %', query;
point := originid;
seq := 0;
FOR rec IN EXECUTE query
LOOP
-- Flip geometry (if required)
IF ( point != rec.source ) THEN
rec.the_geom := rec.flip_geom;
point := rec.source;
ELSE
point := rec.target;
END IF;
-- Calculate heading (simplified)
EXECUTE 'SELECT degrees( ST_Azimuth(
ST_StartPoint(''' || rec.the_geom::text || '''),
ST_EndPoint(''' || rec.the_geom::text || ''') ) )'
INTO heading;
-- Return record
seq := seq + 1;
gid := rec.gid;
name := rec.name;
cost := rec.cost;
geom := rec.the_geom;
RETURN NEXT;
END LOOP;
RETURN;
END
$BODY$
LANGUAGE 'plpgsql' VOLATILE STRICT;