calculate adjusted size too

This commit is contained in:
Motiejus Jakštys 2021-05-19 22:57:46 +03:00 committed by Motiejus Jakštys
parent 711c050d53
commit 863b23f2ef
2 changed files with 42 additions and 21 deletions

View File

@ -11,7 +11,7 @@ begin
end $$ LANGUAGE plpgsql; end $$ LANGUAGE plpgsql;
drop table if exists debug_wm; drop table if exists debug_wm;
create table debug_wm(section text, name text, way geometry); create table debug_wm(name text, way geometry, props json);
drop table if exists figures; drop table if exists figures;
create table figures (name text, way geometry); create table figures (name text, way geometry);
@ -53,14 +53,14 @@ create table demo_selfcrossing3 (name text, i bigint, way geometry);
insert into demo_selfcrossing3 select name, generate_subscripts(ways, 1), unnest(ways) from selfcrossing; insert into demo_selfcrossing3 select name, generate_subscripts(ways, 1), unnest(ways) from selfcrossing;
-- BEND ATTRS -- BEND ATTRS
drop table if exists bendattrs; do $$
create table bendattrs (way geometry, area real, cmp real); begin perform bend_attrs(ways, true) from inflections; end
insert into bendattrs (way, area, cmp) select (bend_attrs(ways, true)).* from inflections; $$ language plpgsql;
-- COMBINED -- COMBINED
drop table if exists demo_wm; drop table if exists demo_wm;
create table demo_wm (name text, i bigint, way geometry); create table demo_wm (name text, i bigint, way geometry);
insert into demo_wm (name, way) select name, ST_SimplifyWM(way, true) from figures where name='multi-island'; insert into demo_wm (name, way) select name, ST_SimplifyWM(way, true) from figures;
do $$ do $$
declare declare

49
wm.sql
View File

@ -267,14 +267,35 @@ end
$$ language plpgsql; $$ language plpgsql;
drop function if exists bend_attrs; drop function if exists bend_attrs;
create function bend_attrs(bends geometry[], dbg boolean default false) returns table(polygon geometry, area real, cmp real) as $$ create function bend_attrs(bends geometry[], dbg boolean default false) returns table(bend geometry, area real, cmp real, adjsize real) as $$
declare declare
i int4; i int4;
fourpi real;
polygon geometry;
begin begin
fourpi = 4*radians(180);
for i in 1..array_length(bends, 1) loop for i in 1..array_length(bends, 1) loop
select st_makepolygon(st_addpoint(bends[i], st_startpoint(bends[i]))) into polygon; bend = bends[i];
select st_makepolygon(st_addpoint(bend, st_startpoint(bend))) into polygon;
-- Compactness Index (cmp) is defined as "the ratio of the area of the
-- polygon over the circle whose circumference length is the same as the
-- length of the circumference of the polygon". I assume they meant the
-- area of the circle. So here goes:
-- 1. get polygon area P.
-- 2. get polygon perimeter = u. Pretend it's our circle's circumference.
-- 3. get A (area) of the circle from circumference u := (u^2)/(4*pi)
-- 4. divide area by A: cmp = P/((u^2)*4*pi) = 4*pi*P/u^2
select st_area(polygon) into area;
select fourpi*area/(st_perimeter(polygon)^2) into cmp;
if cmp > 0 then
select (area*(0.75/cmp)) into adjsize;
if dbg then if dbg then
insert into debug_wm (section, name, way) values('bend_attrs', i, polygon); insert into debug_wm (name, way, props) values(
'bend_attrs_' || i,
polygon,
json_build_object('area', area, 'cmp', cmp, 'adjsize', adjsize)
);
end if;
end if; end if;
return next; return next;
end loop; end loop;
@ -304,15 +325,12 @@ begin
raise 'Unknown geometry type %', l_type; raise 'Unknown geometry type %', l_type;
end if; end if;
for i in 1..array_length(lines, 1) loop for i in 1..array_length(lines, 1) loop
mutated = true; mutated = true;
dbg_stage = 1; dbg_stage = 1;
while mutated loop while mutated loop
if dbg then if dbg then
insert into debug_wm (section, name, way) values( insert into debug_wm (name, way) values(
'simplifywm',
dbg_stage || 'afigures_' || i, dbg_stage || 'afigures_' || i,
lines[i] lines[i]
); );
@ -321,8 +339,7 @@ begin
bends = detect_bends(lines[i]); bends = detect_bends(lines[i]);
if dbg then if dbg then
insert into debug_wm(section, name, way) values( insert into debug_wm(name, way) values(
'simplifywm',
dbg_stage || 'bbends_' || i || '_' || generate_subscripts(bends, 1), dbg_stage || 'bbends_' || i || '_' || generate_subscripts(bends, 1),
unnest(bends) unnest(bends)
); );
@ -331,8 +348,7 @@ begin
bends = fix_gentle_inflections(bends); bends = fix_gentle_inflections(bends);
if dbg then if dbg then
insert into debug_wm(section, name, way) values( insert into debug_wm(name, way) values(
'simplifywm',
dbg_stage || 'cinflections' || i || '_' || generate_subscripts(bends, 1), dbg_stage || 'cinflections' || i || '_' || generate_subscripts(bends, 1),
unnest(bends) unnest(bends)
); );
@ -341,17 +357,22 @@ begin
select * from self_crossing(bends) into bends, mutated; select * from self_crossing(bends) into bends, mutated;
if dbg then if dbg then
insert into debug_wm(section, name, way) values( insert into debug_wm(name, way) values(
'simplifywm',
dbg_stage || 'dcrossings' || i || '_' || generate_subscripts(bends, 1), dbg_stage || 'dcrossings' || i || '_' || generate_subscripts(bends, 1),
unnest(bends) unnest(bends)
); );
end if; end if;
if mutated then
lines[i] = st_linemerge(st_union(bends)); lines[i] = st_linemerge(st_union(bends));
dbg_stage = dbg_stage + 1; dbg_stage = dbg_stage + 1;
continue;
end if;
-- self-crossing mutations are done, calculate bend properties
perform bend_attrs(bends, true);
end loop; end loop;
end loop; end loop;
if l_type = 'ST_LineString' then if l_type = 'ST_LineString' then