Sap hana linestring to segment

时间:2017-03-30 10:22:57

标签: sql sap geospatial hana

如何将线串分成线段?使用SAP HANA DB

例如:

'LINESTRING(0 0, 2 2, 0 2, 0 5)'

将成为:

'LINESTRING(0 0, 2 2) LINESTRING(2 2, 0 2) LINESTRING(0 2, 0 5)'

1 个答案:

答案 0 :(得分:0)

您可以使用函数ST_PointN()提取单个点。 将其称为第1,2,3,... N点 我使用一个名为TMP的临时表来存储计数器值:

CREATE TABLE TMP (NUM INT);
select TOP 100 ROW_NUMBER() OVER () from PUBLIC.M_TABLES ;

然后我使用计数器表为每个点应用ST_PointN。

select NUM, LS.ST_PointN(NUM).ST_WKT() as CUR_POINT
from(
    select NEW ST_LineString('LINESTRING(0 0, 2 2, 0 2, 0 5)') as LS, NUM
    from TMP    
)nested1    
where NUM<=LS.ST_NumPoints()

返回

NUM| CUR_POINT  
1  |POINT (0 0)  
2  |POINT (2 2)  
3  |POINT (0 2)  
4  |POINT (0 5)  

您可以使用ST_UnionAggr()轻松地将这些几何连接到ST_Multipoint几何体中:

select ST_UnionAggr(CUR_POINT).ST_AsWKT()
from(
    select NUM, LS.ST_PointN(NUM) as CUR_POINT
    from(
        select NEW ST_LineString('LINESTRING(0 0, 2 2, 0 2, 0 5)') as LS,     NUM
        from TMP    
    )nested1    
    where NUM<=LS.ST_NumPoints()
)nested2

返回

MULTIPOINT ((0 0),(2 2),(0 2),(0 5))

注意:我们可以循环而不是使用计数器表。

现在针对您的确切问题,我们将使用窗口函数构建3个ST_LineString(),以将当前点与下一个点组合。编写此类查询的方法有多种,其中一种方法是:

select NEW ST_LineString('LineString ('||START_POINT.ST_X()||' '||START_POINT.ST_Y()||','||END_POINT.ST_X()||' '||END_POINT.ST_Y()||')').ST_AsWKT() as LS
from(
    select CUR_POINT as START_POINT,
           NEW ST_Point(FIRST_VALUE(CUR_POINT) OVER(order by NUM asc rows BETWEEN 1 following and 1 following) )as END_POINT
    from(
        select NUM, LS.ST_PointN(NUM) as CUR_POINT, LS.ST_NumPoints() as NB_POINTS
        from(
            select NEW ST_LineString('LINESTRING(0 0, 2 2, 0 2, 0 5)') as LS, NUM
            from TMP    
        )nested1    
        where NUM<=LS.ST_NumPoints()
    )nested2
)nested3 
where END_POINT is not null

多田:

LS  
LINESTRING (0 0,2 2)  
LINESTRING (2 2,0 2)  
LINESTRING (0 2,0 5)