SQL 服务器 - 来自点几何的线串
SQL Server - Linestring from Points Geometry
有没有一种简单的方法可以从 SQL 中的一列点中创建一个 LineString?
例如我有一个查询:
SELECT Geom FROM api.Table WHERE WellId = 'XYZ1234' ORDER BY MeasuredDepth ASC;
那 returns 186 点作为 wkb:
Geom
0xE6100000010CDEB06D5166275AC024B4E55C8A114440
0xE6100000010CE5F21FD26F275AC061FD9FC37C114440
0xE6100000010C4512BD8C62275AC0FCA5457D92114440
0xE6100000010CF33CB83B6B275AC063450DA661144440
0xE6100000010CA56B26DF6C275AC01E32E54350134440
0xE6100000010CBDFBE3BD6A275AC0C1CAA145B6134440
0xE6100000010CB6D617096D275AC03A92CB7F48134440
0xE6100000010CFD82DDB06D275AC04C4F58E201134440
0xE6100000010C9A081B9E5E275AC0041C42959A114440
...
我也可以将这些 wkb 保存在一个临时的 table 和 return 中作为文本:
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 documentation I can't find any way to make a LineString out of all of these points. Is this possible, i.e. something like PostGIS ST_MakeLine?
您可以将 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);
编辑
为 添加第二个答案,您可以使用标识列遍历 table。
在下面的示例中,我将创建一个具有 Id 标识的 table 变量,将记录插入到该 table 中,然后迭代并填充字符串。
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);
接受的答案正是我要找的,但它是否丢失了升序(基于另一个变量)?将其作为输出:
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, ...)
有没有一种简单的方法可以从 SQL 中的一列点中创建一个 LineString?
例如我有一个查询:
SELECT Geom FROM api.Table WHERE WellId = 'XYZ1234' ORDER BY MeasuredDepth ASC;
那 returns 186 点作为 wkb:
Geom
0xE6100000010CDEB06D5166275AC024B4E55C8A114440
0xE6100000010CE5F21FD26F275AC061FD9FC37C114440
0xE6100000010C4512BD8C62275AC0FCA5457D92114440
0xE6100000010CF33CB83B6B275AC063450DA661144440
0xE6100000010CA56B26DF6C275AC01E32E54350134440
0xE6100000010CBDFBE3BD6A275AC0C1CAA145B6134440
0xE6100000010CB6D617096D275AC03A92CB7F48134440
0xE6100000010CFD82DDB06D275AC04C4F58E201134440
0xE6100000010C9A081B9E5E275AC0041C42959A114440
...
我也可以将这些 wkb 保存在一个临时的 table 和 return 中作为文本:
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 documentation I can't find any way to make a LineString out of all of these points. Is this possible, i.e. something like PostGIS ST_MakeLine?
您可以将 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 标识的 table 变量,将记录插入到该 table 中,然后迭代并填充字符串。
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);
接受的答案正是我要找的,但它是否丢失了升序(基于另一个变量)?将其作为输出:
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, ...)