Mercurial > gemma
view schema/updates/1466/02.fix_bn_areas.sql @ 5513:68358e4603c8
Use current axis only for calculating bottleneck areas
This is a fixup of rev. cf25b23e3eec, which introduced historic data
for the waterway axis but missed to take this into account in the
calculation of bottleneck areas, leading to sometimes excessive runtime
and bad results due to multiple (almost) equal axis geometries being
considered as candidates in the bottleneck stretch.
The database migration tries to recalculate all bottleneck areas, while
some might fail that did not fail on import. A warning message is emitted
for these and the area is left untouched.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Tue, 19 Oct 2021 13:12:39 +0200 |
parents | |
children |
line wrap: on
line source
DO $$ DECLARE bns CURSOR FOR SELECT * FROM waterway.bottlenecks FOR UPDATE; call_stack text; BEGIN FOR cur_bn IN bns LOOP BEGIN UPDATE waterway.bottlenecks bn SET area = ISRSrange_area( ISRSrange_axis(stretch, COALESCE(( -- Guess tolerance from the last successful -- bottleneck import owned by a waterway_admin of -- the country matching the bottleneck_id SELECT DISTINCT ON (usr.country) CAST(substring(msg FROM '((\d*\.)?\d+)$') AS float) FROM import.import_logs log JOIN import.imports imp ON log.import_id = imp.id JOIN users.list_users usr USING (username) WHERE starts_with(log.msg, 'Tolerance used to snap waterway axis:') AND imp.kind = 'bn' AND imp.state IN('accepted', 'reviewed') AND usr.rolname = 'waterway_admin' AND usr.country = substring( bn.bottleneck_id FROM 1 FOR 2) ORDER BY usr.country, imp.changed DESC), -- Use default tolerance if originally used cannot -- be determined 5)), (SELECT ST_Collect(CAST(area AS geometry)) FROM waterway.waterway_area)) WHERE CURRENT OF bns; EXCEPTION WHEN no_data_found THEN GET STACKED DIAGNOSTICS call_stack = PG_EXCEPTION_CONTEXT; RAISE WARNING '% (%): %, CONTEXT: %', cur_bn.bottleneck_id, cur_bn.validity, SQLERRM, call_stack; END; END LOOP; END $$;