为什么SqlGeography STDistance有区别?

时间:2017-09-11 15:20:07

标签: c# sqlgeography

我一直在追踪我的代码中的一个错误,我发现这是因为Microsoft c#SqlGeography 2014库返回的STDistance结果与我计算点之间距离的常规代码略有不同。

我写了一个小的控制台exe来演示这个问题,但我无法弄清楚为什么我会得到这么不同的结果?

static void Main(string[] args) {
        double earthRadius = 6378137; // meters => from both nad83 & wgs84
        var a = new { lat = 43.68151632, lng = -79.61162263 };
        var b = new { lat = 43.67575602, lng = -79.59586143 };

        // sql geography lib
        SqlGeographyBuilder sgb;
        sgb = new SqlGeographyBuilder();
        sgb.SetSrid(4326);
        sgb.BeginGeography(OpenGisGeographyType.Point);
        sgb.BeginFigure(a.lat, a.lng);
        sgb.EndFigure();
        sgb.EndGeography();
        SqlGeography geoA = sgb.ConstructedGeography;

        sgb = new SqlGeographyBuilder();
        sgb.SetSrid(4326);
        sgb.BeginGeography(OpenGisGeographyType.Point);
        sgb.BeginFigure(b.lat, b.lng);
        sgb.EndFigure();
        sgb.EndGeography();
        SqlGeography geoB = sgb.ConstructedGeography;

        // distance cast from SqlDouble
        double geoDistance = (double)geoA.STDistance(geoB);

        // math!
        double d2r = Math.PI / 180; // for converting degrees to radians
        double lat1 = a.lat * d2r,
            lat2 = b.lat * d2r,
            lng1 = a.lng * d2r,
            lng2 = b.lng * d2r,
            dLat = lat2 - lat1,
            dLng = lng2 - lng1,
            sin_dLat_half = Math.Pow(Math.Sin(dLat / 2), 2),
            sin_dLng_half = Math.Pow(Math.Sin(dLng / 2), 2),
            distance = sin_dLat_half + Math.Cos(lat1) * Math.Cos(lat2) * sin_dLng_half;

        // math distance
        double mathDistance = (2 * Math.Atan2(Math.Sqrt(distance), Math.Sqrt(1 - distance))) * earthRadius;

        // haversine
        double sLat1 = Math.Sin(a.lat * d2r),
                sLat2 = Math.Sin(b.lat * d2r),
                cLat1 = Math.Cos(a.lat * d2r),
                cLat2 = Math.Cos(b.lat * d2r),
                cLon = Math.Cos((a.lng * d2r) - (b.lng * d2r)),
                cosD = sLat1 * sLat2 + cLat1 * cLat2 * cLon,
                d = Math.Acos(cosD);

        // math distance
        double methDistance = d * earthRadius;


        // write the outputs
        Console.WriteLine("geo distance:\t" + geoDistance);    // 1422.99560435875
        Console.WriteLine("math distance:\t" + mathDistance);  // 1421.73656776243
        Console.WriteLine("meth distance:\t" + methDistance);  // 1421.73656680185
        Console.WriteLine("geo vs math:\t" + (geoDistance - mathDistance));     // 1.25903659632445
        Console.WriteLine("haversine vs math:\t" + (methDistance - methDistance)); // ~0.00000096058011
    }

Microsoft是否使用不同的计算方法?当计算距离小于1.5公里时偏离超过1米是一个巨大差异。

2 个答案:

答案 0 :(得分:2)

好的,经过多次挖掘后我找到了答案,微软更多正确。

具体来说,他们正在使用Vincenty's formulae。准确度在0.5毫米(不是半毫米)内,而不是Haversine formula的0.3%。

原因是Haversine(我,谷歌和Bing地图显然太明显)很快,但依赖于球形地球而不是椭圆体。 Microsoft使用椭圆体地球来计算距离而不是球体,从而提供更准确的结果。

