From 15aa42afdff2742b507e726bc5e1b65436dd0b08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Motiejus=20Jak=C5=A1tys?= Date: Sat, 13 Feb 2021 17:05:16 +0200 Subject: [PATCH] bend detection --- IV/bend.sql | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/IV/bend.sql b/IV/bend.sql index 272f8ae..a656866 100644 --- a/IV/bend.sql +++ b/IV/bend.sql @@ -1,3 +1,6 @@ +\set ON_ERROR_STOP on +SET plpgsql.extra_errors TO 'all'; + create or replace function detect_bends(line geometry) returns table(bend geometry) as $$ /* for each bend, should return: - size (area) @@ -9,6 +12,8 @@ declare p1 geometry; p2 geometry; p3 geometry; + prev_sign int4; + cur_sign int4; begin pi = radians(180); @@ -16,8 +21,20 @@ begin p3 = p2; p2 = p1; p1 = p; - if p3 is null then continue; end if; - raise notice 'ANGLE %', degrees(pi - st_angle(p1, p2, p2, p3)); + if p3 is null then + continue; + end if; + cur_sign = sign(pi - st_angle(p1, p2, p2, p3)); + + bend = st_linemerge(st_union(bend, st_makeline(p3, p2))); + + if prev_sign + cur_sign = 0 then + if bend is not null then + return next; + end if; + bend = st_makeline(p3, p2); + end if; + prev_sign = cur_sign; end loop; end $$ language plpgsql;