Linq to sql Haversine formula

本文关键字:formula Haversine sql to Linq | 更新日期: 2023-09-27 18:36:22

我在c#和TSQL中都有Harversine公式的实现。我不确定如何在服务器端最好地实现该公式,以便能够在 Linq 查询中使用它。

理想情况下,我只是将我的本地公式链接到服务器上的函数。从而避免了"没有转换为sql"的错误,并拥有一切美好而无缝。

显然,对这个问题的任何看法都是有帮助的。

我知道SQL2008中的地理类型。然而,我正在反对的代码库已经对 Linq to SQL 有如此大的依赖,我希望它付出的努力超过了它的价值!

谢谢

Linq to sql Haversine formula

为什么不使用100%SQL,因为这是进行计算的最佳方式,并且只需获得一个已经填满距离的表格?

从现有答案

CREATE FUNCTION dbo.udf_Haversine(@lat1 float, @long1 float, @lat2 float, @long2 float) RETURNS float 
  BEGIN
    DECLARE @dlon float, @dlat float, @rlat1 float, @rlat2 float, @rlong1 float, @rlong2 float, @a float, @c float, @R float, @d float, @DtoR float
    SELECT @DtoR = 0.017453293
    SELECT @R = 3937 --3976
    SELECT 
        @rlat1 = @lat1 * @DtoR,
        @rlong1 = @long1 * @DtoR,
        @rlat2 = @lat2 * @DtoR,
        @rlong2 = @long2 * @DtoR
    SELECT 
        @dlon = @rlong1 - @rlong2,
        @dlat = @rlat1 - @rlat2
    SELECT @a = power(sin(@dlat/2), 2) + cos(@rlat1) * cos(@rlat2) * power(sin(@dlon/2), 2)
    SELECT @c = 2 * atn2(sqrt(@a), sqrt(1-@a))
    SELECT @d = @R * @c
    RETURN @d 
  END

并像这样使用:

var table = from r in db.VenuePostCodes 
            select new {
                lat = r.Latitude,
                lng = r.Longitude,
                name = r.Name,
                distance = db.udf_Haversine(
                                  r.Latitude,r.Longitude,
                                  r.Latitude,r.Longitude2)
            };

但最好的办法是始终将所有内容都放在SQL上,这样您的托管服务器就可以少做,只需将VIEW添加到SQL并调用该视图,让我们想象一下:

SELECT 
   latitude, longitude, name, latitude1, longitude2, postcode, 
   udf_Haversine(latitude, longitude, latitude2, longitude2) AS distance 
FROM 
   venuepostcodes
ORDER BY 
   distance

并使用 LINQ 直接调用该视图。

@balexandre的答案很棒,但我对提供的 SQL 函数不满意(它缺少注释,有趣的注释是否经常,是英里吗?公里?等等...

CREATE FUNCTION [dbo].[udf_Haversine](@lat1 float, @long1 float, @lat2 float, @long2 float) RETURNS float 
BEGIN
    DECLARE @dlon float, @dlat float,
            @rlat1 float, @rlat2 float, @rlong1 float, @rlong2 float,
            @a float, @c float, @R float, @d float, @DtoR float
    SELECT
        @DtoR = PI() / 180, -- Degrees to radians const
        @R = 6371 -- Radius of Earth in KM
    SELECT 
        @rlat1 = @lat1 * @DtoR,
        @rlong1 = @long1 * @DtoR,
        @rlat2 = @lat2 * @DtoR,
        @rlong2 = @long2 * @DtoR
    SELECT 
        @dlat = @rlat1 - @rlat2,
        @dlon = @rlong1 - @rlong2
    SELECT @a = SIN(@dlat / 2) * SIN(@dlat / 2) +
                SIN(@dlon / 2) * SIN(@dlon / 2) * COS(@rlat2) * COS(@rlat1)
    SELECT @c = 2 * atn2(sqrt(@a), sqrt(1 - @a))
    SELECT @d = @R * @c -- Final distance in KM
    SELECT @d = @d * 0.621371192 -- Final distance in miles
RETURN @d 
END

它是从我们从这里抓取的 JavaScript 实现转换而来的,

最后还转换为英里:
// Converted from JavaScript implementation:
// http://www.movable-type.co.uk/scripts/latlong.html
var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;