root/branches/debug/extra/tsp/sql/routing_tsp_wrappers.sql

Revision 128, 7.3 KB (checked in by anton, 3 years ago)

Debugging branch added

Line 
1--
2-- Copyright (c) 2005 Sylvain Pasche,
3--               2006-2007 Anton A. Patrushev, Orkney, Inc.
4--
5-- This program is free software; you can redistribute it and/or modify
6-- it under the terms of the GNU General Public License as published by
7-- the Free Software Foundation; either version 2 of the License, or
8-- (at your option) any later version.
9--
10-- This program is distributed in the hope that it will be useful,
11-- but WITHOUT ANY WARRANTY; without even the implied warranty of
12-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13-- GNU General Public License for more details.
14--
15-- You should have received a copy of the GNU General Public License
16-- along with this program; if not, write to the Free Software
17-- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19
20-- BEGIN;
21
22-----------------------------------------------------
23-- Returns TSP solution as a set of vertex ids
24--
25-- Last changes: 14.02.2008
26-----------------------------------------------------
27CREATE OR REPLACE FUNCTION tsp_ids(geom_table varchar,
28       ids varchar, source integer)
29       RETURNS SETOF integer AS
30$$
31DECLARE
32        r record;
33        path_result record;
34        v_id integer;
35        prev integer;
36
37BEGIN
38        prev := -1;
39        FOR path_result IN EXECUTE 'SELECT vertex_id FROM tsp(''select distinct source::integer as source_id, x(startpoint(the_geom)), y(startpoint(the_geom)) from ' ||
40                quote_ident(geom_table) || ' where source in (' ||
41                ids || ')'', '''|| ids  ||''', '|| source  ||')' LOOP
42
43                v_id = path_result.vertex_id;
44        RETURN NEXT v_id;
45        END LOOP;
46
47        RETURN;
48END;
49$$
50LANGUAGE 'plpgsql' VOLATILE STRICT;
51
52
53------------------------------------------------------
54-- Returns TSP solution as a set of vertices connected
55-- with A* paths
56--
57-- Last changes: 14.02.2008
58------------------------------------------------------
59CREATE OR REPLACE FUNCTION tsp_astar(
60       geom_table varchar,ids varchar, source integer, delta double precision)
61       RETURNS SETOF GEOMS AS
62$$
63DECLARE
64        r record;
65        path_result record;
66        v_id integer;
67        prev integer;
68        geom geoms;
69
70        id integer;
71BEGIN
72       
73        id :=0;
74        prev := source;
75        FOR path_result IN EXECUTE 'SELECT vertex_id FROM tsp(''select distinct source::integer as source_id, x1::double precision as x, y1::double precision as y from ' ||
76          quote_ident(geom_table) || ' where source in (' ||
77          ids || ')'', '''|| ids  ||''', '|| source  ||')' LOOP
78
79                v_id = path_result.vertex_id;
80               
81                FOR r IN EXECUTE 'SELECT gid, the_geom FROM astar_sp_delta( ''' ||
82                  quote_ident(geom_table)  ||''', '|| v_id ||', '||
83                  prev ||','||delta||')' LOOP
84                    geom.gid := r.gid;
85                    geom.the_geom := r.the_geom;
86                    id := id+1;
87                    geom.id       := id;
88                    RETURN NEXT geom;
89                END LOOP;
90               
91        prev = v_id;
92        END LOOP;
93        RETURN;
94END;
95$$
96LANGUAGE 'plpgsql' VOLATILE STRICT;
97
98------------------------------------------------------
99-- Returns TSP solution as a set of vertices connected
100-- with A* paths.
101-- For directed graphs.
102--
103-- Last changes: 14.02.2008
104------------------------------------------------------
105CREATE OR REPLACE FUNCTION tsp_astar_directed(
106       geom_table varchar,ids varchar, source integer, delta float8, dir boolean, rc boolean)
107       RETURNS SETOF GEOMS AS
108$$
109DECLARE
110        r record;
111        path_result record;
112        v_id integer;
113        prev integer;
114        geom geoms;
115       
116        query text;
117
118        id integer;
119BEGIN
120       
121        id :=0;
122        prev := source;
123        query := 'SELECT vertex_id FROM tsp(''select distinct source::integer '||
124                'as source_id, x1::double precision as x, y1::double precision as y ';
125               
126        IF rc THEN query := query || ' , reverse_cost ';
127        END IF;
128
129        query := query || 'from ' || quote_ident(geom_table) || ' where source in (' ||
130          ids || ')'', '''|| ids  ||''', '|| source  ||')';
131       
132        FOR path_result IN EXECUTE query
133        LOOP
134
135                v_id = path_result.vertex_id;
136               
137                FOR r IN EXECUTE 'SELECT gid, the_geom FROM astar_sp_delta_directed( ''' ||
138                  quote_ident(geom_table)  ||''', '|| v_id ||', '||
139                  prev ||','||delta||', '''||text(dir)||''', '''||text(rc)||''')' LOOP
140                    geom.gid := r.gid;
141                    geom.the_geom := r.the_geom;
142                    id := id+1;
143                    geom.id       := id;
144                    RETURN NEXT geom;
145                END LOOP;
146               
147        prev = v_id;
148        END LOOP;
149        RETURN;
150END;
151$$
152LANGUAGE 'plpgsql' VOLATILE STRICT;
153
154
155------------------------------------------------------
156-- Returns TSP solution as a set of vertices connected
157-- with Dijkstra paths.
158------------------------------------------------------
159CREATE OR REPLACE FUNCTION tsp_dijkstra(
160       geom_table varchar,ids varchar, source integer)
161       RETURNS SETOF GEOMS AS
162$$
163DECLARE
164        r record;
165        path_result record;
166        v_id integer;
167        prev integer;
168        geom geoms;
169
170        id integer;
171BEGIN
172       
173        id :=0;
174        prev := source;
175        FOR path_result IN EXECUTE 'SELECT vertex_id FROM tsp(''select distinct source::integer as source_id, x(startpoint(the_geom)), y(startpoint(the_geom)) from ' ||
176           quote_ident(geom_table) || ' where source in (' ||
177           ids || ')'', '''|| ids  ||''', '|| source  ||')' LOOP
178
179                v_id = path_result.vertex_id;
180               
181
182                FOR r IN EXECUTE 'SELECT gid, the_geom FROM dijkstra_sp_delta( ''' ||
183                  quote_ident(geom_table)  ||''', '|| v_id ||', '||
184                  prev ||',0.03)' LOOP
185                    geom.gid := r.gid;
186                    geom.the_geom := r.the_geom;
187                    id := id+1;
188                    geom.id       := id;
189                    RETURN NEXT geom;
190                END LOOP;
191               
192        prev = v_id;
193        END LOOP;
194        RETURN;
195END;
196$$
197LANGUAGE 'plpgsql' VOLATILE STRICT;
198
199------------------------------------------------------
200-- Returns TSP solution as a set of vertices connected
201-- with Dijkstra paths.
202-- For directed graphs.
203--
204-- Last changes: 14.02.2008
205------------------------------------------------------
206CREATE OR REPLACE FUNCTION tsp_dijkstra_directed(
207       geom_table varchar,ids varchar, source integer, delta float8, dir boolean, rc boolean)
208       RETURNS SETOF GEOMS AS
209$$
210DECLARE
211        r record;
212        path_result record;
213        v_id integer;
214        prev integer;
215        geom geoms;
216       
217        query text;
218
219        id integer;
220BEGIN
221       
222        id :=0;
223        prev := source;
224       
225        query := 'SELECT vertex_id FROM tsp(''select distinct source::integer as source_id, '||
226                    'x(startpoint(the_geom)), y(startpoint(the_geom))';
227                   
228        IF rc THEN query := query || ' , reverse_cost ';
229        END IF;
230
231        query := query || ' from ' || quote_ident(geom_table) || ' where source in (' ||
232           ids || ')'', '''|| ids  ||''', '|| source  ||')';
233           
234        FOR path_result IN EXECUTE query
235        LOOP
236
237                v_id = path_result.vertex_id;
238               
239
240                FOR r IN EXECUTE 'SELECT gid, the_geom FROM dijkstra_sp_delta_directed( ''' ||
241                  quote_ident(geom_table)  ||''', '|| v_id ||', '||
242                  prev ||','||delta||', '''||text(dir)||''', '''||text(rc)||''')' LOOP
243                    geom.gid := r.gid;
244                    geom.the_geom := r.the_geom;
245                    id := id+1;
246                    geom.id       := id;
247                    RETURN NEXT geom;
248                END LOOP;
249               
250        prev = v_id;
251        END LOOP;
252        RETURN;
253END;
254$$
255LANGUAGE 'plpgsql' VOLATILE STRICT;
256
257-- COMMIT;
Note: See TracBrowser for help on using the browser.