ポイントが別のポイントの南、東などにあるかどうかをSQL Server 2008R2で知る方法はありますか?
たとえば、原点があり、その原点から北、西などのpoint(lat1,lng1)場所を知りたいとします。point(lat2,lng2)
私は風配図を作成しようとしていますが、これは私にとって役立つかもしれません。
ポイントが別のポイントの南、東などにあるかどうかをSQL Server 2008R2で知る方法はありますか?
たとえば、原点があり、その原点から北、西などのpoint(lat1,lng1)場所を知りたいとします。point(lat2,lng2)
私は風配図を作成しようとしていますが、これは私にとって役立つかもしれません。
私は、標準の SQL 関数をかなり単純に使用して方位を計算する方法を思いつきました。ATAN 関数は、実際の作業のほとんどを行います。2 つの 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
    SQL Server 2008 R2 で Geography 型を使用しているときに 2 つの座標間の方位を計算するには、次の関数を使用できます。
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)
UPDATE : スキーマの追加
