less conditionals in bend_attrs

This commit is contained in:
Motiejus Jakštys 2021-04-06 07:16:00 +03:00
parent 0d805d1be6
commit fdc7835535

View File

@ -265,28 +265,26 @@ drop function if exists bend_attrs;
drop type if exists t_bend_attrs; drop type if exists t_bend_attrs;
create type t_bend_attrs as ( create type t_bend_attrs as (
bend geometry, bend geometry,
area real, area real not null default 0,
cmp real, cmp real not null default 0,
adjsize real, adjsize real not null default 0,
baselinelength real baselinelength real not null default 0
); );
create function bend_attrs(bends geometry[], dbgname text default null) returns setof t_bend_attrs as $$ create function bend_attrs(bends geometry[], dbgname text default null) returns setof t_bend_attrs as $$
declare declare
fourpi constant real default 4*radians(180);
i int4; i int4;
fourpi real;
polygon geometry; polygon geometry;
bend geometry; bend geometry;
res t_bend_attrs; res t_bend_attrs;
begin begin
fourpi = 4*radians(180);
for i in 1..array_length(bends, 1) loop for i in 1..array_length(bends, 1) loop
res = null;
bend = bends[i]; bend = bends[i];
if st_numpoints(bend) < 3 then res = null;
polygon = null; res.bend = bend;
else if st_numpoints(bend) >= 3 then
select st_makepolygon(st_addpoint(bend, st_startpoint(bend))) into polygon; polygon = st_makepolygon(st_addpoint(bend, st_startpoint(bend)));
select st_distance(st_startpoint(bend), st_endpoint(bend)) into res.baselinelength; res.baselinelength = st_distance(st_startpoint(bend), st_endpoint(bend));
-- Compactness Index (cmp) is defined as "the ratio of the area of the -- 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 -- polygon over the circle whose circumference length is the same as the
-- length of the circumference of the polygon". I assume they meant the -- length of the circumference of the polygon". I assume they meant the
@ -295,13 +293,11 @@ begin
-- 2. get polygon perimeter = u. Pretend it's our circle's circumference. -- 2. get polygon perimeter = u. Pretend it's our circle's circumference.
-- 3. get A (area) of the circle from u: A = (u^2)/(4*pi) -- 3. get A (area) of the circle from u: A = (u^2)/(4*pi)
-- 4. divide P by A: cmp = P/A = P/((u^2)*4*pi) = 4*pi*P/u^2 -- 4. divide P by A: cmp = P/A = P/((u^2)*4*pi) = 4*pi*P/u^2
select st_area(polygon) into res.area; res.area = st_area(polygon);
select fourpi*res.area/(st_perimeter(polygon)^2) into res.cmp; res.cmp = fourpi*res.area/(st_perimeter(polygon)^2);
select bend into res.bend; if res.cmp > 0 then
res.adjsize = (res.area*(0.75/res.cmp));
end if; end if;
if res.area > 0 then
select (res.area*(0.75/res.cmp)) into res.adjsize;
end if; end if;
if dbgname is not null then if dbgname is not null then
@ -318,7 +314,9 @@ begin
) )
); );
end if; end if;
return next res; return next res;
end loop; end loop;
end; end;
$$ language plpgsql; $$ language plpgsql;