From 6e5f66ee6b408608c59b823484c0af9be4b3644f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Thu, 25 Mar 2021 11:34:30 +0200 Subject: [PATCH] easier debugging --- IV/Makefile | 2 +- IV/tests-integration.sql | 2 +- IV/tests.sql | 2 +- IV/wm.sql | 98 ++++++++++++++++++---------------------- 4 files changed, 46 insertions(+), 58 deletions(-) diff --git a/IV/Makefile b/IV/Makefile index fe86488..a48ed3d 100644 --- a/IV/Makefile +++ b/IV/Makefile @@ -16,7 +16,7 @@ clean: .PHONY: clean-tables clean-tables: - for t in $$(./db -c '\dt' | awk '/demo|integ/{print $$3}'); do \ + for t in $$(./db -c '\dt' | awk '/demo|debug|integ/{print $$3}'); do \ ./db -c "drop table $$t"; \ done diff --git a/IV/tests-integration.sql b/IV/tests-integration.sql index 15f103e..a9fbfa9 100644 --- a/IV/tests-integration.sql +++ b/IV/tests-integration.sql @@ -2,4 +2,4 @@ drop table if exists agg_rivers_wm; create table agg_rivers_wm (name text, way geometry); -insert into agg_rivers_wm (name, way) select name, ST_SimplifyWM_DEBUG(way) from agg_rivers where name='Nemunas'; +insert into agg_rivers_wm (name, way) select name, ST_SimplifyWM(way, true) from agg_rivers where name='Nemunas'; diff --git a/IV/tests.sql b/IV/tests.sql index 19c6a2c..d3c41f6 100644 --- a/IV/tests.sql +++ b/IV/tests.sql @@ -56,7 +56,7 @@ insert into demo_selfcrossing3 select name, generate_subscripts(ways, 1), unnest insert into figures (name, way) values ('multi-island',ST_GeomFromText('MULTILINESTRING((-15 10,-10 10,-5 11,0 11,5 11,10 10,11 9,13 10,15 9),(-5 11,-1 15,0 16,1 15,5 11))')); drop table if exists demo_wm; create table demo_wm (name text, i bigint, way geometry); -insert into demo_wm (name, way) select name, ST_SimplifyWM_DEBUG(way) from figures where name='multi-island'; +insert into demo_wm (name, way) select name, ST_SimplifyWM(way, true) from figures where name='multi-island'; do $$ declare diff --git a/IV/wm.sql b/IV/wm.sql index 17c3aa6..17a44c8 100644 --- a/IV/wm.sql +++ b/IV/wm.sql @@ -270,8 +270,9 @@ $$ language plpgsql; -- "Line Generalization Based on Analysis of Shape Characteristics" algorithm, -- 1998. drop function if exists ST_SimplifyWM; -create function ST_SimplifyWM(geom geometry) returns geometry as $$ +create function ST_SimplifyWM(geom geometry, dbg boolean default false) returns geometry as $$ declare + dbg_stage integer; line geometry; lines geometry[]; bends geometry[]; @@ -287,68 +288,55 @@ begin raise 'Unknown geometry type %', l_type; end if; + if dbg then + drop table if exists debug_wm; + create table debug_wm(name text, i bigint, way geometry); + end if; + + dbg_stage = 1; foreach line in array lines loop mutated = true; while mutated loop + if dbg then + insert into debug_wm (name, way) values( + dbg_stage || 'afigures', + line + ); + end if; + bends = detect_bends(line); + + if dbg then + insert into debug_wm(name, i, way) values( + dbg_stage || 'bbends', + generate_subscripts(bends, 1), + unnest(bends) + ); + end if; + bends = fix_gentle_inflections(bends); + + if dbg then + insert into debug_wm(name, i, way) values( + dbg_stage || 'cinflections', + generate_subscripts(bends, 1), + unnest(bends) + ); + end if; + select * from self_crossing(bends) into bends, mutated; + + if dbg then + insert into debug_wm(name, i, way) values( + dbg_stage || 'dcrossings', + generate_subscripts(bends, 1), + unnest(bends) + ); + end if; + line = st_linemerge(st_union(bends)); end loop; - end loop; - - if l_type = 'ST_LineString' then - return st_linemerge(st_union(bends)); - elseif l_type = 'ST_MultiLineString' then - return st_union(bends); - end if; -end -$$ language plpgsql; - -drop function if exists ST_SimplifyWM_DEBUG; -create function ST_SimplifyWM_DEBUG(geom geometry) returns geometry as $$ -declare - i integer; - line geometry; - lines geometry[]; - bend geometry; - bends geometry[]; - mutated boolean; - l_type text; -begin - l_type = st_geometrytype(geom); - if l_type = 'ST_LineString' then - lines = array[geom]; - elseif l_type = 'ST_MultiLineString' then - lines = array((select a.geom from st_dump(geom) a order by path[1] asc)); - else - raise 'Unsupported geometry type %', l_type; - end if; - - - i = 1; - foreach line in array lines loop - mutated = true; - while mutated loop - execute format('create table if not exists integ_%safigures (way geometry)', i); - -- if anyone has suggestions how to insert a variable to a table without - -- such hackery, I'll be glad to know - raise notice 'inserting: %', st_astext(unnest(array[line])); - execute format('insert into integ_%safigures select $1;', i) using (select unnest(array[line])); - raise notice 's: %', array((select st_summary(way) from integ_1afigures)); - bends = detect_bends(line); - - execute format('create table if not exists integ_%sbbends (i bigint, way geometry)', i); - execute format('insert into integ_%sbbends (i, way) select generate_subscripts($1, 1), unnest($1)', i) using bends; - bends = fix_gentle_inflections(bends); - execute format('create table if not exists integ_%scinflections (i bigint, way geometry)', i); - execute format('insert into integ_%scinflections (i, way) select generate_subscripts($1, 1), unnest($1)', i) using bends; - select * from self_crossing(bends) into bends, mutated; - execute format('create table if not exists integ_%sdselfcrossing (i bigint, way geometry)', i); - execute format('insert into integ_%sdselfcrossing (i, way) select generate_subscripts($1, 1), unnest($1)', i) using bends; - line = st_linemerge(st_union(bends)); - end loop; - i = i + 1; + dbg_stage = dbg_stage + 1; end loop; if l_type = 'ST_LineString' then