2

有没有一种简单的方法可以从 SQL 中的一列点中创建 LineString?

例如我有一个查询:

SELECT Geom FROM api.Table WHERE WellId = 'XYZ1234' ORDER BY MeasuredDepth ASC;

以 wkb 形式返回 186 点:

Geom
0xE6100000010CDEB06D5166275AC024B4E55C8A114440
0xE6100000010CE5F21FD26F275AC061FD9FC37C114440
0xE6100000010C4512BD8C62275AC0FCA5457D92114440
0xE6100000010CF33CB83B6B275AC063450DA661144440
0xE6100000010CA56B26DF6C275AC01E32E54350134440
0xE6100000010CBDFBE3BD6A275AC0C1CAA145B6134440
0xE6100000010CB6D617096D275AC03A92CB7F48134440
0xE6100000010CFD82DDB06D275AC04C4F58E201134440
0xE6100000010C9A081B9E5E275AC0041C42959A114440
...

我还可以将这些 wkb 保存在临时表中并将它们作为文本返回:

POINT (-104.61562 40.137035)
POINT (-104.6162 40.13662)
POINT (-104.61539 40.137283)
POINT (-104.61592 40.15923)
POINT (-104.61602 40.150887)
POINT (-104.61589 40.154)
POINT (-104.61603 40.15065)
POINT (-104.61607 40.148495)
POINT (-104.61515 40.13753)
POINT (-104.61605 40.15017)
...

但是从MSSQL 文档中,我找不到任何方法可以从所有这些点中生成 LineString。这可能吗,例如PostGIS ST_MakeLine 之类的

4

2 回答 2

2

您可以将 Geom 列的 X 和 Y 属性连接到一个字符串变量中,然后将 LINESTRING 与该变量中包含的所有点一起使用。

    declare @string varchar(max)

    Select @string = isnull(@string + ',', '') + cast(Geom.STX as varchar(20)) + ' ' + cast(Geom.STY as varchar(20))
    from api.Table

    Set @string = 'LINESTRING(' + @string + ')';   

    Select geometry::STLineFromText(@string, 4326);

编辑

为排序问题添加第二个答案,您可以遍历带有标识列的表。

在下面的示例中,我创建了一个具有 Id 标识的表变量,将记录插入到该表中,然后迭代并填充字符串。

    declare @points table (Id int identity, Geom geometry)

    Insert into @points (Geom)  
    SELECT Geom 
    FROM api.Table 
    WHERE WellId = 'XYZ1234' 
    ORDER BY MeasuredDepth ASC;

    declare @iterRow    int,
            @rowCount   int,
            @string     varchar(max),
            @x          varchar(20),
            @y          varchar(20)

    Select  @iterRow = 1,
            @rowCount = count(1)
    from @points

    While (@iterRow <= @rowCount)
    Begin   
        Select  @x = cast(Geom.STX as varchar(20)),
                @y = cast(Geom.STY as varchar(20))
        From @points
        where Id = @iterRow
        
        Set @string = isnull(@string + ',', '') + @x + ' ' + @y

        Set @iterRow += 1
    End

    Set @string = 'LINESTRING(' + @string + ')';   

    Select geometry::STLineFromText(@string, 0);
于 2020-08-11T18:09:45.670 回答
1

接受的答案正是我正在寻找的,但它是否失去了升序(基于另一个变量)?将此作为输出:

LINESTRING (-104.615 40.1378, -104.615 40.1378, -104.615 40.1378, -104.615 40.1378, -104.615 40.1378, -104.615 40.1377, -104.615 40.1377, -104.615 40.1377, -104.615 40.1377, -104.615 40.1377, -104.615 40.1376, -104.615 40.1376, ...)
于 2020-08-11T21:49:55.920 回答