获取地理多边形的 Lat Long 点的函数 SQL
Function to get Lat Long points for geography polygon SQL
我创建了一个函数,用于 return MS-SQL 中多点地理字段中的一串点。我想知道是否有更好的方法来做到这一点?我想要的是向不使用 SQL 地理数据类型(他们使用 SQL Lite)的外部客户提供一个点列表作为 Lat/Long 坐标。
Create FUNCTION [dbo].GetLatLongForPolygon
(
@Perimeter GEOGRAPHY
)
RETURNS Varchar(max)
AS
BEGIN
declare @NumPoints as Int
declare @OutputString as varchar(max)
declare @latpoint as varchar(max)
declare @longpoint as varchar(max)
set @NumPoints = @Perimeter.STNumPoints()
while @NumPoints >0
begin
set @LatPoint = @Perimeter.STPointN(@NumPoints).Lat
set @longpoint = @Perimeter.STPointN(@NumPoints).Long
set @OutputString = concat (@OutputString, '(' , @latpoint, ',', @longpoint , '), ')
set @NumPoints = @NumPoints -1
End
RETURN left (@OutputString, len(@outputstring)-1)
GO
是的。就在这里。我正在使用计数 table 来消除光标和标准习语来模拟 T-SQL.
中丢失的字符串连接聚合
declare @g geography = geography::STLineFromText('LINESTRING(20 20, 21 21, 22 22)', 4236);
with cte as (
select @g.STPointN(n.Number) as [Point]
from dbadmin.dbo.Numbers as n
where n.Number <= @g.STNumPoints()
)
select stuff((
select ', ' + concat('(', Point.Lat, ', ', Point.Long, ')')
from cte
for xml path('')
), 1, 2, '')
我创建了一个函数,用于 return MS-SQL 中多点地理字段中的一串点。我想知道是否有更好的方法来做到这一点?我想要的是向不使用 SQL 地理数据类型(他们使用 SQL Lite)的外部客户提供一个点列表作为 Lat/Long 坐标。
Create FUNCTION [dbo].GetLatLongForPolygon
(
@Perimeter GEOGRAPHY
)
RETURNS Varchar(max)
AS
BEGIN
declare @NumPoints as Int
declare @OutputString as varchar(max)
declare @latpoint as varchar(max)
declare @longpoint as varchar(max)
set @NumPoints = @Perimeter.STNumPoints()
while @NumPoints >0
begin
set @LatPoint = @Perimeter.STPointN(@NumPoints).Lat
set @longpoint = @Perimeter.STPointN(@NumPoints).Long
set @OutputString = concat (@OutputString, '(' , @latpoint, ',', @longpoint , '), ')
set @NumPoints = @NumPoints -1
End
RETURN left (@OutputString, len(@outputstring)-1)
GO
是的。就在这里。我正在使用计数 table 来消除光标和标准习语来模拟 T-SQL.
中丢失的字符串连接聚合declare @g geography = geography::STLineFromText('LINESTRING(20 20, 21 21, 22 22)', 4236);
with cte as (
select @g.STPointN(n.Number) as [Point]
from dbadmin.dbo.Numbers as n
where n.Number <= @g.STNumPoints()
)
select stuff((
select ', ' + concat('(', Point.Lat, ', ', Point.Long, ')')
from cte
for xml path('')
), 1, 2, '')