3

有没有办法在 SQL Server 2008R2 中知道一个点是否在另一个点的东南部、东部等...?

例如,我有一个原点point(lat1,lng1),我想知道point(lat2,lng2)该原点的位置:北、西等...

我正在尝试构建风玫瑰图,这可能对我有用。

4

5 回答 5

5

我想出了一种使用标准 SQL 函数相当简单的计算方位角的方法。ATAN 函数完成了大部分实际工作;这两个 CASE 语句只是特殊情况的更正。1 是源,2 是目标。

atan(([Longitude2]-[Longitude1])/(10e-10+[Latitude2]-[Latitude1]))*360/pi()/2
+case when [Latitude2]<[Latitude1] then 180 else 0 end
+case when [Longitude2]<[Longitude1] and [Latitude2]>[Latitude1] then 360 else 0 end
于 2015-03-18T10:07:08.717 回答
4

为了在 SQL Server 2008 R2 中使用 Geography 类型计算两个坐标之间的方位角,您可以使用此函数:

CREATE FUNCTION [dbo].[CalculateBearing] 
(
    @pointA as geography
    ,@pointB as geography
)

RETURNS decimal(18,12)

AS

    BEGIN

    -- Declare the return variable
    DECLARE @bearing decimal(18,12)

    -- Declare the local variables
    DECLARE @x decimal(18,12)
    DECLARE @y decimal(18,12)
    DECLARE @dLat decimal(18,12)
    DECLARE @dLong decimal(18,12)
    DECLARE @rLat1 decimal(18,12)
    DECLARE @rLat2 decimal(18,12)

    IF(@pointA.STIsEmpty() = 1 OR @pointB.STIsEmpty() = 1)
        set @bearing = null
    ELSE
        BEGIN

        -- Calculate delta between coordinates
        SET @dLat = RADIANS(@pointB.Lat - @pointA.Lat)
        SET @dLong = RADIANS(@pointB.Long - @pointA.Long)

        -- Calculate latitude as radians
        SET @rLat1 = RADIANS(@pointA.Lat)
        SET @rLat2 = RADIANS(@pointB.Lat)

        SET @y = SIN(@dLong)*COS(@rLat2)
        SET @x = COS(@rLat1)*SIN(@rLat2)-SIN(@rLat1)*COS(@rlat2)*COS(@dLong)

        IF (@x = 0 and @y = 0)
            SET @bearing = null
        ELSE
            BEGIN
                SET @bearing = CAST((DEGREES(ATN2(@y,@x)) + 360) as decimal(18,12)) % 360
            END
    END

    -- Return the result of the function
    RETURN @bearing

END

GO

在此之后,您可以像这样使用此功能:

DECLARE @pointA as geography
DECLARE @pointB as geography

SET @pointA = geography::STGeomFromText('POINT(3 45)', 4326)
SET @pointB = geography::STGeomFromText('POINT(4 47)', 4326)

SELECT [dbo].[CalculateBearing](@pointA, @pointB)

更新:添加架构

轴承说明

于 2013-02-08T20:44:34.893 回答
3

今天早上,当我在我们的系统中搜索附近的订单时,我需要这个功能来为用户提供范围和基本方向。我来到尼古拉斯的答案这里,它让我大部分时间都在那里。我创建了第二个函数,它使用 Nicolas 为我的 UI 获取一个缩写的基本方向(N、NE、E 等)。

使用此处提供的 Nicolas 方位计算结合来自https://en.wikipedia.org/wiki/Points_of_the_compass的值来确定每个基本方向的范围,

CREATE FUNCTION [dbo].[CalculateCardinalDirection] 
(
    @pointA as geography
    ,@pointB as geography
)

RETURNS varchar(2)

AS
BEGIN
    DECLARE @bearing decimal(18,12)
    -- Bearing calculation provided by http://stackoverflow.com/a/14781032/4142441
    SELECT @bearing = dbo.CalculateBearing(@pointA, @pointB)

    RETURN CASE WHEN @bearing BETWEEN 0 AND 22.5 THEN 'N'
                WHEN @bearing BETWEEN 22.5 AND 67.5 THEN 'NE'
                WHEN @bearing BETWEEN 67.5 AND 112.5 THEN 'E'
                WHEN @bearing BETWEEN 112.5 AND 157.5 THEN 'SE'
                WHEN @bearing BETWEEN 157.5 AND 202.5 THEN 'S'
                WHEN @bearing BETWEEN 202.5 AND 247.5 THEN 'SW'
                WHEN @bearing BETWEEN 247.5 AND 292.5 THEN 'W'
                WHEN @bearing BETWEEN 292.5 AND 337.5 THEN 'NW'
                ELSE 'N' -- Catches NULL bearings and the 337.5 to 360.0 range
           END
END
GO
于 2016-09-01T17:06:56.780 回答
0

X=X2-X1Y=Y2-Y1.一个公式,从 0(正 Y 轴)到 360 度顺时针给出方位角。

  f(X,Y)=180-90*(1+SIGN(Y))*(1-SIGN(X^2))-45*(2+SIGN(Y))*SIGN(X)-180/PI()*SIGN(Y*X)*ATAN((ABS(Y)-ABS(X))/(ABS(Y)+ABS(X)))
于 2019-01-31T19:01:21.580 回答
0

如果点数据类型是“几何”(如 UTM 坐标系),您可以使用以下公式:

DEGREES(ATAN((X2-X1)/(Y2-Y1)))
+case when Y2<Y1 then 180 else 0 end
+case when Y2>Y1 and X2<X1 then 360 else 0 end

以下是更多说明的架构:

连接两点的直线的方位角

于 2019-01-02T18:06:58.437 回答