【问题标题】:Function to get Lat Long points for geography polygon SQL获取地理多边形 SQL 的经纬度点的函数
【发布时间】:2016-01-07 11:28:01
【问题描述】:

我创建了一个函数来返回 MS-SQL 中多点地理字段中的点字符串。我想知道是否有更好的方法来做到这一点?我想要的是将点列表作为纬度/经度坐标提供给不使用 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

【问题讨论】:

    标签: sql-server sqlgeography


    【解决方案1】:

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

    【讨论】:

    • 干杯,以前没有使用过数字表,但这会为我做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 2023-03-20
    相关资源
    最近更新 更多