2021-03-13 16:44:31 +02:00
|
|
|
/* Aggregates rivers by name and proximity. */
|
|
|
|
drop function if exists aggregate_rivers;
|
|
|
|
create function aggregate_rivers() returns table(osm_id bigint, name text, way geometry) as $$
|
|
|
|
declare
|
|
|
|
c record;
|
|
|
|
cc record;
|
|
|
|
changed boolean;
|
|
|
|
begin
|
2021-03-15 18:58:16 +02:00
|
|
|
while (select count(1) from aggregate_rivers_tmp) > 0 loop
|
|
|
|
select * from aggregate_rivers_tmp limit 1 into c;
|
|
|
|
delete from aggregate_rivers_tmp a where a.osm_id = c.osm_id;
|
2021-03-13 16:44:31 +02:00
|
|
|
changed = true;
|
|
|
|
while changed loop
|
|
|
|
changed = false;
|
2021-03-15 18:58:16 +02:00
|
|
|
for cc in (select * from aggregate_rivers_tmp a where a.name = c.name and st_dwithin(a.way, c.way, 500)) loop
|
2021-03-13 16:44:31 +02:00
|
|
|
c.way = st_linemerge(st_union(c.way, cc.way));
|
2021-03-15 18:58:16 +02:00
|
|
|
delete from aggregate_rivers_tmp a where a.osm_id = cc.osm_id;
|
2021-03-13 16:44:31 +02:00
|
|
|
changed = true;
|
|
|
|
end loop;
|
|
|
|
end loop; -- while changed
|
|
|
|
return query select c.osm_id, c.name, c.way;
|
2021-03-15 18:58:16 +02:00
|
|
|
end loop; -- count(1) from aggregate_rivers_tmp > 0
|
2021-03-13 16:44:31 +02:00
|
|
|
return;
|
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
2021-03-15 18:58:16 +02:00
|
|
|
create temporary table aggregate_rivers_tmp (osm_id bigint, name text, way geometry);
|
|
|
|
create index aggregate_rivers_tmp_id on aggregate_rivers_tmp(osm_id);
|
|
|
|
create index aggregate_rivers_tmp_gix on aggregate_rivers_tmp using gist(way) include(name);
|
2021-03-13 16:44:31 +02:00
|
|
|
|
2021-03-15 18:58:16 +02:00
|
|
|
insert into aggregate_rivers_tmp
|
2021-03-13 16:44:31 +02:00
|
|
|
select p.osm_id, p.name, p.way from planet_osm_line p
|
|
|
|
where waterway in ('river', 'stream', 'canal') and :where;
|
|
|
|
|
|
|
|
drop table if exists agg_rivers;
|
|
|
|
create table agg_rivers as (select * from aggregate_rivers());
|
2021-03-15 18:58:16 +02:00
|
|
|
drop table aggregate_rivers_tmp;
|