我在c#中实现了Vincenty的方法,并且到目前为止它已经工作了,但是没有准备就绪。

    const double d2r = Math.PI / 180;  // degrees to radians
    const double EARTH_RADIUS = 6378137;  // meters
    const double EARTH_ELLIPSOID = 298.257223563; // wgs84
    const double EARTH_BESSEL = 1 / EARTH_ELLIPSOID;
    const double EARTH_RADIUS_MINOR = EARTH_RADIUS - (EARTH_RADIUS * EARTH_BESSEL); // 6356752.3142 meters => wgs84

    static double vincentyDistance(double lat1, double lng1, double lat2, double lng2) {
        double L = (lng2 - lng1) * d2r,
                U1 = Math.Atan((1 - EARTH_BESSEL) * Math.Tan(lat1 * d2r)),
                U2 = Math.Atan((1 - EARTH_BESSEL) * Math.Tan(lat2 * d2r)),
                sinU1 = Math.Sin(U1),
                cosU1 = Math.Cos(U1),
                sinU2 = Math.Sin(U2),
                cosU2 = Math.Cos(U2),
                lambda = L,
                lambdaP,
                iterLimit = 100,
                sinLambda,
                cosLambda,
                sinSigma,
                cosSigma,
                sigma,
                sinAlpha,
                cosSqAlpha,
                cos2SigmaM,
                C;
        do {
            sinLambda = Math.Sin(lambda);
            cosLambda = Math.Cos(lambda);
            sinSigma = Math.Sqrt((cosU2 * sinLambda) * (cosU2 * sinLambda) + (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda) * (cosU1 * sinU2 - sinU1 * cosU2 * cosLambda));
            if (0 == sinSigma) {
                return 0; // co-incident points
            };
            cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
            sigma = Math.Atan2(sinSigma, cosSigma);
            sinAlpha = cosU1 * cosU2 * sinLambda / sinSigma;
            cosSqAlpha = 1 - sinAlpha * sinAlpha;
            cos2SigmaM = cosSigma - 2 * sinU1 * sinU2 / cosSqAlpha;
            C = EARTH_BESSEL / 16 * cosSqAlpha * (4 + EARTH_BESSEL * (4 - 3 * cosSqAlpha));
            //  if (isNaN(cos2SigmaM)) {
            //      cos2SigmaM = 0; // equatorial line: cosSqAlpha = 0 (§6)
            //  };
            lambdaP = lambda;
            lambda = L + (1 - C) * EARTH_BESSEL * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
        } while (Math.Abs(lambda - lambdaP) > 1e-12 && --iterLimit > 0);

        if (iterLimit == 0) {
            return 0; // formula failed to converge
        };

        double uSq = cosSqAlpha * (EARTH_RADIUS * EARTH_RADIUS - EARTH_RADIUS_MINOR * EARTH_RADIUS_MINOR) / (EARTH_RADIUS_MINOR * EARTH_RADIUS_MINOR),
            A = 1 + uSq / 16384 * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq))),
            B = uSq / 1024 * (256 + uSq * (-128 + uSq * (74 - 47 * uSq))),
            deltaSigma = B * sinSigma * (cos2SigmaM + B / 4 * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - B / 6 * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM))),
            s = EARTH_RADIUS_MINOR * A * (sigma - deltaSigma);
        return s;
    }

此代码是从我在此处找到的JavaScript实现转换而来的:https://gist.github.com/mathiasbynens/354587

答案 1 :(得分:0)

Microsoft可能更多正确;但它正确!

根据Kallay (2010), SqlGeography STDistance不返回测地距离但是 沿着连接2个点的大椭圆的距离。最棒的 椭圆距离不是最短的距离,它不遵守 三角不等式。大椭圆返回一个合理的 接近点的测地距离的近似值。为了遥远 点误差可能高达33公里。

C#给出了测地距离的精确计算 图书馆 NETGeographicLib (我写了底层的C ++库)。这比准确更准确 Vincenty(10纳米而不是0.5毫米),更重要的是, 始终获得正确的结果。 (文森特没有收敛 几乎是对映点。)