如何检查Circle纬度经度

时间:2016-06-29 20:54:52

标签: sql-server sql-server-2012

我有以下查询如何检查纬度和经度是否在圈内但是显示错误的结果请帮助

 declare @latitudeCar float = 25.681137335685306;  
declare @longitudeCar float = 66.697998046875;
declare @latitudePlace float = 25.918526162075153;
declare @longitudePlace float = 66.170654296875;


declare @source geography = 'POINT(' + cast(@latitudeCar as nvarchar) + ' ' + cast(@longitudeCar as nvarchar) + ')'
declare @target geography = 'POINT(' + cast(@latitudePlace as nvarchar) + ' ' + cast(@longitudePlace as nvarchar) + ')'
declare @radius float = 164096.68447201277
declare @check float
declare @isPastCircle bit

set @check = (select @source.STDistance(@target)/1609.344)
--select @source.STDistance(@target)/1000

set @isPastCircle = (select case when @check > @radius then 1 else 0 end);
select @isPastCircle [isPastCircle], @check [carDistance], @radius [acceptableRadius]

总是结果错了:(

2 个答案:

答案 0 :(得分:0)

除了“<”之外,您的查询似乎是正确的在选择的情况下。它应该是:

set @isPastCircle = (select case when @check > @radius then 1 else 0 end);

答案 1 :(得分:0)

看起来你正试图以英里指定的固定半径进行计算,但是你正在绊倒转换。以下是代码的清理版本:

sshpass

TL; DR - 尽可能经常尝试抽象出你的转化。在这种情况下,我以我习惯的单位(即英里)指定我想要的半径,然后使用不同的变量来保持空间参考使用的所需半径(如果你'有疑问,只需查询declare @latitudeCar float = 25.681137335685306, @longitudeCar float = 66.697998046875, @latitudePlace float = 25.918526162075153, @longitudePlace float = 66.170654296875; declare @source geography = geography::Point(@latitudeCar, @longitudeCar, 4326), @target geography = geography::Point(@latitudePlace, @longitudePlace, 4326); declare @radius_in_miles int = 100; declare @radius_in_meters float = @radius_in_miles * 1609.344; select @target.STBuffer(@radius_in_meters).STContains(@source) AS [isWithinCircle], @target.STDistance(@source) AS [distance_in_meters], @target.STBuffer(@radius_in_meters).STDisjoint(@source) AS [isPastCircle]; )。另请注意,我简化了指定半径计算的内部/外部,以分别使用sys.spatial_reference_systemsSTContains()。我假设你实际上并不关心两点之间的距离,所以单独使用它们可以加快速度。