有没有办法在SQL Server 2008R2中知道某个点是在另一个点的南,东等等?
例如,我有一个来源point(lat1,lng1)
,我想知道point(lat2,lng2)
位于该来源的位置:北,西等......
我正在尝试构建风玫瑰图,这可能对我有用。
答案 0 :(得分: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)
更新:添加架构
答案 1 :(得分:4)
我想出了一种使用标准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
答案 2 :(得分: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
答案 3 :(得分: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
以下是用于进一步说明的架构:
答案 4 :(得分:0)
X=X2-X1
和Y=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)))