stud/IV/wm.sql

48 lines
1.1 KiB
MySQL
Raw Normal View History

2021-02-13 17:05:16 +02:00
\set ON_ERROR_STOP on
SET plpgsql.extra_errors TO 'all';
2021-02-10 00:08:55 +02:00
create or replace function detect_bends(line geometry) returns table(bend geometry) as $$
/* for each bend, should return:
- size (area)
- shape (cmp, compactness index)
*/
declare
pi real;
p geometry;
p1 geometry;
p2 geometry;
p3 geometry;
2021-02-13 17:05:16 +02:00
prev_sign int4;
cur_sign int4;
2021-02-10 00:08:55 +02:00
begin
pi = radians(180);
for p in (select (dp).geom from st_dumppoints(line) as dp) loop
p3 = p2;
p2 = p1;
p1 = p;
2021-02-13 17:05:16 +02:00
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;
2021-02-10 00:08:55 +02:00
end loop;
2021-02-18 07:08:04 +02:00
-- the last bend may be lost if there is no "final" inflection angle.
-- to avoid that, return the last bend if the last accumulation has >3
-- vertices.
if (select count(1) from ((select st_dumppoints(bend) as a)) b) >= 3 then
return next;
end if;
2021-02-10 00:08:55 +02:00
end
$$ language plpgsql;