用于为地理多边形SQL获取Lat Long点的函数

时间:2016-01-07 11:28:01

标签: sql-server sqlgeography

我创建了一个函数,用于在MS-SQL中的多点地理区域内返回一串点。我想知道是否有更好的方法来做到这一点?我想要的是提供一个点列表作为Lat / Long坐标给不使用SQL地理数据类型的外部客户端(他们使用的是SQL Lite)。

   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

1 个答案:

答案 0 :(得分:0)

是。就在这里。我使用计数表来消除游标和标准习惯用法来模拟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, '')