2021-05-19 22:57:47 +03:00
|
|
|
\set ON_ERROR_STOP on
|
|
|
|
SET plpgsql.extra_errors TO 'all';
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- wm_detect_bends detects bends using the inflection angles. No corrections.
|
|
|
|
drop function if exists wm_detect_bends;
|
|
|
|
create function wm_detect_bends(
|
2021-05-19 22:57:47 +03:00
|
|
|
line geometry,
|
|
|
|
dbgname text default null,
|
2021-05-19 22:57:49 +03:00
|
|
|
dbggen integer default null,
|
2021-05-19 22:57:47 +03:00
|
|
|
OUT bends geometry[]
|
|
|
|
) as $$
|
2021-05-19 22:57:45 +03:00
|
|
|
declare
|
|
|
|
p geometry;
|
|
|
|
p1 geometry;
|
|
|
|
p2 geometry;
|
|
|
|
p3 geometry;
|
2021-05-19 22:57:45 +03:00
|
|
|
bend geometry;
|
2021-05-19 22:57:45 +03:00
|
|
|
prev_sign int4;
|
|
|
|
cur_sign int4;
|
2021-05-19 22:57:47 +03:00
|
|
|
l_type text;
|
2021-05-19 22:57:48 +03:00
|
|
|
dbgpolygon geometry;
|
2021-05-19 22:57:45 +03:00
|
|
|
begin
|
2021-05-19 22:57:47 +03:00
|
|
|
l_type = st_geometrytype(line);
|
|
|
|
if l_type != 'ST_LineString' then
|
|
|
|
raise 'This function works with ST_LineString, got %', l_type;
|
|
|
|
end if;
|
2021-05-19 22:57:47 +03:00
|
|
|
|
2021-05-19 22:57:46 +03:00
|
|
|
-- The last vertex is iterated over twice, because the algorithm uses 3
|
2021-05-19 22:57:46 +03:00
|
|
|
-- vertices to calculate the angle between them.
|
2021-05-19 22:57:45 +03:00
|
|
|
--
|
|
|
|
-- Given 3 vertices p1, p2, p3:
|
|
|
|
--
|
2021-05-19 22:57:45 +03:00
|
|
|
-- p1___ ...
|
|
|
|
-- /
|
|
|
|
-- ... _____/
|
|
|
|
-- p3 p2
|
2021-05-19 22:57:45 +03:00
|
|
|
--
|
2021-05-19 22:57:45 +03:00
|
|
|
-- When looping over the line, p1 will be head (lead) 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].
|
2021-05-19 22:57:45 +03:00
|
|
|
-- 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-05-19 22:57:47 +03:00
|
|
|
for p in
|
|
|
|
(select geom from st_dumppoints(line) order by path[1] asc)
|
|
|
|
union all
|
|
|
|
(select geom from st_dumppoints(line) order by path[1] desc limit 1)
|
|
|
|
loop
|
2021-05-19 22:57:45 +03:00
|
|
|
p3 = p2;
|
|
|
|
p2 = p1;
|
|
|
|
p1 = p;
|
2021-05-19 22:57:45 +03:00
|
|
|
continue when p3 is null;
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
cur_sign = sign(pi() - st_angle(p1, p2, p2, p3));
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:45 +03: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-05-19 22:57:45 +03:00
|
|
|
|
|
|
|
if prev_sign + cur_sign = 0 then
|
|
|
|
if bend is not null then
|
2021-05-19 22:57:45 +03:00
|
|
|
bends = bends || bend;
|
2021-05-19 22:57:45 +03:00
|
|
|
end if;
|
|
|
|
bend = st_makeline(p3, p2);
|
|
|
|
end if;
|
|
|
|
prev_sign = cur_sign;
|
2021-05-19 22:57:45 +03:00
|
|
|
end loop;
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:45 +03:00
|
|
|
-- the last line may be lost if there is no "final" inflection angle. Add it.
|
|
|
|
if (select count(1) >= 2 from st_dumppoints(bend)) then
|
2021-05-19 22:57:45 +03:00
|
|
|
bends = bends || bend;
|
2021-05-19 22:57:45 +03:00
|
|
|
end if;
|
2021-05-19 22:57:47 +03:00
|
|
|
|
|
|
|
if dbgname is not null then
|
|
|
|
for i in 1..array_length(bends, 1) loop
|
|
|
|
insert into wm_debug(stage, name, gen, nbend, way) values(
|
2021-05-19 22:57:49 +03:00
|
|
|
'bbends', dbgname, dbggen, i, bends[i]);
|
2021-05-19 22:57:48 +03:00
|
|
|
|
|
|
|
dbgpolygon = null;
|
|
|
|
if st_npoints(bends[i]) >= 3 then
|
2021-05-19 22:57:49 +03:00
|
|
|
dbgpolygon = st_makepolygon(
|
2021-05-19 22:57:49 +03:00
|
|
|
st_addpoint(bends[i],
|
|
|
|
st_startpoint(bends[i]))
|
2021-05-19 22:57:49 +03:00
|
|
|
);
|
2021-05-19 22:57:48 +03:00
|
|
|
end if;
|
2021-05-19 22:57:47 +03:00
|
|
|
insert into wm_debug(stage, name, gen, nbend, way) values(
|
2021-05-19 22:57:49 +03:00
|
|
|
'bbends-polygon', dbgname, dbggen, i, dbgpolygon);
|
2021-05-19 22:57:47 +03:00
|
|
|
end loop;
|
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- wm_fix_gentle_inflections moves bend endpoints following "Gentle Inflection
|
|
|
|
-- at End of a Bend" section.
|
2021-05-19 22:57:45 +03:00
|
|
|
--
|
|
|
|
-- The text does not specify how many vertices can be "adjusted"; it can
|
|
|
|
-- equally be one or many. This function is adjusting many, as long as the
|
2021-05-19 22:57:50 +03:00
|
|
|
-- cumulative inflection angle is small (see variable below).
|
2021-05-19 22:57:45 +03:00
|
|
|
--
|
|
|
|
-- The implementation could be significantly optimized to avoid `st_reverse`
|
2021-05-19 22:57:49 +03:00
|
|
|
-- and array reversals, trading for complexity in wm_fix_gentle_inflections1.
|
2021-05-19 22:57:49 +03:00
|
|
|
drop function if exists wm_fix_gentle_inflections;
|
|
|
|
create function wm_fix_gentle_inflections(
|
2021-05-19 22:57:47 +03:00
|
|
|
INOUT bends geometry[],
|
|
|
|
dbgname text default null,
|
2021-05-19 22:57:49 +03:00
|
|
|
dbggen integer default null
|
2021-05-19 22:57:47 +03:00
|
|
|
) as $$
|
2021-05-19 22:57:45 +03:00
|
|
|
declare
|
2021-05-19 22:57:45 +03:00
|
|
|
len int4;
|
2021-05-19 22:57:45 +03:00
|
|
|
bends1 geometry[];
|
2021-05-19 22:57:48 +03:00
|
|
|
dbgpolygon geometry;
|
2021-05-19 22:57:45 +03:00
|
|
|
begin
|
2021-05-19 22:57:45 +03:00
|
|
|
len = array_length(bends, 1);
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
bends = wm_fix_gentle_inflections1(bends);
|
2021-05-19 22:57:45 +03:00
|
|
|
for i in 1..len loop
|
2021-05-19 22:57:45 +03:00
|
|
|
bends1[i] = st_reverse(bends[len-i+1]);
|
2021-05-19 22:57:45 +03:00
|
|
|
end loop;
|
2021-05-19 22:57:49 +03:00
|
|
|
bends1 = wm_fix_gentle_inflections1(bends1);
|
2021-05-19 22:57:45 +03:00
|
|
|
|
|
|
|
for i in 1..len loop
|
2021-05-19 22:57:45 +03:00
|
|
|
bends[i] = st_reverse(bends1[len-i+1]);
|
2021-05-19 22:57:45 +03:00
|
|
|
end loop;
|
2021-05-19 22:57:47 +03:00
|
|
|
|
|
|
|
if dbgname is not null then
|
|
|
|
for i in 1..array_length(bends, 1) loop
|
|
|
|
insert into wm_debug(stage, name, gen, nbend, way) values(
|
2021-05-19 22:57:49 +03:00
|
|
|
'cinflections', dbgname, dbggen, i, bends[i]);
|
2021-05-19 22:57:48 +03:00
|
|
|
|
|
|
|
dbgpolygon = null;
|
|
|
|
if st_npoints(bends[i]) >= 3 then
|
2021-05-19 22:57:49 +03:00
|
|
|
dbgpolygon = st_makepolygon(
|
|
|
|
st_addpoint(bends[i],
|
|
|
|
st_startpoint(bends[i]))
|
|
|
|
);
|
2021-05-19 22:57:48 +03:00
|
|
|
end if;
|
|
|
|
|
2021-05-19 22:57:47 +03:00
|
|
|
insert into wm_debug(stage, name, gen, nbend, way) values(
|
2021-05-19 22:57:49 +03:00
|
|
|
'cinflections-polygon', dbgname, dbggen, i, dbgpolygon);
|
2021-05-19 22:57:47 +03:00
|
|
|
end loop;
|
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- wm_fix_gentle_inflections1 fixes gentle inflections of an array of lines in
|
2021-05-19 22:57:49 +03:00
|
|
|
-- one direction. An implementation detail of wm_fix_gentle_inflections.
|
2021-05-19 22:57:49 +03:00
|
|
|
drop function if exists wm_fix_gentle_inflections1;
|
|
|
|
create function wm_fix_gentle_inflections1(INOUT bends geometry[]) as $$
|
2021-05-19 22:57:45 +03:00
|
|
|
declare
|
2021-05-19 22:57:46 +03:00
|
|
|
-- the threshold when the angle is still "small", so gentle inflections can
|
|
|
|
-- be joined
|
2021-05-19 22:57:49 +03:00
|
|
|
small_angle constant real default radians(45);
|
2021-05-19 22:57:45 +03:00
|
|
|
ptail geometry; -- tail point of tail bend
|
|
|
|
phead geometry[]; -- 3 tail points of head bend
|
2021-05-19 22:57:45 +03:00
|
|
|
i int4; -- bends[i] is the current head
|
2021-05-19 22:57:45 +03:00
|
|
|
begin
|
2021-05-19 22:57:45 +03:00
|
|
|
for i in 2..array_length(bends, 1) loop
|
2021-05-19 22:57:45 +03:00
|
|
|
-- Predicate: two bends will always share an edge. Assuming (A,B,C,D,E,F)
|
|
|
|
-- is a bend:
|
|
|
|
-- C________D
|
|
|
|
-- / \
|
|
|
|
-- \________/ \_______/
|
|
|
|
-- A B E F
|
|
|
|
--
|
|
|
|
-- Then edges (A,B) and (E,F) are shared with the neighboring bends.
|
|
|
|
--
|
|
|
|
--
|
2021-05-19 22:57:45 +03:00
|
|
|
-- Assume this curve (figure `inflection-1`), going clockwise from A:
|
2021-05-19 22:57:45 +03:00
|
|
|
--
|
2021-05-19 22:57:45 +03:00
|
|
|
-- \______B
|
|
|
|
-- A `-------. C
|
|
|
|
-- |
|
|
|
|
-- G___ F |
|
|
|
|
-- / `-----.____+ D
|
|
|
|
-- E
|
2021-05-19 22:57:45 +03:00
|
|
|
--
|
|
|
|
-- After processing the curve following the definition of a bend, the bend
|
2021-05-19 22:57:45 +03:00
|
|
|
-- [A-E] would be detected. Assuming inflection point E and F are "small",
|
|
|
|
-- the bend needs to be extended by two edges to [A,G].
|
2021-05-19 22:57:45 +03:00
|
|
|
select geom from st_dumppoints(bends[i-1])
|
|
|
|
order by path[1] asc limit 1 into ptail;
|
|
|
|
|
2021-05-19 22:57:45 +03:00
|
|
|
while true loop
|
|
|
|
-- copy last 3 points of bends[i-1] (tail) to ptail
|
2021-05-19 22:57:45 +03:00
|
|
|
select array(
|
|
|
|
select geom from st_dumppoints(bends[i]) order by path[1] asc limit 3
|
|
|
|
) into phead;
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:45 +03:00
|
|
|
-- if the bend got too short, stop processing it
|
|
|
|
exit when array_length(phead, 1) < 3;
|
|
|
|
|
2021-05-19 22:57:46 +03:00
|
|
|
-- inflection angle between ptail[1:3] is "large", stop processing
|
2021-05-19 22:57:49 +03:00
|
|
|
exit when abs(st_angle(phead[1], phead[2], phead[3]) - pi()) > small_angle;
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:46 +03:00
|
|
|
-- distance from head's 1st vertex should be larger than from 2nd vertex
|
2021-05-19 22:57:45 +03:00
|
|
|
exit when st_distance(ptail, phead[2]) < st_distance(ptail, phead[3]);
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:46 +03:00
|
|
|
-- Detected a gentle inflection.
|
|
|
|
-- Move head of the tail to the tail of head
|
2021-05-19 22:57:45 +03:00
|
|
|
bends[i] = st_removepoint(bends[i], 0);
|
|
|
|
bends[i-1] = st_addpoint(bends[i-1], phead[3]);
|
2021-05-19 22:57:45 +03:00
|
|
|
end loop;
|
|
|
|
|
|
|
|
end loop;
|
2021-05-19 22:57:49 +03:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- wm_if_selfcross returns whether baseline of bendi crosses bendj.
|
2021-05-19 22:57:47 +03:00
|
|
|
-- If it doesn't, returns a null geometry.
|
|
|
|
-- Otherwise, it will return the baseline split into a few parts where it
|
|
|
|
-- crosses bendj.
|
2021-05-19 22:57:49 +03:00
|
|
|
drop function if exists wm_if_selfcross;
|
|
|
|
create function wm_if_selfcross(
|
2021-05-19 22:57:47 +03:00
|
|
|
bendi geometry,
|
|
|
|
bendj geometry
|
|
|
|
) returns geometry as $$
|
|
|
|
declare
|
|
|
|
a geometry;
|
|
|
|
b geometry;
|
2021-05-19 22:57:47 +03:00
|
|
|
multi geometry;
|
2021-05-19 22:57:47 +03:00
|
|
|
begin
|
|
|
|
a = st_pointn(bendi, 1);
|
|
|
|
b = st_pointn(bendi, -1);
|
2021-05-19 22:57:47 +03:00
|
|
|
multi = st_split(bendj, st_makeline(a, b));
|
2021-05-19 22:57:47 +03:00
|
|
|
|
2021-05-19 22:57:47 +03:00
|
|
|
if st_numgeometries(multi) = 1 then
|
2021-05-19 22:57:47 +03:00
|
|
|
return null;
|
|
|
|
end if;
|
|
|
|
|
2021-05-19 22:57:47 +03:00
|
|
|
if st_numgeometries(multi) = 2 and
|
2021-05-19 22:57:47 +03:00
|
|
|
(st_contains(bendj, a) or st_contains(bendj, b)) then
|
|
|
|
return null;
|
|
|
|
end if;
|
|
|
|
|
2021-05-19 22:57:47 +03:00
|
|
|
return multi;
|
2021-05-19 22:57:49 +03:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
2021-05-19 22:57:47 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- wm_self_crossing eliminates self-crossing from the bends, following the
|
2021-05-19 22:57:45 +03:00
|
|
|
-- article's section "Self-line Crossing When Cutting a Bend".
|
2021-05-19 22:57:49 +03:00
|
|
|
drop function if exists wm_self_crossing;
|
|
|
|
create function wm_self_crossing(
|
2021-05-19 22:57:46 +03:00
|
|
|
INOUT bends geometry[],
|
2021-05-19 22:57:49 +03:00
|
|
|
dbgname text default null,
|
|
|
|
dbggen integer default null,
|
2021-05-19 22:57:46 +03:00
|
|
|
OUT mutated boolean
|
|
|
|
) as $$
|
2021-05-19 22:57:45 +03:00
|
|
|
declare
|
2021-05-19 22:57:45 +03:00
|
|
|
i int4;
|
|
|
|
j int4;
|
2021-05-19 22:57:45 +03:00
|
|
|
multi geometry;
|
2021-05-19 22:57:45 +03:00
|
|
|
begin
|
2021-05-19 22:57:47 +03:00
|
|
|
mutated = false;
|
2021-05-19 22:57:47 +03:00
|
|
|
<<bendloop>>
|
2021-05-19 22:57:45 +03:00
|
|
|
for i in 1..array_length(bends, 1) loop
|
2021-05-19 22:57:49 +03:00
|
|
|
continue when abs(wm_inflection_angle(bends[i])) <= pi();
|
2021-05-19 22:57:45 +03:00
|
|
|
-- sum of inflection angles for this bend is >180, so it may be
|
2021-05-19 22:57:49 +03:00
|
|
|
-- self-crossing. Now try to find another bend in this line that
|
2021-05-19 22:57:45 +03:00
|
|
|
-- crosses an imaginary line of end-vertices
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:47 +03:00
|
|
|
-- Go through each bend in the given line, and see if has a potential to
|
|
|
|
-- cross bends[i]. The line-cut process is different when i<j and i>j;
|
|
|
|
-- therefore there are two loops, one for each case.
|
2021-05-19 22:57:47 +03:00
|
|
|
for j in 1..i-1 loop
|
2021-05-19 22:57:49 +03:00
|
|
|
multi = wm_if_selfcross(bends[i], bends[j]);
|
2021-05-19 22:57:47 +03:00
|
|
|
continue when multi is null;
|
2021-05-19 22:57:47 +03:00
|
|
|
mutated = true;
|
|
|
|
|
|
|
|
-- remove first vertex of the following bend, because the last
|
|
|
|
-- segment is always duplicated with the i'th bend.
|
|
|
|
bends[i+1] = st_removepoint(bends[i+1], 0);
|
|
|
|
bends[j] = st_geometryn(multi, 1);
|
|
|
|
bends[j] = st_setpoint(
|
|
|
|
bends[j],
|
|
|
|
st_npoints(bends[j])-1,
|
|
|
|
st_pointn(bends[i], st_npoints(bends[i]))
|
|
|
|
);
|
2021-05-19 22:57:47 +03:00
|
|
|
bends = bends[1:j] || bends[i+1:];
|
2021-05-19 22:57:47 +03:00
|
|
|
continue bendloop;
|
2021-05-19 22:57:47 +03:00
|
|
|
end loop;
|
|
|
|
|
|
|
|
for j in reverse array_length(bends, 1)..i+1 loop
|
2021-05-19 22:57:49 +03:00
|
|
|
multi = wm_if_selfcross(bends[i], bends[j]);
|
2021-05-19 22:57:47 +03:00
|
|
|
continue when multi is null;
|
2021-05-19 22:57:46 +03:00
|
|
|
mutated = true;
|
|
|
|
|
2021-05-19 22:57:47 +03:00
|
|
|
-- remove last vertex of the previous bend, because the last
|
|
|
|
-- segment is duplicated with the i'th bend.
|
|
|
|
bends[i-1] = st_removepoint(bends[i-1], st_npoints(bends[i-1])-1);
|
|
|
|
bends[i] = st_makeline(
|
|
|
|
st_pointn(bends[i], 1),
|
|
|
|
st_removepoint(st_geometryn(multi, st_numgeometries(multi)), 0)
|
|
|
|
);
|
2021-05-19 22:57:47 +03:00
|
|
|
bends = bends[1:i] || bends[j+1:];
|
2021-05-19 22:57:47 +03:00
|
|
|
continue bendloop;
|
2021-05-19 22:57:45 +03:00
|
|
|
end loop;
|
2021-05-19 22:57:46 +03:00
|
|
|
end loop;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
|
|
|
if dbgname is not null then
|
|
|
|
insert into wm_debug(stage, name, gen, nbend, way) values(
|
2021-05-19 22:57:49 +03:00
|
|
|
'dcrossings', dbgname, dbggen, generate_subscripts(bends, 1),
|
2021-05-19 22:57:49 +03:00
|
|
|
unnest(bends)
|
|
|
|
);
|
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2021-05-19 22:57:45 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
drop function if exists wm_inflection_angle;
|
|
|
|
create function wm_inflection_angle (IN bend geometry, OUT angle real) as $$
|
2021-05-19 22:57:46 +03:00
|
|
|
declare
|
|
|
|
p0 geometry;
|
|
|
|
p1 geometry;
|
|
|
|
p2 geometry;
|
|
|
|
p3 geometry;
|
|
|
|
begin
|
2021-05-19 22:57:47 +03:00
|
|
|
angle = 0;
|
2021-05-19 22:57:47 +03:00
|
|
|
for p0 in select geom from st_dumppoints(bend) order by path[1] asc loop
|
2021-05-19 22:57:46 +03:00
|
|
|
p3 = p2;
|
|
|
|
p2 = p1;
|
|
|
|
p1 = p0;
|
|
|
|
continue when p3 is null;
|
2021-05-19 22:57:49 +03:00
|
|
|
angle = angle + abs(pi() - st_angle(p1, p2, p3));
|
2021-05-19 22:57:46 +03:00
|
|
|
end loop;
|
2021-05-19 22:57:49 +03:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2021-05-19 22:57:46 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
drop function if exists wm_bend_attrs;
|
2021-05-19 22:57:49 +03:00
|
|
|
drop function if exists wm_isolated_bends;
|
2021-05-19 22:57:49 +03:00
|
|
|
drop function if exists wm_elimination;
|
2021-05-19 22:57:49 +03:00
|
|
|
drop function if exists wm_exaggeration;
|
2021-05-19 22:57:49 +03:00
|
|
|
drop type if exists wm_t_bend_attrs;
|
|
|
|
create type wm_t_bend_attrs as (
|
|
|
|
bend geometry,
|
|
|
|
area real,
|
2021-05-19 22:57:47 +03:00
|
|
|
adjsize real,
|
2021-05-19 22:57:47 +03:00
|
|
|
baselinelength real,
|
2021-05-19 22:57:47 +03:00
|
|
|
curvature real,
|
|
|
|
isolated boolean
|
2021-05-19 22:57:46 +03:00
|
|
|
);
|
2021-05-19 22:57:49 +03:00
|
|
|
create function wm_bend_attrs(
|
2021-05-19 22:57:47 +03:00
|
|
|
bends geometry[],
|
2021-05-19 22:57:49 +03:00
|
|
|
dbgname text default null,
|
|
|
|
dbggen integer default null
|
2021-05-19 22:57:49 +03:00
|
|
|
) returns setof wm_t_bend_attrs as $$
|
2021-05-19 22:57:46 +03:00
|
|
|
declare
|
2021-05-19 22:57:49 +03:00
|
|
|
cmp float;
|
2021-05-19 22:57:49 +03:00
|
|
|
i int4;
|
2021-05-19 22:57:49 +03:00
|
|
|
polygon geometry;
|
|
|
|
bend geometry;
|
|
|
|
res wm_t_bend_attrs;
|
2021-05-19 22:57:46 +03:00
|
|
|
begin
|
|
|
|
for i in 1..array_length(bends, 1) loop
|
2021-05-19 22:57:46 +03:00
|
|
|
bend = bends[i];
|
2021-05-19 22:57:49 +03:00
|
|
|
res = null;
|
|
|
|
res.bend = bend;
|
|
|
|
res.adjsize = 0;
|
|
|
|
res.baselinelength = st_distance(st_startpoint(bend), st_endpoint(bend));
|
|
|
|
res.curvature = wm_inflection_angle(bend) / st_length(bend);
|
|
|
|
res.isolated = false;
|
2021-05-19 22:57:47 +03:00
|
|
|
if st_numpoints(bend) >= 3 then
|
2021-05-19 22:57:49 +03:00
|
|
|
res.adjsize = wm_adjsize(bend);
|
2021-05-19 22:57:46 +03:00
|
|
|
end if;
|
2021-05-19 22:57:47 +03:00
|
|
|
|
2021-05-19 22:57:46 +03:00
|
|
|
if dbgname is not null then
|
2021-05-19 22:57:49 +03:00
|
|
|
insert into wm_debug (stage, name, gen, nbend, way, props) values(
|
2021-05-19 22:57:49 +03:00
|
|
|
'ebendattrs', dbgname, dbggen, i, bend,
|
2021-05-19 22:57:47 +03:00
|
|
|
jsonb_build_object(
|
2021-05-19 22:57:49 +03:00
|
|
|
'adjsize', res.adjsize,
|
|
|
|
'baselinelength', res.baselinelength,
|
|
|
|
'curvature', res.curvature
|
2021-05-19 22:57:46 +03:00
|
|
|
)
|
2021-05-19 22:57:49 +03:00
|
|
|
);
|
2021-05-19 22:57:46 +03:00
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
return next res;
|
2021-05-19 22:57:46 +03:00
|
|
|
end loop;
|
2021-05-19 22:57:49 +03:00
|
|
|
end;
|
|
|
|
$$ language plpgsql;
|
2021-05-19 22:57:46 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- sm_st_split a line by a point in a more robust way than st_split.
|
|
|
|
-- See https://trac.osgeo.org/postgis/ticket/2192
|
2021-05-19 22:57:49 +03:00
|
|
|
drop function if exists wm_st_split;
|
|
|
|
create function wm_st_split(
|
|
|
|
input geometry,
|
|
|
|
blade geometry
|
|
|
|
) returns geometry as $$
|
|
|
|
declare
|
|
|
|
type1 text;
|
|
|
|
type2 text;
|
|
|
|
begin
|
|
|
|
type1 = st_geometrytype(input);
|
|
|
|
type2 = st_geometrytype(blade);
|
|
|
|
if not (type1 = 'ST_LineString' and
|
|
|
|
type2 = 'ST_Point') then
|
|
|
|
raise 'Arguments must be LineString and Point, got: % and %', type1, type2;
|
|
|
|
end if;
|
|
|
|
return st_split(st_snap(input, blade, 0.00000001), blade);
|
|
|
|
end $$ language plpgsql;
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- wm_exaggerate_bend exaggerates a given bend. Must be a simple linestring.
|
|
|
|
drop function if exists wm_exaggerate_bend;
|
2021-05-19 22:57:49 +03:00
|
|
|
create function wm_exaggerate_bend(
|
|
|
|
INOUT bend geometry,
|
|
|
|
size float,
|
|
|
|
desired_size float
|
|
|
|
) as $$
|
2021-05-19 22:57:49 +03:00
|
|
|
declare
|
2021-05-19 22:57:49 +03:00
|
|
|
scale constant float default 1.2; -- exaggeration enthusiasm
|
2021-05-19 22:57:49 +03:00
|
|
|
midpoint geometry; -- midpoint of the baseline
|
2021-05-19 22:57:49 +03:00
|
|
|
splitbend geometry; -- bend split across its half
|
2021-05-19 22:57:49 +03:00
|
|
|
bendm geometry; -- bend with coefficients to prolong the lines
|
2021-05-19 22:57:49 +03:00
|
|
|
points geometry[];
|
2021-05-19 22:57:49 +03:00
|
|
|
begin
|
2021-05-19 22:57:49 +03:00
|
|
|
if size = 0 then
|
|
|
|
raise 'invalid input: zero-area bend';
|
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
midpoint = st_lineinterpolatepoint(st_makeline(
|
|
|
|
st_pointn(bend, 1),
|
|
|
|
st_pointn(bend, -1)
|
|
|
|
), .5);
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
while size < desired_size loop
|
|
|
|
splitbend = wm_st_split(bend, st_lineinterpolatepoint(bend, .5));
|
|
|
|
-- Convert bend to LINESTRINGM, where M is the fraction by how
|
|
|
|
-- much the point will be prolonged:
|
|
|
|
-- 1. draw a line between midpoint and the point on the bend.
|
|
|
|
-- 2. multiply the line length by M. Midpoint stays intact.
|
|
|
|
-- 3. the new set of lines form a new bend.
|
|
|
|
-- Uses linear interpolation; can be updated to gaussian or similar;
|
|
|
|
-- then interpolate manually instead of relying on st_addmeasure.
|
|
|
|
bendm = st_collect(
|
|
|
|
st_addmeasure(st_geometryn(splitbend, 1), 1, scale),
|
|
|
|
st_addmeasure(st_geometryn(splitbend, 2), scale, 1)
|
|
|
|
);
|
|
|
|
|
|
|
|
points = array((
|
|
|
|
select st_scale(
|
|
|
|
st_makepoint(st_x(geom), st_y(geom)),
|
|
|
|
st_makepoint(st_m(geom), st_m(geom)),
|
|
|
|
midpoint
|
|
|
|
)
|
|
|
|
from st_dumppoints(bendm)
|
|
|
|
order by path[1], path[2]
|
|
|
|
));
|
|
|
|
|
|
|
|
bend = st_setsrid(st_makeline(points), st_srid(bend));
|
|
|
|
size = wm_adjsize(bend);
|
|
|
|
end loop;
|
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
|
|
|
-- wm_adjsize calculates adjusted size for a polygon. Can return 0.
|
|
|
|
drop function if exists wm_adjsize;
|
|
|
|
create function wm_adjsize(bend geometry, OUT adjsize float) as $$
|
|
|
|
declare
|
|
|
|
polygon geometry;
|
|
|
|
area float;
|
|
|
|
cmp float;
|
|
|
|
begin
|
|
|
|
adjsize = 0;
|
|
|
|
polygon = st_makepolygon(st_addpoint(bend, st_startpoint(bend)));
|
|
|
|
-- 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
|
|
|
|
-- length of the circumference of the polygon". I assume they meant the
|
|
|
|
-- area of the circle. So here goes:
|
|
|
|
-- 1. get polygon area P.
|
|
|
|
-- 2. get polygon perimeter = u. Pretend it's our circle's circumference.
|
|
|
|
-- 3. get A (area) of the circle from u: A = u^2/(4pi)
|
|
|
|
-- 4. divide P by A: cmp = P/A = P/(u^2/(4pi)) = 4pi*P/u^2
|
|
|
|
area = st_area(polygon);
|
|
|
|
cmp = 4*pi()*area/(st_perimeter(polygon)^2);
|
|
|
|
if cmp > 0 then
|
|
|
|
adjsize = (area*(0.75/cmp));
|
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- wm_exaggeration is the Exaggeration Operator described in the WM paper.
|
2021-05-19 22:57:49 +03:00
|
|
|
create function wm_exaggeration(
|
2021-05-19 22:57:49 +03:00
|
|
|
INOUT bendattrs wm_t_bend_attrs[],
|
2021-05-19 22:57:49 +03:00
|
|
|
dhalfcircle float,
|
2021-05-19 22:57:49 +03:00
|
|
|
intersect_patience integer,
|
2021-05-19 22:57:49 +03:00
|
|
|
dbgname text default null,
|
|
|
|
dbggen integer default null,
|
|
|
|
OUT mutated boolean
|
|
|
|
) as $$
|
|
|
|
declare
|
2021-05-19 22:57:49 +03:00
|
|
|
desired_size constant float default pi()*(dhalfcircle^2)/8;
|
2021-05-19 22:57:49 +03:00
|
|
|
tmpbendattr wm_t_bend_attrs;
|
|
|
|
tmpint geometry;
|
2021-05-19 22:57:49 +03:00
|
|
|
i integer;
|
2021-05-19 22:57:49 +03:00
|
|
|
n integer;
|
|
|
|
last_id integer;
|
2021-05-19 22:57:49 +03:00
|
|
|
begin
|
2021-05-19 22:57:49 +03:00
|
|
|
mutated = false;
|
2021-05-19 22:57:49 +03:00
|
|
|
<<bendloop>>
|
|
|
|
for i in 1..array_length(bendattrs, 1) loop
|
|
|
|
if bendattrs[i].isolated and bendattrs[i].adjsize < desired_size then
|
|
|
|
tmpbendattr.bend = wm_exaggerate_bend(
|
|
|
|
bendattrs[i].bend,
|
|
|
|
bendattrs[i].adjsize,
|
|
|
|
desired_size
|
|
|
|
);
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- does tmpbendattrs.bend intersect with the previous or next
|
|
|
|
-- intersect_patience bends? If they do, abort exaggeration for this one.
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- Do close-by bends intersect with this one? Special
|
|
|
|
-- handling first, because 2 vertices need to be removed before checking.
|
|
|
|
n = st_npoints(bendattrs[i-1].bend);
|
|
|
|
if n > 3 then
|
|
|
|
continue when st_intersects(tmpbendattr.bend,
|
|
|
|
st_removepoint(st_removepoint(bendattrs[i-1].bend, n-1), n-2));
|
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
n = st_npoints(bendattrs[i+1].bend);
|
|
|
|
if n > 3 then
|
|
|
|
continue when st_intersects(tmpbendattr.bend,
|
|
|
|
st_removepoint(st_removepoint(bendattrs[i+1].bend, 0), 0));
|
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
for n in -intersect_patience+1..intersect_patience-1 loop
|
|
|
|
continue when n in (-1, 0, 1);
|
|
|
|
continue when i+n < 1;
|
|
|
|
continue when i+n > array_length(bendattrs, 1);
|
|
|
|
|
|
|
|
-- More special handling: if the neigbhoring bend has 3 vertices, the
|
|
|
|
-- neighbor's neighbor may just touch the tmpbendattr.bend; in this
|
|
|
|
-- case, the nearest vertex should be removed before comparing.
|
|
|
|
tmpint = bendattrs[i+n].bend;
|
|
|
|
if st_npoints(tmpint) > 2 then
|
|
|
|
if n = -2 and st_npoints(bendattrs[i+n+1].bend) = 3 then
|
|
|
|
tmpint = st_removepoint(tmpint, st_npoints(tmpint)-1);
|
|
|
|
elsif n = 2 and st_npoints(bendattrs[i+n-1].bend) = 3 then
|
|
|
|
tmpint = st_removepoint(tmpint, 0);
|
|
|
|
end if;
|
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
continue bendloop when st_intersects(tmpbendattr.bend, tmpint);
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
-- No intersections within intersect_patience, mutate bend!
|
|
|
|
mutated = true;
|
|
|
|
bendattrs[i] = tmpbendattr;
|
|
|
|
|
|
|
|
-- remove last vertex of the previous bend and first vertex of the next
|
|
|
|
-- bend, because bends always share a line segment together this is
|
|
|
|
-- duplicated in a few places, because PostGIS does not allow (?)
|
|
|
|
-- mutating an array when passed to a function.
|
|
|
|
tmpbendattr.bend = st_removepoint(
|
|
|
|
bendattrs[i-1].bend,
|
|
|
|
st_npoints(bendattrs[i-1].bend)-1
|
|
|
|
);
|
|
|
|
bendattrs[i-1] = tmpbendattr;
|
|
|
|
tmpbendattr.bend = st_removepoint(bendattrs[i+1].bend, 0);
|
|
|
|
bendattrs[i+1] = tmpbendattr;
|
|
|
|
|
|
|
|
if dbgname is not null then
|
|
|
|
insert into wm_debug (stage, name, gen, nbend, way) values(
|
|
|
|
'gexaggeration', dbgname, dbggen, i, bendattrs[i].bend
|
|
|
|
);
|
|
|
|
end if;
|
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
end loop;
|
2021-05-19 22:57:49 +03:00
|
|
|
end $$ language plpgsql;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
create function wm_elimination(
|
2021-05-19 22:57:49 +03:00
|
|
|
INOUT bendattrs wm_t_bend_attrs[],
|
2021-05-19 22:57:49 +03:00
|
|
|
dhalfcircle float,
|
2021-05-19 22:57:49 +03:00
|
|
|
dbgname text default null,
|
|
|
|
dbggen integer default null,
|
2021-05-19 22:57:49 +03:00
|
|
|
OUT mutated boolean
|
|
|
|
) as $$
|
|
|
|
declare
|
2021-05-19 22:57:49 +03:00
|
|
|
desired_size constant float default pi()*(dhalfcircle^2)/8;
|
2021-05-19 22:57:49 +03:00
|
|
|
leftsize float;
|
|
|
|
rightsize float;
|
|
|
|
i int4;
|
2021-05-19 22:57:49 +03:00
|
|
|
j int4;
|
|
|
|
tmpbendattr wm_t_bend_attrs;
|
|
|
|
dbgbends geometry[];
|
2021-05-19 22:57:49 +03:00
|
|
|
begin
|
|
|
|
mutated = false;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
i = 1;
|
2021-05-19 22:57:49 +03:00
|
|
|
while i < array_length(bendattrs, 1)-1 loop
|
2021-05-19 22:57:49 +03:00
|
|
|
i = i + 1;
|
2021-05-19 22:57:49 +03:00
|
|
|
continue when bendattrs[i].adjsize = 0;
|
|
|
|
continue when bendattrs[i].adjsize > desired_size;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
|
|
|
if i = 2 then
|
2021-05-19 22:57:49 +03:00
|
|
|
leftsize = bendattrs[i].adjsize + 1;
|
2021-05-19 22:57:49 +03:00
|
|
|
else
|
2021-05-19 22:57:49 +03:00
|
|
|
leftsize = bendattrs[i-1].adjsize;
|
2021-05-19 22:57:49 +03:00
|
|
|
end if;
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
if i = array_length(bendattrs, 1)-1 then
|
|
|
|
rightsize = bendattrs[i].adjsize + 1;
|
2021-05-19 22:57:49 +03:00
|
|
|
else
|
2021-05-19 22:57:49 +03:00
|
|
|
rightsize = bendattrs[i+1].adjsize;
|
2021-05-19 22:57:49 +03:00
|
|
|
end if;
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
continue when bendattrs[i].adjsize >= leftsize;
|
|
|
|
continue when bendattrs[i].adjsize >= rightsize;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- Local minimum. Elminate bend!
|
2021-05-19 22:57:49 +03:00
|
|
|
mutated = true;
|
2021-05-19 22:57:49 +03:00
|
|
|
tmpbendattr.bend = st_makeline(
|
|
|
|
st_pointn(bendattrs[i].bend, 1),
|
|
|
|
st_pointn(bendattrs[i].bend, -1)
|
|
|
|
);
|
|
|
|
bendattrs[i] = tmpbendattr;
|
2021-05-19 22:57:49 +03:00
|
|
|
-- remove last vertex of the previous bend and
|
|
|
|
-- first vertex of the next bend, because bends always
|
|
|
|
-- share a line segment together
|
2021-05-19 22:57:49 +03:00
|
|
|
tmpbendattr.bend = st_removepoint(
|
|
|
|
bendattrs[i-1].bend,
|
|
|
|
st_npoints(bendattrs[i-1].bend)-1
|
|
|
|
);
|
|
|
|
bendattrs[i-1] = tmpbendattr;
|
|
|
|
tmpbendattr.bend = st_removepoint(bendattrs[i+1].bend, 0);
|
|
|
|
bendattrs[i+1] = tmpbendattr;
|
2021-05-19 22:57:49 +03:00
|
|
|
-- the next bend's adjsize is now messed up; it should not be taken
|
|
|
|
-- into consideration for other local minimas. Skip over 2.
|
|
|
|
i = i + 2;
|
2021-05-19 22:57:49 +03:00
|
|
|
end loop;
|
|
|
|
|
|
|
|
if dbgname is not null then
|
2021-05-19 22:57:49 +03:00
|
|
|
for j in 1..array_length(bendattrs, 1) loop
|
|
|
|
dbgbends[j] = bendattrs[j].bend;
|
|
|
|
end loop;
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
insert into wm_debug(stage, name, gen, nbend, way) values(
|
2021-05-19 22:57:49 +03:00
|
|
|
'helimination',
|
2021-05-19 22:57:49 +03:00
|
|
|
dbgname,
|
|
|
|
dbggen,
|
2021-05-19 22:57:49 +03:00
|
|
|
generate_subscripts(dbgbends, 1),
|
|
|
|
unnest(dbgbends)
|
2021-05-19 22:57:49 +03:00
|
|
|
);
|
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
|
|
|
|
|
|
|
create function wm_isolated_bends(
|
|
|
|
INOUT bendattrs wm_t_bend_attrs[],
|
|
|
|
dbgname text default null,
|
|
|
|
dbggen integer default null
|
|
|
|
) as $$
|
|
|
|
declare
|
|
|
|
-- if neighbor's curvatures are within this fraction of the current bend
|
|
|
|
isolation_threshold constant real default 0.5;
|
|
|
|
this real;
|
|
|
|
skip_next bool;
|
|
|
|
res wm_t_bend_attrs;
|
|
|
|
i int4;
|
|
|
|
last_id integer;
|
|
|
|
begin
|
|
|
|
for i in 1..array_length(bendattrs, 1) loop
|
|
|
|
if dbgname is not null then
|
|
|
|
insert into wm_debug (stage, name, gen, nbend, way, props) values(
|
|
|
|
'fisolated_bends', dbgname, dbggen, i, bendattrs[i].bend,
|
|
|
|
jsonb_build_object('isolated', false)
|
|
|
|
) returning id into last_id;
|
|
|
|
end if;
|
|
|
|
-- first and last bends cannot be isolated
|
|
|
|
if i = 1 or i = array_length(bendattrs, 1) then
|
|
|
|
continue;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
res = bendattrs[i];
|
|
|
|
if skip_next then
|
|
|
|
skip_next = false;
|
|
|
|
else
|
|
|
|
this = bendattrs[i].curvature * isolation_threshold;
|
|
|
|
if bendattrs[i-1].curvature < this and
|
|
|
|
bendattrs[i+1].curvature < this then
|
|
|
|
res.isolated = true;
|
|
|
|
bendattrs[i] = res;
|
|
|
|
skip_next = true;
|
|
|
|
|
|
|
|
if dbgname is not null then
|
|
|
|
update wm_debug
|
|
|
|
set props=props || jsonb_build_object('isolated', true)
|
|
|
|
where id=last_id;
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
end loop;
|
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:48 +03:00
|
|
|
drop function if exists ST_SimplifyWM_Estimate;
|
|
|
|
create function ST_SimplifyWM_Estimate(
|
|
|
|
geom geometry,
|
|
|
|
OUT npoints bigint,
|
|
|
|
OUT secs bigint
|
|
|
|
) as $$
|
|
|
|
declare
|
|
|
|
lines geometry[];
|
|
|
|
l_type text;
|
|
|
|
begin
|
|
|
|
l_type = st_geometrytype(geom);
|
|
|
|
if l_type = 'ST_LineString' then
|
|
|
|
lines = array[geom];
|
|
|
|
elseif l_type = 'ST_MultiLineString' then
|
|
|
|
lines = array((select a.geom from st_dump(geom) a order by path[1] asc));
|
|
|
|
else
|
|
|
|
raise 'Unknown geometry type %', l_type;
|
|
|
|
end if;
|
|
|
|
|
|
|
|
npoints = 0;
|
|
|
|
for i in 1..array_length(lines, 1) loop
|
|
|
|
npoints = npoints + st_numpoints(lines[i]);
|
|
|
|
end loop;
|
2021-05-19 22:57:49 +03:00
|
|
|
secs = npoints / 33;
|
2021-05-19 22:57:49 +03:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|
2021-05-19 22:57:47 +03:00
|
|
|
|
2021-05-19 22:57:46 +03:00
|
|
|
-- ST_SimplifyWM simplifies a given geometry using Wang & Müller's
|
|
|
|
-- "Line Generalization Based on Analysis of Shape Characteristics" algorithm,
|
|
|
|
-- 1998.
|
2021-05-19 22:57:49 +03:00
|
|
|
-- Input parameters:
|
|
|
|
-- - geom: ST_LineString or ST_MultiLineString: the geometry to be simplified
|
2021-05-19 22:57:49 +03:00
|
|
|
-- - dhalfcircle: the diameter of a half-circle, whose area is an approximate
|
2021-05-19 22:57:49 +03:00
|
|
|
-- threshold for small bend elimination. If bend's area is larger than that,
|
|
|
|
-- the bend will be left alone.
|
2021-05-19 22:57:46 +03:00
|
|
|
drop function if exists ST_SimplifyWM;
|
2021-05-19 22:57:47 +03:00
|
|
|
create function ST_SimplifyWM(
|
|
|
|
geom geometry,
|
2021-05-19 22:57:49 +03:00
|
|
|
dhalfcircle float,
|
2021-05-19 22:57:49 +03:00
|
|
|
intersect_patience integer default 10,
|
2021-05-19 22:57:47 +03:00
|
|
|
dbgname text default null
|
|
|
|
) returns geometry as $$
|
2021-05-19 22:57:46 +03:00
|
|
|
declare
|
2021-05-19 22:57:49 +03:00
|
|
|
gen integer;
|
2021-05-19 22:57:46 +03:00
|
|
|
i integer;
|
2021-05-19 22:57:49 +03:00
|
|
|
j integer;
|
2021-05-19 22:57:46 +03:00
|
|
|
line geometry;
|
2021-05-19 22:57:46 +03:00
|
|
|
lines geometry[];
|
2021-05-19 22:57:46 +03:00
|
|
|
bends geometry[];
|
2021-05-19 22:57:49 +03:00
|
|
|
bendattrs wm_t_bend_attrs[];
|
2021-05-19 22:57:46 +03:00
|
|
|
mutated boolean;
|
2021-05-19 22:57:46 +03:00
|
|
|
l_type text;
|
|
|
|
begin
|
2021-05-19 22:57:49 +03:00
|
|
|
if intersect_patience is null then
|
|
|
|
intersect_patience = 10;
|
|
|
|
end if;
|
2021-05-19 22:57:46 +03:00
|
|
|
l_type = st_geometrytype(geom);
|
|
|
|
if l_type = 'ST_LineString' then
|
2021-05-19 22:57:46 +03:00
|
|
|
lines = array[geom];
|
2021-05-19 22:57:46 +03:00
|
|
|
elseif l_type = 'ST_MultiLineString' then
|
2021-05-19 22:57:46 +03:00
|
|
|
lines = array((select a.geom from st_dump(geom) a order by path[1] asc));
|
2021-05-19 22:57:46 +03:00
|
|
|
else
|
|
|
|
raise 'Unknown geometry type %', l_type;
|
|
|
|
end if;
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
<<lineloop>>
|
2021-05-19 22:57:46 +03:00
|
|
|
for i in 1..array_length(lines, 1) loop
|
2021-05-19 22:57:46 +03:00
|
|
|
mutated = true;
|
2021-05-19 22:57:49 +03:00
|
|
|
gen = 1;
|
2021-05-19 22:57:46 +03:00
|
|
|
while mutated loop
|
2021-05-19 22:57:46 +03:00
|
|
|
if dbgname is not null then
|
2021-05-19 22:57:47 +03:00
|
|
|
insert into wm_debug (stage, name, gen, nbend, way) values(
|
2021-05-19 22:57:49 +03:00
|
|
|
'afigures', dbgname, gen, i, lines[i]);
|
2021-05-19 22:57:46 +03:00
|
|
|
end if;
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
bends = wm_detect_bends(lines[i], dbgname, gen);
|
|
|
|
bends = wm_fix_gentle_inflections(bends, dbgname, gen);
|
2021-05-19 22:57:46 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
select * from wm_self_crossing(bends, dbgname, gen) into bends, mutated;
|
2021-05-19 22:57:46 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
if mutated then
|
|
|
|
lines[i] = st_linemerge(st_union(bends));
|
|
|
|
gen = gen + 1;
|
|
|
|
continue;
|
2021-05-19 22:57:49 +03:00
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
bendattrs = array((select wm_bend_attrs(bends, dbgname, gen)));
|
|
|
|
bendattrs = wm_isolated_bends(bendattrs, dbgname, gen);
|
|
|
|
|
|
|
|
select * from wm_exaggeration(
|
|
|
|
bendattrs, dhalfcircle, intersect_patience, dbgname, gen
|
|
|
|
) into bendattrs, mutated;
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
-- TODO: wm_combination
|
|
|
|
|
|
|
|
if not mutated then
|
|
|
|
select * from wm_elimination(
|
2021-05-19 22:57:49 +03:00
|
|
|
bendattrs, dhalfcircle, dbgname, gen) into bendattrs, mutated;
|
2021-05-19 22:57:49 +03:00
|
|
|
end if;
|
|
|
|
|
2021-05-19 22:57:49 +03:00
|
|
|
if mutated then
|
2021-05-19 22:57:49 +03:00
|
|
|
for j in 1..array_length(bendattrs, 1) loop
|
|
|
|
bends[j] = bendattrs[j].bend;
|
|
|
|
end loop;
|
2021-05-19 22:57:49 +03:00
|
|
|
lines[i] = st_linemerge(st_union(bends));
|
2021-05-19 22:57:49 +03:00
|
|
|
if st_geometrytype(lines[i]) != 'ST_LineString' then
|
2021-05-19 22:57:49 +03:00
|
|
|
-- For manual debugging:
|
|
|
|
--insert into wm_manual(name, way)
|
|
|
|
--select 'non-linestring-' || a.path[1], a.geom
|
|
|
|
--from st_dump(lines[i]) a
|
|
|
|
--order by a.path[1];
|
|
|
|
raise 'Got % (in %) instead of ST_LineString. '
|
2021-05-19 22:57:49 +03:00
|
|
|
'Does the exaggerated bend intersect with the line? '
|
|
|
|
'If so, try increasing intersect_patience.',
|
|
|
|
st_geometrytype(lines[i]), dbgname;
|
2021-05-19 22:57:49 +03:00
|
|
|
--exit lineloop;
|
2021-05-19 22:57:49 +03:00
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
gen = gen + 1;
|
2021-05-19 22:57:49 +03:00
|
|
|
continue;
|
2021-05-19 22:57:46 +03:00
|
|
|
end if;
|
2021-05-19 22:57:46 +03:00
|
|
|
end loop;
|
|
|
|
end loop;
|
|
|
|
|
|
|
|
if l_type = 'ST_LineString' then
|
2021-05-19 22:57:46 +03:00
|
|
|
return st_linemerge(st_union(lines));
|
2021-05-19 22:57:46 +03:00
|
|
|
elseif l_type = 'ST_MultiLineString' then
|
2021-05-19 22:57:46 +03:00
|
|
|
return st_union(lines);
|
2021-05-19 22:57:46 +03:00
|
|
|
end if;
|
2021-05-19 22:57:49 +03:00
|
|
|
end
|
|
|
|
$$ language plpgsql;
|