simplify debugging

This commit is contained in:
Motiejus Jakštys 2021-05-19 22:57:46 +03:00 committed by Motiejus Jakštys
parent a68cebcb4d
commit 773bc97759
2 changed files with 35 additions and 23 deletions

View File

@ -11,7 +11,7 @@ begin
end $$ LANGUAGE plpgsql;
drop table if exists debug_wm;
create table debug_wm(name text, way geometry, props json);
create table debug_wm(stage text, dbgname text, i bigint, j bigint, way geometry, props json);
drop table if exists figures;
create table figures (name text, way geometry);
@ -34,7 +34,7 @@ insert into figures (name, way) values ('multi-island',ST_GeomFromText('MULTILIN
-- DETECT BENDS
drop table if exists bends, demo_bends1;
create table bends (name text, ways geometry[]);
insert into bends select name, detect_bends(way) from figures;
insert into bends select name, detect_bends(way, name) from figures;
create table demo_bends1 (name text, i bigint, way geometry);
insert into demo_bends1 select name, generate_subscripts(ways, 1), unnest(ways) from bends;
@ -57,14 +57,14 @@ do $$
declare
recs t_bend_attrs[];
begin
select array(select bend_attrs(ways, true) from inflections) into recs;
select array(select bend_attrs(ways, name) from inflections) into recs;
end
$$ language plpgsql;
-- COMBINED
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(way, true) from figures;
insert into demo_wm (name, way) select name, ST_SimplifyWM(way, name) from figures;
do $$
declare

50
wm.sql
View File

@ -3,7 +3,7 @@ SET plpgsql.extra_errors TO 'all';
-- detect_bends detects bends using the inflection angles. No corrections.
drop function if exists detect_bends;
create function detect_bends(line geometry, OUT bends geometry[]) as $$
create function detect_bends(line geometry, dbgname text default null, OUT bends geometry[]) as $$
declare
pi constant real default radians(180);
p geometry;
@ -266,7 +266,7 @@ create type t_bend_attrs as (
adjsize real,
baselinelength real
);
create function bend_attrs(bends geometry[], dbg boolean default false) returns setof t_bend_attrs as $$
create function bend_attrs(bends geometry[], dbgname text default null) returns setof t_bend_attrs as $$
declare
i int4;
fourpi real;
@ -300,9 +300,10 @@ begin
if res.area > 0 then
select (res.area*(0.75/res.cmp)) into res.adjsize;
end if;
if dbg then
insert into debug_wm (name, way, props) values(
'bend_attrs_' || i,
if dbgname is not null then
insert into debug_wm (stage, dbgname, way, props) values(
'ebendattrs',
dbgname,
bend,
json_build_object(
'area', res.area,
@ -321,7 +322,7 @@ $$ language plpgsql;
-- "Line Generalization Based on Analysis of Shape Characteristics" algorithm,
-- 1998.
drop function if exists ST_SimplifyWM;
create function ST_SimplifyWM(geom geometry, dbg boolean default false) returns geometry as $$
create function ST_SimplifyWM(geom geometry, dbgname text default null) returns geometry as $$
declare
dbg_stage integer;
i integer;
@ -344,36 +345,47 @@ begin
mutated = true;
dbg_stage = 1;
while mutated loop
if dbg then
insert into debug_wm (name, way) values(
dbg_stage || 'afigures_' || i,
if dbgname is not null then
insert into debug_wm (stage, dbgname, i, way) values(
'afigures',
dbgname,
i,
lines[i]
);
end if;
bends = detect_bends(lines[i]);
if dbg then
insert into debug_wm(name, way) values(
dbg_stage || 'bbends_' || i || '_' || generate_subscripts(bends, 1),
if dbgname is not null then
insert into debug_wm(stage, dbgname, i, j, way) values(
'bbends',
dbgname,
i,
generate_subscripts(bends, 1),
unnest(bends)
);
end if;
bends = fix_gentle_inflections(bends);
if dbg then
insert into debug_wm(name, way) values(
dbg_stage || 'cinflections' || i || '_' || generate_subscripts(bends, 1),
if dbgname is not null then
insert into debug_wm(stage, dbgname, i, j, way) values(
'cinflections',
dbgname,
i,
generate_subscripts(bends, 1),
unnest(bends)
);
end if;
select * from self_crossing(bends) into bends, mutated;
if dbg then
insert into debug_wm(name, way) values(
dbg_stage || 'dcrossings' || i || '_' || generate_subscripts(bends, 1),
if dbgname is not null then
insert into debug_wm(stage, dbgname, i, j, way) values(
'dcrossings',
dbgname,
i,
generate_subscripts(bends, 1),
unnest(bends)
);
end if;
@ -385,7 +397,7 @@ begin
end if;
-- self-crossing mutations are done, calculate bend properties
perform bend_attrs(bends, true);
perform bend_attrs(bends, dbgname);
end loop;
end loop;