forked from dwmorley/BioshareCNOSSOS_EU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02_cnososs_bioshare_set_up_functions.sql
1228 lines (1152 loc) · 41.1 KB
/
02_cnososs_bioshare_set_up_functions.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- CHRISTY'S MODIFICATIONS TO MORELY'S CODE
-- THIS FILE SETS UP ALL THE FUNCTIONS IN SQL
--################################################################################
--## Copyright 2014-15 David Morley
--##
--## Licensed under the Apache License, Version 2.0 (the "License");
--## you may not use this file except in compliance with the License.
--## You may obtain a copy of the License at
--##
--## http://www.apache.org/licenses/LICENSE-2.0
--##
--## Unless required by applicable law or agreed to in writing, software
--## distributed under the License is distributed on an "AS IS" BASIS,
--## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--## See the License for the specific language governing permissions and
--## limitations under the License.
--################################################################################
--#################################################
--## ##
--## CNOSSOS-EU ROAD TRAFFIC NOISE MODEL ##
--## ADAPTED FOR BIOSHARE PROJECT ##
--## [2012 report eur 25379 en] ##
--## 24hr Version ##
--## ##
--## David Morley: d.morley@imperial.ac.uk ##
--## Version 1.3, 4th August 2015 ##
--## ##
--#################################################
--## For full details please see Environmental Pollution (2015), pp. 332-341 DOI information: 10.1016/j.envpol.2015.07.031
----################################################################################################
-- CHANGES FROM THE ORIGINAL CODE
----------------------------------
-- function ST_Line_Interpolate_Point updated to ST_LineInterpolatePoint
-- function ST_Line_Locate_Point updated to ST_LineLocatePoint
--get sound power level at source before attenuation
create or replace function get_sound(ql double precision, qh double precision,
speed1 double precision, speed3 double precision, tau double precision, p double precision,
lf_oct double precision[], lh_oct double precision[])
returns llt_octave as $$
declare
llt double precision[8];
tmp double precision; --air temperature correction
k double precision[] := array[0.08, 0.04]; --air temp correction [light, heavy]
vm double precision; --velocity from roadtype
q double precision; --traffic flow
lwp double precision; --individual propulsion noise
lwr double precision; --individual rolling noise
--final heavy class 3
ar double precision[][] := array[[79.7, 85.7, 84.5, 90.2, 97.3, 93.9, 84.1, 74.3], [87.0, 91.7, 94.1, 100.7, 100.8, 94.3, 87.1, 82.5]];--rolling a
ap double precision[][] := array[[94.5, 89.2, 88.0, 85.9, 84.2, 86.9, 83.3, 76.1], [104.4, 100.6, 101.7, 101.0, 100.1, 95.9, 91.3, 85.3]];--propulsion a
br double precision[][] := array[[30.0, 41.5, 38.9, 25.7, 32.5, 37.2, 39.0, 40.0], [30.0, 33.5, 31.3, 25.4, 31.8, 37.1, 38.6, 40.6]];-- rolling b
bp double precision[][] := array[[-1.3, 7.2, 7.7, 8.0, 8.0, 8.0, 8.0, 8.0], [0.0, 3.0, 4.6, 5.0, 5.0, 5.0, 5.0, 5.0]]; --propulsion b
vref double precision := 70.0; --velocity constant
lwm double precision[2]; --category source noise level
lwpr double precision; --combined rolling and propulsion
lw double precision; --combined categories source noise level
outllt llt_octave; --(return) 8x octaves at r before sum
lh double precision; --sound level in homogenous
lf double precision; --sound level in favourable
begin
--if no traffic or negative traffic
if ql <= 0 and qh <= 0 then
return null;
end if;
ql := greatest(ql, 0);
qh := greatest(qh, 0);
--for each octave
for i in 2..7 loop
--for light then heavy categories
for m in 1..2 loop
--air temp correction
tmp := k[m] * (20 - tau); --iii-10
--speed, flow for road, vehicle class
if m = 1 then
vm := speed1;
q := ql;
else
vm := speed3;
q := qh;
end if;
--individual vehicle noise
lwr := ar[m][i] + br[m][i] * log(vm / vref) + tmp; --iii-5
lwp := ap[m][i] + bp[m][i] * ((vm - vref) / vref); --iii-11
lwpr := 10 * log((10 ^ (lwr / 10)) + (10 ^ (lwp / 10)));--iii-3
--segment flow combined noise per category
if q > 0 then
lwm[m] := lwpr + 10 * log(q / (1000 * vm)); --iii-1
else
lwm[m] := 0;
end if;
end loop;
--combined light and heavy sources at s
lw := 10 * log((10 ^ (lwm[1] / 10)) + (10 ^ (lwm[2] / 10)));
lf := lw - greatest(lf_oct[i], 0);
lh := lw - greatest(lh_oct[i], 0);
--integrated long term sound level for path sr per octave
llt[i] := 10 * log((p * (10 ^ (lf / 10))) + ((1 - p) * (10 ^ (lh / 10)))); --vi-9
end loop;
outllt := (get_lglt(llt[1]), get_lglt(llt[2]), get_lglt(llt[3]), get_lglt(llt[4]),
get_lglt(llt[5]), get_lglt(llt[6]), get_lglt(llt[7]), get_lglt(llt[8]));
return outllt;
end
$$ language 'plpgsql' stable;
--get curved ray path, vi-25,26
create or replace function get_curve(d double precision)
returns double precision as $$
declare
hat double precision;
gamma double precision;
begin
gamma := 8 * d;
gamma := greatest(1000, gamma); --vi-24
hat := (2 * gamma * asin(d / (2 * gamma)));
return hat;
end
$$ language 'plpgsql' stable;
--get adif, vi-30
create or replace function get_adif(dif double precision[3], agroundso double precision, agroundor double precision)
returns double precision as $$
declare
adif double precision;
begin
adif := dif[1] + --vi-21
(-20 * log(1 + (((10 ^ (-agroundso / 20)) - 1) * (10 ^ (-(dif[2] - dif[1]) / 20))))) + --vi-31
(-20 * log(1 + (((10 ^ (-agroundor / 20)) - 1) * (10 ^ (-(dif[3] - dif[1]) / 20))))); --vi-32
return adif;
exception when invalid_argument_for_logarithm then
return dif[1];
end;
$$ language 'plpgsql' stable;
--get pure diffraction, delta dif, vi-21
create or replace function get_dif(lamda double precision, mdc double precision,
delta double precision[3], ch double precision)
returns double precision[3] as $$
declare
dif double precision[3];
begin
for i in 1..3 loop
if ((40 / lamda) * mdc * delta[i]) >= -2 then
dif[i] := 10 * ch * log(3 + ((40 / lamda)) * mdc * delta[i]);
if dif[i] < 0 then
dif[i] := 0;
end if;
if i = 1 then
dif[i] := least(dif[i], 25);
end if;
else
dif[i] := 0;
end if;
end loop;
return dif;
end
$$ language 'plpgsql' stable;
--get cf for vi-16,17
create or replace function get_cf(w double precision, d double precision)
returns double precision as $$
declare
cf double precision;
begin
cf := d * ((1 + (3 * w * d * exp(-(sqrt(w * d))))) / (1 + (w * d)));
return cf;
end
$$ language 'plpgsql' stable;
--get aground favourable
create or replace function get_agroundf(g double precision, gpath double precision, d double precision,
kk double precision, cf double precision, zs double precision, zr double precision)
returns double precision as $$
declare
agroundf double precision;
x double precision;
zsc double precision;
zrc double precision;
begin
zsc := zs + (0.0002 * ((((zs / (zs + zr)) ^ 2) * ((d ^ 2) / 2)))) + (0.006 * (d / (zs + zr))); --vi-19
zrc := zr + (0.0002 * ((((zr / (zs + zr)) ^ 2) * ((d ^ 2) / 2)))) + (0.006 * (d / (zs + zr)));
if d <= 30 * (zsc + zrc) then --vi-20
x := -3 * (1 - g);
else
x := (-3 * (1 - g)) * (1 + (2 * (1 - ((30 * (zsc + zrc)) / d))));
end if;
if gpath = 0 then
agroundf := x;
else
agroundf := -10 * log((4 * (kk ^ 2) / (d ^ 2)) * ((zsc ^ 2) -
(sqrt(((2 * cf) / kk)) * zsc) + (cf / kk)) *
((zrc ^ 2) - (sqrt(((2 * cf) / kk)) * zrc) + (cf / kk))); --vi-15
if agroundf < x then agroundf := x;
end if;
end if;
return agroundf;
end
$$ language 'plpgsql' stable;
--get aground homogenous
create or replace function get_agroundh(g double precision, gpath double precision, d double precision,
kk double precision, cf double precision, zs double precision, zr double precision)
returns double precision as $$
declare
agroundh double precision;
x double precision;
begin
if gpath = 0 then
agroundh := -3;
else
agroundh := -10 * log((4 * (kk ^ 2) / (d ^ 2)) * ((zs ^ 2) -
(sqrt(((2 * cf) / kk)) * zs) + (cf / kk)) *
((zr ^ 2) - (sqrt(((2 * cf) / kk)) * zr) + (cf / kk))); --vi-15
x := -3 * (1 - g);
if agroundh < x then agroundh := x;
end if;
end if;
return agroundh;
end
$$ language 'plpgsql' stable;
--get w for vi-16,17
create or replace function get_w(g double precision, fm double precision)
returns double precision as $$
declare
w double precision;
begin
w := 0.0185 * (((fm ^ 2.5) * (g ^ 2.6)) / (((fm ^ 1.5) * (g ^ 2.6)) +
(1300 * ((fm ^ 0.75) * (g ^ 1.3))) + 1160000)); --vi-17
return w;
end
$$ language 'plpgsql' stable;
--log transform sound level
create or replace function get_lglt(lt double precision)
returns double precision as $$
declare
t double precision;
begin
t := 10 ^ (lt / 10);
return t;
end
$$ language 'plpgsql' stable;
--integrate octave band totals to final dba level
create or replace function get_dba(lt double precision[8])
returns double precision as $$
declare
awc double precision[] := array[-26.22302364022129, -16.189851062139507, -8.675022287681816,
-3.2479917598088837, 0, 1.2016993444284976, 0.9642291552357972, -1.144507424157968]; --iec 61672:2003
t double precision;
begin
t := 0;
for i in 2..7 loop
t := t + (10 ^ ((lt[i] + awc[i]) / 10));
end loop;
if t = 0 then
return 0;
end if;
t := 10 * log(t);
if t < 0 then
t := 0;
end if;
return t; --vi-11 (is the final result)
end
$$ language 'plpgsql' stable;
--get step ratio
create or replace function get_steprat(lgth double precision, x integer)
returns integer as $$
declare
step integer;
begin
if lgth < x then
--take only midpoint of road sections < x m long
step := 100000;
else
--x metre intervals
step := cast(trunc(100000 / (lgth / x)) as integer);
end if;
return step;
end
$$ language 'plpgsql' stable;
--get air temperature at source
create or replace function get_temperature(r geometry, stations text, b integer)
returns double precision as $$
declare
tau double precision;
nn integer;
begin
if b = 0 then
return cast(stations as double precision);
else
nn := nnid(r, 15000, 2, 100, stations, 'src_id', 'geom');
execute '
select w.air_temp from '|| stations ||' as w
where w.src_id = ' || nn into tau;
return tau;
end if;
end
$$ language 'plpgsql' stable;
--get probability of favourable conditions for path
create or replace function get_favourable(rp geometry, stations text, b integer)
returns double precision as $$
declare
p double precision;
az double precision;
d varchar;
s geometry;
nn integer;
begin
if b = 0 then
return cast(stations as double precision);
else
s := st_endpoint(rp);
az := degrees(st_azimuth(st_startpoint(rp), s));
if az >= 0 and az < 90 then
d := 'ne';
elsif az >= 90 and az < 180 then
d := 'se';
elsif az >= 180 and az < 270 then
d := 'sw';
else
d := 'nw';
end if;
nn := nnid(s, 15000, 2, 100, stations, 'src_id', 'geom');
execute 'select w.' || d || ' from '|| stations ||' as w where w.src_id = ' || nn
into p;
return p;
end if;
end
$$ language 'plpgsql' stable;
--get multiplier for g based on corine class, table vi-1
create or replace function get_coeffg(lcc varchar)
returns double precision as $$
declare
g double precision;
s text;
begin
s := substring(lcc from 1 for 2);
case s
when '11', '12', '13',
'51', '52' then
g := 0.0; --hard things
when '33' then
g := 0.3;
when '14' then
g := 0.7;
when '21', '22', '23', '24',
'31', '32', '41', '42' then
g := 1.0; --soft things
else
g := 0.0;
end case;
return g;
end
$$ language 'plpgsql' stable;
--get corrected g path, vi-14
create or replace function get_gpath(g double precision, d double precision,
zs double precision, zr double precision)
returns double precision as $$
declare
gpath double precision;
gs double precision := 0; --ground effect at source (road)
begin
gpath := (g * (d / (30 * (zs + zr)))) + (gs * (1 - (d / (30 * (zs + zr)))));
return gpath;
end
$$ language 'plpgsql' stable;
--get angle of segment view from receptor, radians
create or replace function get_angleofview(r geometry, stt double precision,
stp double precision, road geometry)
returns segtri as $$
declare
triangle segtri;
anglea double precision;
angleb double precision;
anglec double precision;
pdist double precision;
radist float;
rbdist float;
abdist float;
a geometry;
b geometry;
begin
stp := least(stp, 100000); --snap to end of road
a := ST_LineInterpolatePoint(road, stt / 100000);
b := ST_LineInterpolatePoint(road, stp / 100000);
abdist := st_distance(a, b);
radist := st_distance(a, r);
rbdist := st_distance(b, r);
--from crtn noise model
if abdist = 0 or radist = 0 or rbdist = 0 then
anglea := 0;
anglec := 0;
else
anglea := (((rbdist ^ 2) - (radist ^ 2) - (abdist ^ 2)) / (-2 * radist * abdist));
anglec := (((abdist ^ 2) - (radist ^ 2) - (rbdist ^ 2)) / (-2 * radist * rbdist));
end if;
if anglea > 1 or anglea < -1 then
anglea := 0;
else
anglea := acos(anglea);
end if;
if anglec > 1 or anglec < -1 then
anglec := 0;
else
anglec := acos(anglec);
end if;
angleb := pi() - anglea - anglec;
if anglea = pi() / 2 then
pdist := radist;
elsif angleb = pi() / 2 then
pdist := rbdist;
elsif anglea = pi() and angleb = 0 then
pdist := 3.5;
anglec := radians(1);
elsif anglea = 0 and angleb = pi() then
pdist := 3.5;
anglec := radians(1);
elsif anglea = 0 and angleb = 0 then
pdist := 3.5;
anglec := 2 * pi();
elsif anglea > pi() / 2 and anglea < pi() then
pdist := sin(pi() - anglea) * radist;
elsif angleb > pi() / 2 and angleb < pi() then
pdist := sin(pi() - angleb) * rbdist;
elsif anglea < pi() / 2 and angleb < pi() / 2 then
pdist := sin(anglea) * radist;
end if;
if anglec = 0 then
anglec = radians(0.00001);
end if;
if pdist = 0 then
pdist = 3.5;
end if;
triangle := (anglec, pdist);
return triangle;
end
$$ language 'plpgsql' stable;
--http://gis.stackexchange.com/questions/14456/finding-the-closest-geometry-in-postgis
create or replace function nnid(nearto geometry, initialdistance real, distancemultiplier real,
maxpower integer, nearthings text, nearthingsidfield text, nearthingsgeometryfield text)
returns integer as $$
declare
sql text;
result integer;
begin
sql := ' select ' || quote_ident(nearthingsidfield)
|| ' from ' || quote_ident(nearthings)
|| ' where st_dwithin($1, '
|| quote_ident(nearthingsgeometryfield) || ', $2 * ($3 ^ $4))'
|| ' order by st_distance($1, ' || quote_ident(nearthingsgeometryfield) || ')'
|| ' limit 1';
for i in 0..maxpower loop
execute sql into result using nearto -- $1
, initialdistance -- $2
, distancemultiplier -- $3
, i; -- $4
if result is not null then return result; end if;
end loop;
return null;
end
$$ language 'plpgsql' stable;
create or replace function csharp_loop_mimic()
returns void as $$
declare
--gb
recpt text := 'ufpsites_shifted';
roads text := 'tres_const_24hr_bng';
landcover text := 'clc06_100m_v16_gb5k';
buildings text := 'landmapheights_clcurban';
mettemp text := 'midas_temp_bng';
metwind text := 'midas_winddir_bng';
pnt_buf double precision := 25; --buffer around receptor
road_buf double precision := 25; --buffer around road segment
radius integer := 500; --road search buffer from receptor
seg_dst integer := 20; --distance between each point on road
nrow integer;
result double precision;
stt timestamp;
begin
drop table if exists s;
drop table if exists temp_rays;
drop table if exists noroads;
drop table if exists ray_broken;
drop table if exists corine_broke_rays;
drop table if exists ray_details;
drop table if exists rays_intersects;
drop table if exists pths;
drop table if exists tempout;
drop table if exists minorflow;
--output table
create table tempout (
gid integer,
laeq1h_0 double precision,
laeq1h_1 double precision,
laeq1h_2 double precision,
laeq1h_3 double precision,
laeq1h_4 double precision,
laeq1h_5 double precision,
laeq1h_6 double precision,
laeq1h_7 double precision,
laeq1h_8 double precision,
laeq1h_9 double precision,
laeq1h_10 double precision,
laeq1h_11 double precision,
laeq1h_12 double precision,
laeq1h_13 double precision,
laeq1h_14 double precision,
laeq1h_15 double precision,
laeq1h_16 double precision,
laeq1h_17 double precision,
laeq1h_18 double precision,
laeq1h_19 double precision,
laeq1h_20 double precision,
laeq1h_21 double precision,
laeq1h_22 double precision,
laeq1h_23 double precision,
lday double precision,
leve double precision,
lnight double precision,
laeq16 double precision,
lden double precision,
st_x double precision,
st_y double precision
);
--temporary tables
create table s (
gid integer,
geom geometry
);
create table temp_rays (
gid integer,
road geometry,
ray_step integer,
road_id integer,
main_ray_path geometry,
seg_start double precision,
seg_end double precision,
id serial
);
create index temp1_indx on temp_rays using gist(main_ray_path);
create index temp2_indx on temp_rays using gist(road);
create table noroads (
geom geometry,
id serial
);
create index noroads_indx on noroads using gist(geom);
create table ray_broken (
id integer,
geomfrag geometry,
r geometry,
d double precision
);
create index ray_broken_indx on ray_broken using gist(geomfrag);
create index ray_broken_indxr on ray_broken using gist(r);
create table corine_broke_rays (
id integer,
geom geometry,
clc character varying(3),
d double precision,
gc double precision,
mpd double precision,
dist_ee double precision,
dist_se double precision
);
create index corine_broke_rays_indx on corine_broke_rays using gist(geom);
create table rays_intersects (
gid integer,
road geometry,
ray_step integer,
road_id integer,
main_ray_path geometry,
seg_start double precision,
seg_end double precision,
id integer,
o1d double precision,
o2d double precision,
sumhl double precision,
sumh double precision,
g double precision
);
create table ray_details (
main_ray_path geometry,
gid integer,
id integer,
aov segtri,
d double precision,
road_id integer,
o1d double precision,
o2d double precision,
avh double precision,
g double precision,
gor double precision,
gso double precision,
tau double precision,
p double precision
);
create table pths (
hr integer,
octaves llt_octave
);
create table minorflow (
hour double precision,
flow double precision
);
--UK 600 per day
insert into minorflow values (0, 4.679128468);
insert into minorflow values (1, 3.178949875);
insert into minorflow values (2, 2.57173473);
insert into minorflow values (3, 2.714608882);
insert into minorflow values (4, 4.036194785);
insert into minorflow values (5, 8.608167639);
insert into minorflow values (6, 19.93094416);
insert into minorflow values (7, 35.53994523);
insert into minorflow values (8, 39.21895464);
insert into minorflow values (9, 35.21847839);
insert into minorflow values (10, 36.29003453);
insert into minorflow values (11, 38.29027265);
insert into minorflow values (12, 39.00464341);
insert into minorflow values (13, 39.36182879);
insert into minorflow values (14, 40.14763662);
insert into minorflow values (15, 42.39790451);
insert into minorflow values (16, 46.64841053);
insert into minorflow values (17, 46.79128468);
insert into minorflow values (18, 37.3973092);
insert into minorflow values (19, 26.68174783);
insert into minorflow values (20, 19.03798071);
insert into minorflow values (21, 14.21597809);
insert into minorflow values (22, 10.75127991);
insert into minorflow values (23, 7.286581736);
--do the calculations
execute 'select count(*) from '|| recpt into nrow;
for a in 1..nrow loop
select clock_timestamp() into stt;
select cnossos_2(a, recpt, roads,
landcover, buildings, mettemp, metwind,
pnt_buf, road_buf, radius, seg_dst) into result;
raise notice '### % of %: laeq16 = %, time: % ###', a, nrow, result, clock_timestamp() - stt;
end loop;
--final table
drop table if exists output;
execute '
create table output as
select a.*,
laeq1h_0, laeq1h_1, laeq1h_2, laeq1h_3, laeq1h_4, laeq1h_5, laeq1h_6,
laeq1h_7, laeq1h_8, laeq1h_9, laeq1h_10, laeq1h_11, laeq1h_12, laeq1h_13,
laeq1h_14, laeq1h_15, laeq1h_16, laeq1h_17, laeq1h_18, laeq1h_19, laeq1h_20,
laeq1h_21, laeq1h_22, laeq1h_23, lday, leve, lnight, laeq16, lden,
b.st_x, b.st_y from '|| recpt ||' as a
left join tempout as b on a.gid = b.gid';
truncate tempout;
end
$$ language 'plpgsql' volatile;
create or replace function cnossos_2(
i integer,
receptors text,
roads text,
landcover text,
buildings text,
mettemp text,
metwind text,
pnt_buf double precision,
road_buf double precision,
radius double precision,
seg_dst integer
)
returns double precision as $$
declare
cwind integer;
ctemp integer;
db double precision;
mtau double precision;
begin
--get receptor
execute '
insert into s
select gid, geom from ' || receptors ||
' where gid =' || i;
--construct rays and segments
execute '
insert into temp_rays
with line as (
--clip roads within 500m of r
with rds as (
select distinct rr.gid, rr.geom as recpt, t.gid as road_id,
(st_dump(t.geom)).geom as road, get_steprat(st_length(t.geom), '|| seg_dst ||') as step_rat
from s as rr inner join '|| roads ||' as t
on st_dwithin(rr.geom, t.geom, '|| radius ||')
)
select * from rds
union all
--if no roads found in 500m of r, take the next nearest road
select distinct rr.gid, rr.geom as recpt, t.gid as road_id,
(st_dump(t.geom)).geom as road, get_steprat(st_length(t.geom), '|| seg_dst ||') as step_rat
from '|| roads ||' as t, s as rr left join rds
on rds.gid = rr.gid
where rds.gid is null
and t.gid = nnid(rr.geom, '|| radius ||', 2, 100, '''|| roads ||''', ''gid'', ''geom'')
)
select splt.gid, splt.road, splt.ray_step, splt.road_id,
st_makeline(splt.recpt, ST_LineInterpolatePoint(splt.road, cast(ray_step as double precision) / 100000)) as main_ray_path,
(cast(ray_step as double precision) / (cast(step_rat as double precision))) * cast(step_rat as double precision) as seg_start,
(cast(ray_step as double precision) / (cast(step_rat as double precision))) * cast(step_rat as double precision) + cast(step_rat as double precision) as seg_end
from (
select line.gid, line.recpt, line.step_rat, line.road, line.road_id,
generate_series(line.step_rat / 2, 100000, line.step_rat) as ray_step
from line
) as splt';
--clip rays to 500m
execute '
delete from temp_rays using (
select id from
(select r.gid, r.id, st_length(r.main_ray_path) as len
from temp_rays as r) as lengths
left join
(select every.gid, every.total, outside.longer from
(select count(r1.main_ray_path) as total, r1.gid
from temp_rays as r1
group by r1.gid) as every
left join
(select count(r1.main_ray_path) as longer, r1.gid
from temp_rays as r1
where st_length(r1.main_ray_path) > '|| radius ||'
group by r1.gid) as outside
on every.gid = outside.gid) as counts
on lengths.gid = counts.gid
where (len > '|| radius ||' and total != 1)
--calculation limit stated as 2km
or (len > 2000 and total = 1) ) as remove
where remove.id = temp_rays.id';
--buffer for roads used to x metres (25m)
execute '
insert into noroads
with a as (
select st_union(st_buffer(s1.road, '|| road_buf ||')) as geom
from
(select distinct road from temp_rays) as s1
)
select (st_dump(st_difference(l.buf, a.geom))).geom as geom
from a,
(select st_union(st_buffer(rc.geom, 2000)) as buf
from a, s as rc) as l';
--break rays on roads
execute '
insert into ray_broken
with cut as (
select st_makeline(ST_LineInterpolatePoint(rp.main_ray_path, '|| pnt_buf ||' / st_length(rp.main_ray_path)), st_endpoint(rp.main_ray_path)) as cr,
st_length(rp.main_ray_path) as d, rp.id
from temp_rays as rp
where st_length(rp.main_ray_path) >= '|| pnt_buf ||'
union all
select rp.main_ray_path as cr, st_length(rp.main_ray_path) as d, rp.id
from temp_rays as rp
where st_length(rp.main_ray_path) < '|| pnt_buf ||'
)
select cut.id, (st_dump(st_intersection(cut.cr, c.geom))).geom as geomfrag, st_endpoint(cut.cr) as r, cut.d
from cut, noroads as c
where st_intersects(cut.cr, c.geom)';
--intersect ray fragments with corine
execute '
insert into corine_broke_rays
select c.id, c.geom, c.clc, st_length(c.geom) as d, get_coeffg(c.clc) as gc, c.d as mpd,
st_distance(st_endpoint(c.geom), c.r) as dist_ee,
st_distance(st_startpoint(c.geom), c.r) as dist_se
from
(select a.id, (st_dump(st_intersection(a.geomfrag, lc.geom))).geom as geom, lc.code_06 as clc, a.r, a.d
from ray_broken as a, '|| landcover ||' as lc
where st_intersects(a.geomfrag, lc.geom)) as c';
--intersect urban fragments with building heights
execute '
insert into rays_intersects
select r.*, st_length(r.main_ray_path) - (st_length(r.main_ray_path) * h.o1) as o1d, st_length(r.main_ray_path) * h.o2 as o2d,
h.sumhl, h.sumh, gp.g from temp_rays as r left join
(with lminter as (
select (st_dump(st_intersection(a.geom, lm.geom))).geom as h, lm.height, a.id
from (select r.geom, r.id from corine_broke_rays as r
where r.clc = ''111'' or r.clc = ''112'' or r.clc = ''121'') as a, '|| buildings ||' as lm
where st_intersects(a.geom, lm.geom)
and lm.height > 0
)
select max(ST_LineLocatePoint(rp.main_ray_path, st_endpoint(hh.h))) as o1,
min(ST_LineLocatePoint(rp.main_ray_path, st_startpoint(hh.h))) as o2, hh.id,
sum(hh.height * st_length(hh.h)) as sumhl,
sum(st_length(hh.h)) as sumh
from lminter as hh left join temp_rays as rp
on hh.id = rp.id
group by hh.id) as h
on r.id = h.id
left join
(select c.id, sum(c.gc * c.d) / c.mpd as g
from corine_broke_rays as c
group by c.id, c.mpd) as gp
on gp.id = r.id';
--using met data?
execute '
select count(relname) from pg_class
where relname = '''|| mettemp ||''''
into ctemp;
execute '
select count(relname) from pg_class
where relname = '''|| metwind ||''''
into cwind;
--make final table for noise calculation
execute '
insert into ray_details
select ri.main_ray_path, ri.gid, ri.id, get_angleofview(st_startpoint(ri.main_ray_path), ri.seg_start, ri.seg_end, ri.road) as aov, st_length(ri.main_ray_path) as d,
ri.road_id, ri.o1d, ri.o2d,
get_averageheight(ri.sumhl, ri.sumh) as avh,
ri.g, difg.gor, difg.gso, get_temperature(ri.main_ray_path, '''|| mettemp ||''', '|| ctemp ||') as tau, get_favourable(ri.main_ray_path, '''|| metwind ||''', '|| cwind ||') as p
from rays_intersects as ri left join
(select coalesce(o2.id, o1.id) as id, o2.gor, o1.gso from
(select sum(cbr.gc * cbr.d) / ri.o2d as gor, cbr.id
from rays_intersects as ri left join corine_broke_rays as cbr
on ri.id = cbr.id
where dist_se >= (cbr.mpd - ri.o2d)
and gc != 0
and ri.o2d > 0
group by ri.o2d, cbr.id) as o2
full join
(select sum(cbr.gc * cbr.d) / ri.o1d as gso, cbr.id
from rays_intersects as ri left join corine_broke_rays as cbr
on ri.id = cbr.id
where dist_ee <= ri.o1d
and gc != 0
and ri.o1d > 0
group by ri.o1d, cbr.id) as o1
on o1.id = o2.id) as difg
on difg.id = ri.id';
--make calculations for each hour
execute '
select get_noise(
f.qh_0, f.ql_0,
f.qh_1, f.ql_1,
f.qh_2, f.ql_2,
f.qh_3, f.ql_3,
f.qh_4, f.ql_4,
f.qh_5, f.ql_5,
f.qh_6, f.ql_6,
f.qh_7, f.ql_7,
f.qh_8, f.ql_8,
f.qh_9, f.ql_9,
f.qh_10, f.ql_10,
f.qh_11, f.ql_11,
f.qh_12, f.ql_12,
f.qh_13, f.ql_13,
f.qh_14, f.ql_14,
f.qh_15, f.ql_15,
f.qh_16, f.ql_16,
f.qh_17, f.ql_17,
f.qh_18, f.ql_18,
f.qh_19, f.ql_19,
f.qh_20, f.ql_20,
f.qh_21, f.ql_21,
f.qh_22, f.ql_22,
f.qh_23, f.ql_23,
f.speed1, f.speed3, r.aov, r.avh, r.d, r.o1d,
r.o2d, coalesce(r.g, 0), r.tau, coalesce(r.gor, 0), coalesce(r.gso, 0), r.p) as llt
from ray_details as r
left join '|| roads ||' as f
on f.gid = r.road_id';
--temperature for minor correction
select avg(r.tau) from ray_details as r into mtau;
--if cannot get ray average use receptor site
if mtau is null then
mtau := 11.0;
end if;
--save to results table
insert into tempout
with hourly as (
select
get_hourlydba(0, mtau) as laeq1h_0,
get_hourlydba(1, mtau) as laeq1h_1,
get_hourlydba(2, mtau) as laeq1h_2,
get_hourlydba(3, mtau) as laeq1h_3,
get_hourlydba(4, mtau) as laeq1h_4,
get_hourlydba(5, mtau) as laeq1h_5,
get_hourlydba(6, mtau) as laeq1h_6,
get_hourlydba(7, mtau) as laeq1h_7,
get_hourlydba(8, mtau) as laeq1h_8,
get_hourlydba(9, mtau) as laeq1h_9,
get_hourlydba(10, mtau) as laeq1h_10,
get_hourlydba(11, mtau) as laeq1h_11,
get_hourlydba(12, mtau) as laeq1h_12,
get_hourlydba(13, mtau) as laeq1h_13,
get_hourlydba(14, mtau) as laeq1h_14,
get_hourlydba(15, mtau) as laeq1h_15,
get_hourlydba(16, mtau) as laeq1h_16,
get_hourlydba(17, mtau) as laeq1h_17,
get_hourlydba(18, mtau) as laeq1h_18,
get_hourlydba(19, mtau) as laeq1h_19,
get_hourlydba(20, mtau) as laeq1h_20,
get_hourlydba(21, mtau) as laeq1h_21,
get_hourlydba(22, mtau) as laeq1h_22,
get_hourlydba(23, mtau) as laeq1h_23
)
select s.gid,
--hourly predictions
laeq1h_0, laeq1h_1, laeq1h_2, laeq1h_3, laeq1h_4, laeq1h_5, laeq1h_6,
laeq1h_7, laeq1h_8, laeq1h_9, laeq1h_10, laeq1h_11, laeq1h_12,
laeq1h_13, laeq1h_14, laeq1h_15, laeq1h_16, laeq1h_17, laeq1h_18,
laeq1h_19, laeq1h_20, laeq1h_21, laeq1h_22, laeq1h_23,
--day
10 * log(((10 ^ (laeq1h_7 / 10)) + (10 ^ (laeq1h_8 / 10)) + (10 ^ (laeq1h_9 / 10)) + (10 ^ (laeq1h_10 / 10)) + (10 ^ (laeq1h_11 / 10)) +
(10 ^ (laeq1h_12 / 10)) + (10 ^ (laeq1h_13 / 10)) + (10 ^ (laeq1h_14 / 10)) + (10 ^ (laeq1h_15 / 10)) + (10 ^ (laeq1h_16 / 10)) +
(10 ^ (laeq1h_17 / 10)) + (10 ^ (laeq1h_18 / 10))) / 12) as lday,
--evening
10 * log(((10 ^ (laeq1h_19 / 10)) + (10 ^ (laeq1h_20 / 10)) + (10 ^ (laeq1h_21 / 10)) + (10 ^ (laeq1h_22 / 10))) / 4) as leve,
--night
10 * log(((10 ^ (laeq1h_23 / 10)) + (10 ^ (laeq1h_0 / 10)) + (10 ^ (laeq1h_1 / 10)) + (10 ^ (laeq1h_2 / 10)) +
(10 ^ (laeq1h_3 / 10)) + (10 ^ (laeq1h_4 / 10)) + (10 ^ (laeq1h_5 / 10)) + (10 ^ (laeq1h_6 / 10))) / 8) as lnight,
--laeq16
10 * log(((10 ^ (laeq1h_7 / 10)) + (10 ^ (laeq1h_8 / 10)) + (10 ^ (laeq1h_9 / 10)) + (10 ^ (laeq1h_10 / 10)) + (10 ^ (laeq1h_11 / 10)) +
(10 ^ (laeq1h_12 / 10)) + (10 ^ (laeq1h_13 / 10)) + (10 ^ (laeq1h_14 / 10)) + (10 ^ (laeq1h_15 / 10)) + (10 ^ (laeq1h_16 / 10)) +
(10 ^ (laeq1h_17 / 10)) + (10 ^ (laeq1h_18 / 10)) + (10 ^ (laeq1h_19 / 10)) + (10 ^ (laeq1h_20 / 10)) + (10 ^ (laeq1h_21 / 10)) +
(10 ^ (laeq1h_22 / 10))) / 16) as laeq16,
--lden
10 * log(((12 * (10 ^ (((10 * log(((10 ^ (laeq1h_7 / 10)) + (10 ^ (laeq1h_8 / 10)) + (10 ^ (laeq1h_9 / 10)) + (10 ^ (laeq1h_10 / 10)) + (10 ^ (laeq1h_11 / 10)) +
(10 ^ (laeq1h_12 / 10)) + (10 ^ (laeq1h_13 / 10)) + (10 ^ (laeq1h_14 / 10)) + (10 ^ (laeq1h_15 / 10)) + (10 ^ (laeq1h_16 / 10)) +
(10 ^ (laeq1h_17 / 10)) + (10 ^ (laeq1h_18 / 10))) / 12)) / 12) / 10))) + (4 * (10 ^ (((10 * log(((10 ^ (laeq1h_19 / 10))
+ (10 ^ (laeq1h_20 / 10)) + (10 ^ (laeq1h_21 / 10)) + (10 ^ (laeq1h_22 / 10))) / 4)) + 5) / 10))) +
(8 * (10 ^ (((10 * log(((10 ^ (laeq1h_23 / 10)) + (10 ^ (laeq1h_0 / 10)) + (10 ^ (laeq1h_1 / 10)) + (10 ^ (laeq1h_2 / 10)) +
(10 ^ (laeq1h_3 / 10)) + (10 ^ (laeq1h_4 / 10)) + (10 ^ (laeq1h_5 / 10)) + (10 ^ (laeq1h_6 / 10))) / 8)) + 10) / 10)))) / 24) as lden,
--coordinates
st_x(s.geom), st_y(s.geom)
from hourly, s;
--clear tables
truncate s;
truncate temp_rays;
truncate noroads;
truncate ray_broken;
truncate corine_broke_rays;
truncate ray_details;
truncate rays_intersects;
truncate pths;
--report progress
select t.laeq16 from tempout as t where gid = i into db;
return db;
end
$$ language 'plpgsql' volatile;
--###################################################################
--### functions
--###################################################################
--sum and apply a-weighting over octaves
create or replace function get_hourlydba(h integer, avtau double precision)
returns double precision as $$