1 | = pgRouting with A-Star algorithm = |
---|
2 | |
---|
3 | A-Star algorithm is another well-known routing algorithm. It adds geographical |
---|
4 | information to source and target of each network link. This enables the shortest |
---|
5 | path search to prefer links which are closer to the target of the search. |
---|
6 | |
---|
7 | == Prepare routing table for A-Star == |
---|
8 | |
---|
9 | For A-Star you need to prepare your network table and add latitute/longitude |
---|
10 | columns (x1, y1 and x2, y2) and calculate their values. |
---|
11 | |
---|
12 | {{{ |
---|
13 | ALTER TABLE ways ADD COLUMN x1 double precision; |
---|
14 | ALTER TABLE ways ADD COLUMN y1 double precision; |
---|
15 | ALTER TABLE ways ADD COLUMN x2 double precision; |
---|
16 | ALTER TABLE ways ADD COLUMN y2 double precision; |
---|
17 | }}} |
---|
18 | |
---|
19 | {{{ |
---|
20 | UPDATE ways SET x1 = x(startpoint(the_geom)); |
---|
21 | UPDATE ways SET y1 = y(startpoint(the_geom)); |
---|
22 | UPDATE ways SET x2 = x(endpoint(the_geom)); |
---|
23 | UPDATE ways SET y2 = y(endpoint(the_geom)); |
---|
24 | }}} |
---|
25 | |
---|
26 | Note: "endpoint()" function fails for some versions of PostgreSQL (ie. 8.2.5, 8.1.9). |
---|
27 | A workaround for that problem is using the "PointN()" function instead: |
---|
28 | |
---|
29 | {{{ |
---|
30 | UPDATE ways SET x1 = x(PointN(the_geom, 1)); |
---|
31 | UPDATE ways SET y1 = y(PointN(the_geom, 1)); |
---|
32 | UPDATE ways SET x2 = x(PointN(the_geom, NumPoints(the_geom))); |
---|
33 | UPDATE ways SET y2 = y(PointN(the_geom, NumPoints(the_geom))); |
---|
34 | }}} |
---|
35 | |
---|
36 | == Shortest Path A-Star core function == |
---|
37 | |
---|
38 | Shortest Path A-Star function is very similar to the Dijkstra function, though |
---|
39 | it prefers links that are close to the target of the search. The heuristics of |
---|
40 | this search are predefined, so you need to recompile pgRouting if you want to |
---|
41 | make changes to the heuristic function itself. |
---|
42 | |
---|
43 | {{{ |
---|
44 | shortest_path_astar( sql text, |
---|
45 | source_id integer, |
---|
46 | target_id integer, |
---|
47 | directed boolean, |
---|
48 | has_reverse_cost boolean ) |
---|
49 | }}} |
---|
50 | |
---|
51 | Note: |
---|
52 | * Source and target IDs are vertex IDs. |
---|
53 | * Undirected graphs ("directed false") ignores "has_reverse_cost" setting |
---|
54 | |
---|
55 | === Example of A-Star core function === |
---|
56 | |
---|
57 | {{{ |
---|
58 | SELECT * FROM shortest_path_astar(' |
---|
59 | SELECT gid as id, |
---|
60 | source::integer, |
---|
61 | target::integer, |
---|
62 | length::double precision as cost, |
---|
63 | x1, y1, x2, y2 |
---|
64 | FROM ways', |
---|
65 | 10, 20, false, false); |
---|
66 | }}} |
---|
67 | |
---|
68 | {{{ |
---|
69 | vertex_id | edge_id | cost |
---|
70 | -----------+---------+--------------------- |
---|
71 | 10 | 293 | 0.0059596293824534 |
---|
72 | 9 | 4632 | 0.0846731039249787 |
---|
73 | 3974 | 4633 | 0.0765635090514303 |
---|
74 | ... | ... | ... |
---|
75 | 20 | -1 | 0 |
---|
76 | (63 rows) |
---|
77 | }}} |
---|
78 | |
---|
79 | === Wrapper function WITH bounding box === |
---|
80 | |
---|
81 | Wrapper functions extend the core functions with transformations, bounding box |
---|
82 | limitations, etc.. |
---|
83 | |
---|
84 | {{{ |
---|
85 | SELECT gid, AsText(the_geom) AS the_geom |
---|
86 | FROM astar_sp_delta('ways', 10, 20, 0.1); |
---|
87 | }}} |
---|
88 | |
---|
89 | {{{ |
---|
90 | gid | the_geom |
---|
91 | --------+--------------------------------------------------------------- |
---|
92 | 293 | MULTILINESTRING((18.4074149 -33.9443308,18.4074019 -33.9443833)) |
---|
93 | 4632 | MULTILINESTRING((18.4074149 -33.9443308,18.4077388 -33.9436183)) |
---|
94 | 4633 | MULTILINESTRING((18.4077388 -33.9436183,18.4080293 -33.9429733)) |
---|
95 | ... | ... |
---|
96 | 762 | MULTILINESTRING((18.4241422 -33.9179275,18.4237423 -33.9182966)) |
---|
97 | 761 | MULTILINESTRING((18.4243523 -33.9177154,18.4241422 -33.9179275)) |
---|
98 | (62 rows) |
---|
99 | }}} |
---|
100 | |
---|
101 | Note: |
---|
102 | * There is currently no wrapper function for A-Star without bounding box, |
---|
103 | since bounding boxes are very useful to increase performance. If you don't need |
---|
104 | a bounding box Dijkstra will be enough anyway. |
---|
105 | * The projection of OSM data is "degree", so we set a bounding box |
---|
106 | containing start and end vertex plus a 0.1 degree buffer for example. |
---|