-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiline_functions.sql
56 lines (54 loc) · 1.87 KB
/
multiline_functions.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
create or replace function MultiLineLocatePoint(line geometry, point geometry) returns float8 as $$
select
(base + extra) / ST_Length(line)
from (
select
sum(ST_Length(l.geom)) over (order by l.path) - ST_Length(l.geom) base,
ST_LineLocatePoint(l.geom, point) * ST_Length(l.geom) extra,
ST_Distance(l.geom, point) dist
from ST_Dump(line) l
) points
order by dist
limit 1;
$$ language SQL;
create or replace function MultiLineInterpolatePoint(line geometry, location float8) returns geometry as $$
select
ST_LineInterpolatePoint(parts.geom, (ST_Length(line) * location - segment_start) / segment_length)
from (
select
sum(ST_Length(l.geom)) over (order by l.path) - ST_Length(l.geom) segment_start,
sum(ST_Length(l.geom)) over (order by l.path) segment_end,
ST_Length(l.geom) + 0.001 segment_length,
l.geom geom
from ST_Dump(line) l
) parts
where ST_Length(line) * location between segment_start and segment_end
limit 1;
$$ language SQL;
create or replace function MultiLineSubstring(line geometry, startfraction float8, endfraction float8) returns geometry as $$
select
ST_Collect(
ST_LineSubstring(
s.geom,
case
when seg_start > startfraction then 0
else (startfraction - seg_start)/(seg_end - seg_start)
end,
case
when seg_end < endfraction then 1
else 1 - (seg_end - endfraction)/(seg_end - seg_start)
end
)
order by s.path
)
from (
select
l.geom,
l.path,
(sum(ST_Length(l.geom)) over (order by l.path) - ST_Length(l.geom))/ST_Length(line) seg_start,
(sum(ST_Length(l.geom)) over (order by l.path))/ST_Length(line) seg_end
from ST_Dump(line) l
) s
where seg_end > startfraction
and seg_start < endfraction;
$$ language SQL;