【发布时间】:2011-08-25 23:19:43
【问题描述】:
所以这个问题的内容很少,但希望有人有足够的知识来提供帮助。
在我的 SQL Server 数据库中,我有 2 条位置记录,它们依次具有以下属性 latitude(nLatitude)、longitude(nlongtitue) 和 geography(gGeoLocation) 值。
记录 1:39.283638,-76.568567,0xE6100000010C91F3FE3F4EA443406EA5D766632453C0
记录 2:39.285200,-76.554366,0xE6100000010CDC68006F81A44340EB0088BB7A2353C0
我使用下面的 SQL 查询创建了 Geography 值:
UPDATE [Location] SET gGeoLocation = geography::STPointFromText('POINT(' + CAST(nlongtitue AS VARCHAR(20)) + ' ' + CAST([nLatitude] AS VARCHAR(20)) + ')', 4326 )
现在假设我想搜索我的位置表以确定哪些位置在某个纬度/经度的某个半径内 (39.290555,-76.609604) 我使用以下查询:
从 [位置] 中选择 * WHERE gGeoLocation.STDistance(geography::Point(@Latitude, @Longitude, 4326))
然后我根据 C# 函数检查我的结果,两个结果不一样,所以其中一个肯定是错误的,或者我遗漏了一些东西。这是 C# 函数:
//calculate haversine distance for linear distance - Miles
public static double haversine_mi(double lat1, double long1, double lat2, double long2)
{
double dlong = (long2 - long1) * d2r;
double dlat = (lat2 - lat1) * d2r;
double a = Math.Pow(Math.Sin(dlat / 2.0), 2) + Math.Cos(lat1 * d2r) * Math.Cos(lat2 * d2r) * Math.Pow(Math.Sin(dlong / 2.0), 2);
double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
double d = 3956 * c;
return d;
}
最终,如果有人可以为我提供一个 SQL 查询,该查询将在特定半径内搜索位置表,并返回位置与搜索点之间的距离(以英里和公里为单位),这将是很棒的。
但我也想了解为什么我得到不同的结果应该返回相同的结果。我对地理空间数据很陌生。
【问题讨论】:
-
“两个结果不一样”——那是什么?谁在火星上?
标签: c# sql-server geolocation geospatial