Skip to content

Commit d416a3f

Browse files
authored
Merge pull request #61 from konturio/21258-add-coverage-generation-and-indicators-coverage-endpoint-to-insights-api
update find_max_resolution: set coverage polygon
2 parents 9a7f91e + b40a147 commit d416a3f

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

procedures/find_max_resolution.sql

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ $$
77
declare
88
area_km2_uuid uuid;
99
one_uuid uuid;
10+
global_max_res integer;
11+
coverage_max_res integer;
1012
begin
1113

1214
select internal_id into area_km2_uuid
@@ -22,11 +24,32 @@ begin
2224
return;
2325
end if;
2426

25-
update bivariate_indicators_metadata
26-
set max_res = (
27-
select max(h3_get_resolution(h3))
27+
with resolution_counts as (
28+
select
29+
h3_get_resolution(h3) as res,
30+
count(9) as cnt
2831
from stat_h3_transposed
2932
where indicator_uuid = x_numerator_uuid
33+
group by 1
34+
),
35+
global as (
36+
select max(res) as r from resolution_counts
37+
),
38+
coverage as (
39+
select max(res) as r from resolution_counts where cnt <= 1000
40+
)
41+
select global.r, coverage.r
42+
into global_max_res, coverage_max_res
43+
from global, coverage;
44+
45+
update bivariate_indicators_metadata
46+
set
47+
max_res = global_max_res,
48+
coverage_polygon = (
49+
select ST_Transform(ST_Union(h3_cell_to_boundary_geometry(h3)), 4326)
50+
from stat_h3_transposed
51+
where indicator_uuid = x_numerator_uuid
52+
and h3_get_resolution(h3) = coverage_max_res
3053
)
3154
where internal_id = x_numerator_uuid;
3255

0 commit comments

Comments
 (0)