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 | -- Draws an alpha shape around given set of points. |
---|
23 | -- |
---|
24 | -- Last changes: 14.02.2008 |
---|
25 | ---------------------------------------------------------- |
---|
26 | CREATE OR REPLACE FUNCTION points_as_polygon(query varchar) |
---|
27 | RETURNS SETOF GEOMS AS |
---|
28 | $$ |
---|
29 | DECLARE |
---|
30 | r record; |
---|
31 | path_result record; |
---|
32 | i int; |
---|
33 | q text; |
---|
34 | x float8[]; |
---|
35 | y float8[]; |
---|
36 | geom geoms; |
---|
37 | id integer; |
---|
38 | BEGIN |
---|
39 | |
---|
40 | id :=0; |
---|
41 | |
---|
42 | i := 1; |
---|
43 | q := 'select 1 as gid, GeometryFromText(''POLYGON(('; |
---|
44 | |
---|
45 | FOR path_result IN EXECUTE 'select x, y from alphashape('''|| |
---|
46 | query || ''')' LOOP |
---|
47 | x[i] = path_result.x; |
---|
48 | y[i] = path_result.y; |
---|
49 | i := i+1; |
---|
50 | END LOOP; |
---|
51 | |
---|
52 | q := q || x[1] || ' ' || y[1]; |
---|
53 | i := 2; |
---|
54 | |
---|
55 | WHILE x[i] IS NOT NULL LOOP |
---|
56 | q := q || ', ' || x[i] || ' ' || y[i]; |
---|
57 | i := i + 1; |
---|
58 | END LOOP; |
---|
59 | |
---|
60 | q := q || ', ' || x[1] || ' ' || y[1]; |
---|
61 | q := q || '))'',-1) as the_geom'; |
---|
62 | |
---|
63 | FOR r in EXECUTE q LOOP |
---|
64 | geom.gid:=r.gid; |
---|
65 | geom.the_geom=r.the_geom; |
---|
66 | id := id+1; |
---|
67 | geom.id := id; |
---|
68 | RETURN NEXT geom; |
---|
69 | END LOOP; |
---|
70 | |
---|
71 | RETURN; |
---|
72 | END; |
---|
73 | $$ |
---|
74 | |
---|
75 | LANGUAGE 'plpgsql' VOLATILE STRICT; |
---|
76 | |
---|
77 | |
---|
78 | CREATE OR REPLACE FUNCTION driving_distance(table_name varchar, x double precision, y double precision, |
---|
79 | distance double precision, cost varchar, reverse_cost varchar, directed boolean, has_reverse_cost boolean) |
---|
80 | RETURNS SETOF GEOMS AS |
---|
81 | $$ |
---|
82 | DECLARE |
---|
83 | q text; |
---|
84 | srid integer; |
---|
85 | r record; |
---|
86 | geom geoms; |
---|
87 | BEGIN |
---|
88 | |
---|
89 | FOR r IN EXECUTE 'SELECT srid FROM geometry_columns WHERE f_table_name = '''||table_name||'''' LOOP |
---|
90 | END LOOP; |
---|
91 | |
---|
92 | srid := r.srid; |
---|
93 | |
---|
94 | RAISE NOTICE 'SRID: %', srid; |
---|
95 | |
---|
96 | q := 'SELECT gid, the_geom FROM points_as_polygon(''SELECT a.vertex_id::integer AS id, b.x1::double precision AS x, b.y1::double precision AS y'|| |
---|
97 | ' FROM driving_distance(''''''''SELECT gid AS id,source::integer,target::integer, '||cost||'::double precision AS cost, '|| |
---|
98 | reverse_cost||'::double precision as reverse_cost FROM '|| |
---|
99 | table_name||' WHERE setsrid(''''''''''''''''BOX3D('|| |
---|
100 | x-distance||' '||y-distance||', '||x+distance||' '||y+distance||')''''''''''''''''::BOX3D, '||srid||') && the_geom '''''''', (SELECT id FROM find_node_by_nearest_link_within_distance(''''''''POINT('||x||' '||y||')'''''''','||distance/10||','''''''''||table_name||''''''''')),'|| |
---|
101 | distance||',true,true) a, (SELECT * FROM '||table_name||' WHERE setsrid(''''''''BOX3D('|| |
---|
102 | x-distance||' '||y-distance||', '||x+distance||' '||y+distance||')''''''''::BOX3D, '||srid||')&&the_geom) b WHERE a.vertex_id = b.source'')'; |
---|
103 | |
---|
104 | RAISE NOTICE 'Query: %', q; |
---|
105 | |
---|
106 | FOR r IN EXECUTE q LOOP |
---|
107 | geom.gid := r.gid; |
---|
108 | geom.the_geom := r.the_geom; |
---|
109 | RETURN NEXT geom; |
---|
110 | END LOOP; |
---|
111 | |
---|
112 | RETURN; |
---|
113 | |
---|
114 | END; |
---|
115 | $$ |
---|
116 | |
---|
117 | LANGUAGE 'plpgsql' VOLATILE STRICT; |
---|
118 | |
---|
119 | -- COMMIT; |
---|