-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLUR_trafloads.sql
66 lines (63 loc) · 2.29 KB
/
LUR_trafloads.sql
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
-- TRAFMAJORLOAD
-- TRAFLOAD
-- HEAVYTRAFMAJORLOAD
-- HEAVYTRAFLOAD
-- ROADLENGTH
-- MAJORROADLENGTH
do $$
declare
recpt text := 'pem_addresses';
roads text := 'routed_nor_bng'; --constant_nor_lur_bng, all_vehsum, allmv
radii text[] = array['1000', '500', '300', '100', '50'];
i text;
sql text;
begin
drop table if exists buffers;
--Make buffers
sql := 'create table buffers as select ';
foreach i in array radii
loop
sql := sql || 'st_buffer(r.geom, ' || i || ') as b' || i || ',';
end loop;
sql := sql || 'r.gid from ' || recpt || ' as r';
execute sql;
--Perform intersections
foreach i in array radii
loop
raise notice '%', i;
execute 'create index buf_indx_' || i || ' on buffers' || ' using gist (b' || i || ')';
sql := '
drop table if exists trafload' || i || ';
create table trafload' || i || ' as
select b.gid, coalesce(d.roadlength, 0) as roadlength, coalesce(d.heavytrafload, 0) as heavytrafload,
coalesce(d.majorroadlength, 0) as majorroadlength, coalesce(d.heavytrafmajorload, 0) as heavytrafmajorload,
coalesce(d.trafload, 0) as trafload, coalesce(d.trafmajorload, 0) as trafmajorload
from buffers as b left join
(with intsct as (
select r.gid as road, b.gid, st_length(st_intersection(r.geom, b.b'|| i ||')) as length, r.allmv,
r.c3 as heavy
from '|| roads ||' as r, buffers as b
where st_intersects(r.geom, b.b'|| i ||')
)
select RLN.gid, coalesce(RLN.roadlength, 0) as roadlength, coalesce(RLN.heavytrafload, 0) as heavytrafload,
coalesce(MRLN.majorroadlength, 0) as majorroadlength, coalesce(MRLN.heavytrafmajorload, 0) as heavytrafmajorload,
coalesce(RLN.trafload, 0) as trafload, coalesce(MRLN.trafmajorload, 0) as trafmajorload
from
(select intsct.gid, sum(intsct.length) as roadlength, sum(intsct.length * intsct.heavy) as heavytrafload,
sum(intsct.length * intsct.allmv) as trafload
from intsct
group by intsct.gid
) as RLN
left join
--TODO: MajorRoads on FRC or all_mv > 5000?
(select intsct.gid, sum(intsct.length) as majorroadlength, sum(intsct.length * intsct.heavy) as heavytrafmajorload,
sum(intsct.length * intsct.allmv) as trafmajorload
from intsct
where allmv > 5000
group by intsct.gid
) as MRLN
on RLN.gid = MRLN.gid) as d on b.gid = d.gid';
execute sql;
end loop;
end;
$$language plpgsql;