Wil*_*son 5 oracle aggregate spatial oracle-12c
我有一张road_vertices
桌子:
create table road_vertices
(
road_id number,
vertex_index number,
x number,
y number
);
insert into road_vertices values ('100',1,0,5);
insert into road_vertices values ('100',2,10,10);
insert into road_vertices values ('100',3,30,0);
insert into road_vertices values ('100',4,50,10);
insert into road_vertices values ('100',5,60,10);
select * from road_vertices;
ROAD_ID VERTEX_INDEX X Y
---------- --------------- ---------- ----------
100 1 0 5
100 2 10 10
100 3 30 0
100 4 50 10
100 5 60 10
Run Code Online (Sandbox Code Playgroud)
我需要:
这是最终目标:
ROAD_ID LINESTRING
----------------------------------------------------------------------------
100 LINESTRING M ( 0 5 0, 10 10 11.18, 30 0 33.54, 50 10 55.9, 60 10 65.9)
Run Code Online (Sandbox Code Playgroud)
我想出了一种方法来做到这一点:
--Step #3: Collapse the coordinates and cumulative lengths into a linestring
SELECT
ROAD_ID,
'LINESTRING M ( ' || LISTAGG(CUMULATIVE_LENGTH, ', ')
WITHIN GROUP (ORDER BY VERTEX_INDEX) || ')' AS LINESTRING
FROM
(
--Step #2: Calculate each line segment's length using the Pythagorean theorem, and add together to get cumulative length
SELECT
ROAD_ID,
VERTEX_INDEX,
X || ' ' || Y || ' ' || ROUND(SUM(NVL(SQRT(POWER((X - PREV_X),2) + POWER((Y - PREV_Y),2)),0))
OVER (PARTITION BY ROAD_ID ORDER BY ROAD_ID,VERTEX_INDEX),2)
AS CUMULATIVE_LENGTH
FROM
(
--Step #1: Get the previous X and previous Y for Step #2's Pythagorean theorem calculation
SELECT
ROAD_ID,
VERTEX_INDEX,
ROUND(X,2) AS X,
ROUND(Y,2) AS Y,
LAG (X,1) OVER (PARTITION BY ROAD_ID ORDER BY VERTEX_INDEX) AS PREV_X,
LAG (Y,1) OVER (PARTITION BY ROAD_ID ORDER BY VERTEX_INDEX) AS PREV_Y
FROM
INFRASTR.ROAD_VERTICES
)
)
GROUP BY
ROAD_ID;
Run Code Online (Sandbox Code Playgroud)
然而,这个解决方案相当复杂。它可以简化/改进吗?
函数和查询/视图的组合可能是另一种选择。该函数满足您的第一个要求:“计算每个线段的长度......顶点之间的线的部分。” (该函数需要异常处理和测试!)
-- -----------------------------------------------------------------------------
-- function: calculate the segment length
-- -----------------------------------------------------------------------------
create or replace function seglength(
x_ number
, oldx_ number
, y_ number
, oldy_ number
)
return number as
begin
if oldx_ = 0 or oldy_ = 0 then -- vertex_index 1, no "previous"/old values
return 0;
else
return round(
sqrt(
power( ( x_ - oldx_ ), 2 )
+ power( ( y_ - oldy_) , 2 )
)
, 2
);
end if;
end seglength;
/
Run Code Online (Sandbox Code Playgroud)
然后,我们可以使用原始查询的修改版本,如下所示:
select
d.roadid
, 'LINESTRING M ( '
|| listagg( ( round(x,2) || ' ' || round(y,2) || ' '
|| seglength(x, d.old_x, y, d.old_y) ) , ', ' )
within group ( order by d.vertexindex )
|| ')' linestring
from (
select
roadid
, vertexindex
, x
, y
, case
when vertexindex = 1 then 0 -- zero instead of NULL
else ( lag (x,1) over ( partition by roadid order by vertexindex ) )
end old_x
, case
when vertexindex = 1 then 0
else ( lag (y,1) over ( partition by roadid order by vertexindex ) )
end old_y
from rdvx
) d
group by d.roadid;
Run Code Online (Sandbox Code Playgroud)
输出:
500100 LINESTRING M ( 670113.32 4863724.94 0, 670122.42 4863728.94 9.94, 670259.91 4863776.23 145.39)
507200 LINESTRING M ( 670147.94 4863628.42 0, 670158.74 4863632.98 11.72, 670298.55 4863680.65 147.72)
Run Code Online (Sandbox Code Playgroud)
注意:“LINESTRING”中的最后一个值小于您问题中的值。您的原始查询是否实际上计算了顶点 1 和 3 之间的距离?我的理解是“段长度”应该是:距离v1-v1即0,距离v1-v2,距离v2-v3。dbfiddle 在这里
更新
功能:
create or replace function rlength(
x number
, prev_x number
, y number
, prev_y number
)
return number as
begin
if prev_x is null or prev_y is null then
return 0 ;
else
return round(
sqrt(
power( ( x - prev_x ), 2 )
+ power( ( y - prev_y ), 2 )
)
, 2
);
end if;
end rlength;
/
Run Code Online (Sandbox Code Playgroud)
询问:
with roads_ as (
select
road_id
, vertex_index
, round( x, 2 ) x
, round( y, 2 ) y
, sum ( rlen ) over ( partition by road_id order by road_id, vertex_index ) clength
from (
select
road_id
, vertex_index
, x
, y
, rlength(
x
, lag( x,1 ) over ( partition by road_id order by vertex_index )
, y
, lag( y,1 ) over ( partition by road_id order by vertex_index )
) rlen
from road_vertices
)
)
select
road_id
, 'LINESTRING M ( '
|| listagg( x || ' ' || y || ' ' || clength , ', ' )
within group ( order by vertex_index )
|| ' )' linestring
from roads_
group by road_id;
Run Code Online (Sandbox Code Playgroud)
测试数据:
create table road_vertices
(
road_id number,
vertex_index number,
x number,
y number
);
begin
insert into road_vertices values ('100',1,0,5);
insert into road_vertices values ('100',2,10,10);
insert into road_vertices values ('100',3,30,0);
insert into road_vertices values ('100',4,50,10);
insert into road_vertices values ('100',5,60,10);
end;
/
Run Code Online (Sandbox Code Playgroud)
输出:
ROAD_ID LINESTRING
100 LINESTRING M ( 0 5 0, 10 10 11.18, 30 0 33.54, 50 10 55.9, 60 10 65.9 )
Run Code Online (Sandbox Code Playgroud)