2021-02-13 17:05:16 +02:00
|
|
|
\set ON_ERROR_STOP on
|
|
|
|
SET plpgsql.extra_errors TO 'all';
|
|
|
|
|
2021-02-18 10:00:14 +02:00
|
|
|
drop function if exists detect_bends;
|
2021-02-18 07:59:24 +02:00
|
|
|
-- detect_bends detects bends using the inflection angles. It does not do corrections.
|
2021-02-18 10:00:14 +02:00
|
|
|
create or replace function detect_bends(line geometry, OUT bends geometry[]) as $$
|
2021-02-10 00:08:55 +02:00
|
|
|
declare
|
|
|
|
pi real;
|
|
|
|
p geometry;
|
|
|
|
p1 geometry;
|
|
|
|
p2 geometry;
|
|
|
|
p3 geometry;
|
2021-02-18 10:00:14 +02:00
|
|
|
bend 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);
|
|
|
|
|
2021-02-20 14:23:21 +02:00
|
|
|
-- the last vertex is iterated over twice, because the algorithm uses 3 vertices
|
|
|
|
-- to calculate the angle between them.
|
|
|
|
--
|
|
|
|
-- Given 3 vertices p1, p2, p3:
|
|
|
|
--
|
|
|
|
-- p1___ ...
|
|
|
|
-- /
|
|
|
|
-- ..._____/
|
|
|
|
-- p3 p2
|
|
|
|
--
|
|
|
|
-- This loop will use p1 as the head vertex, p2 will be the measured angle,
|
|
|
|
-- and p3 will be trailing. The line that will be added to the bend will
|
|
|
|
-- always be [p3,p2].
|
|
|
|
-- So once the p1 becomes the last vertex, the loop terminates, and the
|
|
|
|
-- [p2,p1] line will not have a chance to be added. So the loop adds the last
|
|
|
|
-- vertex twice, so it has a chance to become p2, and be added to the bend.
|
|
|
|
--
|
2021-02-20 14:10:34 +02:00
|
|
|
for p in (
|
|
|
|
(select (dp).geom from st_dumppoints(line) as dp order by (dp).path[1] asc)
|
|
|
|
union all
|
|
|
|
(select (dp).geom from st_dumppoints(line) as dp order by (dp).path[1] desc limit 1)
|
|
|
|
) loop
|
2021-02-10 00:08:55 +02:00
|
|
|
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));
|
|
|
|
|
2021-02-20 14:10:34 +02:00
|
|
|
if bend is null then
|
|
|
|
bend = st_makeline(p3, p2);
|
|
|
|
else
|
|
|
|
bend = st_linemerge(st_union(bend, st_makeline(p3, p2)));
|
|
|
|
end if;
|
2021-02-13 17:05:16 +02:00
|
|
|
|
|
|
|
if prev_sign + cur_sign = 0 then
|
|
|
|
if bend is not null then
|
2021-02-18 10:00:14 +02:00
|
|
|
bends = bends || bend;
|
2021-02-13 17:05:16 +02:00
|
|
|
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
|
|
|
|
2021-02-20 14:10:34 +02:00
|
|
|
-- the last bend may be lost if there is no "final" inflection angle. Add it.
|
|
|
|
if (select count(1) from ((select st_dumppoints(bend) as a)) b) >= 2 then
|
2021-02-18 10:00:14 +02:00
|
|
|
bends = bends || bend;
|
2021-02-18 07:08:04 +02:00
|
|
|
end if;
|
2021-02-10 00:08:55 +02:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2021-02-18 07:59:24 +02:00
|
|
|
|
2021-02-18 13:45:53 +02:00
|
|
|
-- fix_gentle_inflections moves bend endpoints following "Gentle Inflection at
|
|
|
|
-- End of a Bend" section.
|
|
|
|
create or replace function fix_gentle_inflections(INOUT bends geometry[]) as $$
|
2021-02-18 07:59:24 +02:00
|
|
|
begin
|
|
|
|
end
|
|
|
|
$$ language plpgsql;
|