4

我无法将在纬度/经度和操作系统国家网格参考点之间转换的 javascript 代码移植到 Java。(http://www.movable-type.co.uk/scripts/latlong-gridref.html

我在一些数学运算中得到了不同的结果。我在下面包含了javascriptjava代码,但发生的是Ma计算结果 0.04195508514183418in javascript0.04195511450680837in java。我已经打印了计算的输入,它们是相同的。

这是javascript代码:

OsGridRef.osGridToLatLong = function(gridref) {
     var E = gridref.easting;
     var N = gridref.northing;

     var a = 6377563.396, b = 6356256.910;              // Airy 1830 major & minor semi-axes
     var F0 = 0.9996012717;                             // NatGrid scale factor on central meridian
     var lat0 = 49*Math.PI/180, lon0 = -2*Math.PI/180;  // NatGrid true origin
     var N0 = -100000, E0 = 400000;                     // northing & easting of true origin, metres
     var e2 = 1 - (b*b)/(a*a);                          // eccentricity squared
     var n = (a-b)/(a+b), n2 = n*n, n3 = n*n*n;

     var lat=lat0, M=0;
     var count = 0;

     do {

       count++;
       lat = (N-N0-M)/(a*F0) + lat;

       console.log("pre ma calc");
                console.log("n = " + n);
                console.log("n2 = " + n2);
                console.log("n3 = " + n3);
                console.log("lat = " + lat);
                console.log("lat0 = " + lat0);

       var Ma = (1 + n + (5/4)*n2 + (5/4)*n3) * (lat-lat0);

        console.log("post ma calc ma = " + Ma);

这是输出:

pre ma calc test.html:68
n = 0.0016732202503250534 test.html:69
n2 = 0.0000027996660060978346 test.html:70
n3 = 4.684457855549562e-9 test.html:71
lat = 0.8970962185213205 test.html:72
lat0 = 0.8552113334772214 test.html:73
post ma calc ma = 0.04195511450680837

这是java代码:

LatLon osGridToLatLong(OsGridRef osGridRef) {

        int E = osGridRef.easting;
          int N = osGridRef.northing;


          double a = 6377563.396, b = 6356256.910;              // Airy 1830 major & minor semi-axes
          double F0 = 0.9996012717;                             // NatGrid scale factor on central meridian
          double lat0 = 49*Math.PI/180, lon0 = -2*Math.PI/180;  // NatGrid true origin
          double N0 = -100000, E0 = 400000;                     // northing & easting of true origin, metres
          double e2 = 1 - (b*b)/(a*a);                          // eccentricity squared
          double n = (a-b)/(a+b), n2 = n*n, n3 = n*n*n;

          double lat=lat0, M=0;


          int count = 0;
          do {
              count++;
            lat = (N-N0-M)/(a*F0) + lat;

            Log.e(TAG, "pre ma calc");
            Log.e(TAG, "n = " + n);
            Log.e(TAG, "n2 = " + n2);
            Log.e(TAG, "n3 = " + n3);
            Log.e(TAG, "lat = " + lat);
            Log.e(TAG, "lat0 = " + lat0);

            double Ma = (1 + n + (5/4)*n2 + (5/4)*n3) * (lat-lat0);
            Log.e(TAG, "post ma calc ma = " + String.valueOf(Ma));

            double Mb = (3*n + 3*n*n + (21/8)*n3) * Math.sin(lat-lat0) * Math.cos(lat+lat0);
            double Mc = ((15/8)*n2 + (15/8)*n3) * Math.sin(2*(lat-lat0)) * Math.cos(2*(lat+lat0));
            double Md = (35/24)*n3 * Math.sin(3*(lat-lat0)) * Math.cos(3*(lat+lat0));
            M = b * F0 * (Ma - Mb + Mc - Md);                // meridional arc


          } while (N-N0-M >= 0.00001);  // ie until < 0.01mm

这是输出:

07-03 12:36:03.413: E/DSDS(779): pre ma calc
07-03 12:36:03.423: E/DSDS(779): n = 0.0016732202503250534
07-03 12:36:03.423: E/DSDS(779): n2 = 2.7996660060978346E-6
07-03 12:36:03.443: E/DSDS(779): n3 = 4.684457855549562E-9
07-03 12:36:03.473: E/DSDS(779): lat = 0.8970962185213205
07-03 12:36:03.473: E/DSDS(779): lat0 = 0.8552113334772214
07-03 12:36:03.473: E/DSDS(779): post ma calc ma = 0.04195508514183418
4

1 回答 1

5

5/4将被解释为整数除法,因此将产生5 / 4 = 1.25 = 1

将其更改为,5.0 / 4以便它将使用浮点类型而不是整数。多次计算 Ma、Mb、Mc 和 Md 就是这种情况。

也许这已经使您的结果变得均匀,但就数学本身而言,它肯定会使它们正确。我认为您除此之外还正确迁移了代码。

于 2013-07-03T13:39:23.200 回答