bends are arrays now

This commit is contained in:
Motiejus Jakštys 2021-02-18 10:00:14 +02:00
parent 8aa51d9304
commit 50a2ebfb1e
2 changed files with 12 additions and 5 deletions

View File

@ -18,7 +18,12 @@ insert into figures (name, way) values ('fig3-1', ST_GeomFromText('LINESTRING(0
insert into figures (name, way) values ('fig5', ST_GeomFromText('LINESTRING(0 39,19 52,27 77,26 104,41 115,49 115,65 103,65 75,53 45,63 15,91 0,91 0)')); insert into figures (name, way) values ('fig5', ST_GeomFromText('LINESTRING(0 39,19 52,27 77,26 104,41 115,49 115,65 103,65 75,53 45,63 15,91 0,91 0)'));
do $$ do $$
declare
bends geometry[];
begin begin
perform assert_equals(3::bigint, (select count(1) from detect_bends((select way from figures where name='fig3')))); perform assert_equals(3, array_length(detect_bends((select way from figures where name='fig3')), 1));
perform assert_equals(3::bigint, (select count(1) from detect_bends((select way from figures where name='fig3-1')))); perform assert_equals(3, array_length(detect_bends((select way from figures where name='fig3-1')), 1));
select detect_bends((select way from figures where name='fig5')) into bends;
perform assert_equals(2, array_length(bends, 1));
end $$ language plpgsql; end $$ language plpgsql;

View File

@ -1,14 +1,16 @@
\set ON_ERROR_STOP on \set ON_ERROR_STOP on
SET plpgsql.extra_errors TO 'all'; SET plpgsql.extra_errors TO 'all';
drop function if exists detect_bends;
-- detect_bends detects bends using the inflection angles. It does not do corrections. -- detect_bends detects bends using the inflection angles. It does not do corrections.
create or replace function detect_bends(line geometry) returns table(bend geometry) as $$ create or replace function detect_bends(line geometry, OUT bends geometry[]) as $$
declare declare
pi real; pi real;
p geometry; p geometry;
p1 geometry; p1 geometry;
p2 geometry; p2 geometry;
p3 geometry; p3 geometry;
bend geometry;
prev_sign int4; prev_sign int4;
cur_sign int4; cur_sign int4;
begin begin
@ -27,7 +29,7 @@ begin
if prev_sign + cur_sign = 0 then if prev_sign + cur_sign = 0 then
if bend is not null then if bend is not null then
return next; bends = bends || bend;
end if; end if;
bend = st_makeline(p3, p2); bend = st_makeline(p3, p2);
end if; end if;
@ -38,7 +40,7 @@ begin
-- to avoid that, return the last bend if the last accumulation has >3 -- to avoid that, return the last bend if the last accumulation has >3
-- vertices. -- vertices.
if (select count(1) from ((select st_dumppoints(bend) as a)) b) >= 3 then if (select count(1) from ((select st_dumppoints(bend) as a)) b) >= 3 then
return next; bends = bends || bend;
end if; end if;
end end
$$ language plpgsql; $$ language plpgsql;