2021-05-19 22:57:46 +03:00
|
|
|
/* Aggregates rivers by name and proximity. */
|
|
|
|
drop function if exists aggregate_rivers;
|
2021-05-19 22:57:49 +03:00
|
|
|
create function aggregate_rivers() returns table(
|
2021-05-19 22:57:50 +03:00
|
|
|
id bigint,
|
2021-05-19 22:57:49 +03:00
|
|
|
name text,
|
|
|
|
way geometry
|
|
|
|
) as $$
|
2021-05-19 22:57:46 +03:00
|
|
|
declare
|
|
|
|
c record;
|
|
|
|
cc record;
|
|
|
|
changed boolean;
|
|
|
|
begin
|
2021-05-19 22:57:48 +03:00
|
|
|
while (select count(1) from wm_rivers_tmp) > 0 loop
|
|
|
|
select * from wm_rivers_tmp limit 1 into c;
|
2021-05-19 22:57:50 +03:00
|
|
|
delete from wm_rivers_tmp a where a.id = c.id;
|
2021-05-19 22:57:46 +03:00
|
|
|
changed = true;
|
|
|
|
while changed loop
|
|
|
|
changed = false;
|
2021-05-19 22:57:49 +03:00
|
|
|
for cc in (
|
|
|
|
select * from wm_rivers_tmp a where
|
|
|
|
a.name = c.name and
|
|
|
|
st_dwithin(a.way, c.way, 500)
|
|
|
|
) loop
|
2021-05-19 22:57:46 +03:00
|
|
|
c.way = st_linemerge(st_union(c.way, cc.way));
|
2021-05-19 22:57:50 +03:00
|
|
|
delete from wm_rivers_tmp a where a.id = cc.id;
|
2021-05-19 22:57:46 +03:00
|
|
|
changed = true;
|
|
|
|
end loop;
|
|
|
|
end loop; -- while changed
|
2021-05-19 22:57:50 +03:00
|
|
|
return query select c.id, c.name, c.way;
|
2021-05-19 22:57:48 +03:00
|
|
|
end loop; -- count(1) from wm_rivers_tmp > 0
|
2021-05-19 22:57:46 +03:00
|
|
|
return;
|
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
2021-05-19 22:57:48 +03:00
|
|
|
drop index if exists wm_rivers_tmp_id;
|
|
|
|
drop index if exists wm_rivers_tmp_gix;
|
|
|
|
drop table if exists wm_rivers_tmp;
|
2021-05-19 22:57:50 +03:00
|
|
|
create temporary table wm_rivers_tmp (id bigint, name text, way geometry);
|
|
|
|
create index wm_rivers_tmp_id on wm_rivers_tmp(id);
|
2021-05-19 22:57:48 +03:00
|
|
|
create index wm_rivers_tmp_gix on wm_rivers_tmp using gist(way) include(name);
|
2021-05-19 22:57:46 +03:00
|
|
|
|
2021-05-19 22:57:48 +03:00
|
|
|
insert into wm_rivers_tmp
|
2021-05-19 22:57:50 +03:00
|
|
|
select p.gid as id, p.vardas as name, p.geom as way from hidro_l p;
|
2021-05-19 22:57:46 +03:00
|
|
|
|
2021-05-19 22:57:48 +03:00
|
|
|
drop table if exists wm_rivers;
|
2021-05-19 22:57:49 +03:00
|
|
|
create table wm_rivers as (
|
|
|
|
select * from aggregate_rivers() where st_length(way) >= 50000
|
|
|
|
);
|
2021-05-19 22:57:48 +03:00
|
|
|
drop table wm_rivers_tmp;
|