如何在SQL Server中反转点的经度/纬度?

时间:2016-06-01 15:34:57

标签: sql-server

如何将列中包含多个点的地理线字符串的顺序反转为正常(纬度,经度)?

enter image description here

1 个答案:

答案 0 :(得分:0)

假设您有一个数字表,您可以这样做。它不是我写过的最优雅的东西,但它完成了工作。

DECLARE @g GEOGRAPHY, @g_new GEOGRAPHY, @t VARCHAR(200);
SET @g = geography::STLineFromText('LINESTRING (2 1,4 3)', 4236);
SELECT  @g.ToString();

WITH cte AS (
    SELECT   [n] ,
            @g.[STPointN]([n]) AS [point]
    FROM     [Util].[dbo].[Numbers] AS [n]
    WHERE    [n] <= @g.[STNumPoints]()
)

SELECT  @t = CONCAT('LINESTRING(', 
    STUFF((
        SELECT CONCAT(',', [cte].[point].Lat, ' ', [cte].[point].Long)
        FROM [cte]
        ORDER BY n
        FOR XML PATH('')
    ),1, 1, ''), 
    ')'
);

SET @g_new = GEOGRAPHY::STGeomFromText(@t, @g.[STSrid]);
SELECT @g_new.ToString();