我从一个点进行最近的点查询。以下是序列。我在MSSQLServer中存储了多个兴趣点,表为dbo.Place。第二个表是dbo.Position,它将存储收集到的GPS点。
我正在使用存储过程,并且Position的LatLong已定义并可用。如何根据下面的示例进行查询?
dbo.Place
Id | Name | Lat | Long
1 POI1 1.735 4.73225
2 POI2 1.5665 3.9983
3 Tim2 1.4344 3.1282Lat Long变量是在存储过程中定义的。我想使用下面的公式来找到最近的点,我只会从3个查询中取最近的值(假设样本数据是3行)
SQRT(POW(X(`POI.Lat`) - 49.843317 , 2) + POW(Y(`POI.Long`) - 24.026642, 2)) * 100谢谢。
发布于 2019-02-11 16:36:21
为此,您可以使用SQL Server的地理功能。
DECLARE @InputLatitude FLOAT = 1.64
DECLARE @InputLongitude FLOAT = 4.25
DECLARE @GPS GEOGRAPHY = GEOGRAPHY::Point(@InputLatitude, @InputLongitude, 4326)
SELECT TOP 1
P.*,
Distance = @GPS.STDistance(GEOGRAPHY::Point(P.Lat, P.Long, 4326))
FROM
dbo.Place AS P
ORDER BY
@GPS.STDistance(GEOGRAPHY::Point(P.Lat, P.Long, 4326)) ASC您应该考虑在已经转换了GPS点的表中添加GEOGRAPHY列,并添加SPATIAL INDEX以加快查询速度。
发布于 2019-02-11 17:00:20
除了推荐的地理类型之外,您还可以使用浮点等常规数据类型来实现类似的结果。
DECLARE @latitude FLOAT = 4.5678; -- Latitude of the place to search around
DECLARE @longitude FLOAT = 51.234; -- Longitude of the place to search around
DECLARE @range FLOAT = 100000; -- Max range in meters
SELECT TOP(1000)
[place].[Lat],
[place].[Long],
((((ACOS((SIN((PI() * [place].[Lat]) / 180.0) * SIN((PI() * @latitude) / 180.0)) + ((COS((PI() * [place].[Lat]) / 180.0) * COS((PI() * @latitude) / 180.0)) * COS((PI() * ([place].[Long] - @longitude)) / 180.0))) * 180.0) * 60.0) * 1.1515) * 1609.344) / PI() AS [Distance]
FROM [dbo].[Place] AS [place]
WHERE (((((ACOS((SIN((PI() * [place].[Lat]) / 180.0) * SIN((PI() * @latitude) / 180.0)) + ((COS((PI() * [place].[Lat]) / 180.0) * COS((PI() * @latitude) / 180.0)) * COS((PI() * ([place].[Long] - @longitude)) / 180.0))) * 180.0) * 60.0) * 1.1515) * 1609.344) / PI()) <= @range
ORDER BY [Distance]https://stackoverflow.com/questions/54626513
复制相似问题