将纬度/经度转换为 MGRS 坐标的 Java 库,反之亦然?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7097334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 18:36:44  来源:igfitidea点击:

Java library that converts latitude/longitude to MGRS coordinates and vice versa?

javalatitude-longitude

提问by tf.rz

I'm wondering what kind of libraries exist (licensed or not) where I may find the methods to convert between latitude/longitude coordinates and military grid reference system (mgrs) coordinates. I'm not explicitly looking for a lat/long to UTM converter, it has to be mgrs. I've done some searching and googling and from what I've found, lat/long to UTM frequently shows up, but it seems like nobody has got the library for converting lat/long to MGRS and vice versa.

我想知道存在什么样的库(许可与否),我可以在其中找到在纬度/经度坐标和军事网格参考系统 (mgrs) 坐标之间转换的方法。我没有明确地寻找经纬度到 UTM 转换器,它必须是 mgrs。我已经做了一些搜索和谷歌搜索,从我发现的内容来看,lat/long 到 UTM 经常出现,但似乎没有人拥有将 lat/long 转换为 MGRS 的库,反之亦然。

Does anyone know of any libraries or places where I can find libraries that will do such a thing?

有谁知道任何图书馆或地方我可以找到可以做这种事情的图书馆?

Thanks, any help is appreciated.

谢谢,任何帮助表示赞赏。

tf.rz

tf.rz

采纳答案by Richard Clayton

I know this is an old post, but I'll add some more for posterity. JCoords has an MGRS point conversion, but it requires a small licensing fee. Another source is NASA's WorldWind project (similar to Google Earth), which converted the old GeoTrans API created by NGA. This is the implementation of the MGRS conversion: MGRSCoordConverter.java

我知道这是一个旧帖子,但我会为后代添加更多内容。JCoords 有一个 MGRS 点转换,但它需要少量的许可费用。另一个来源是 NASA 的 WorldWind 项目(类似于 Google Earth),它转换了 NGA 创建的旧 GeoTrans API。这是MGRS转换的实现:MGRSCoordConverter.java

回答by tf.rz

I've found a library called OpenMap in the library com.bbn.openmap.proj.coords they have a class called mgrs point. Props to a few questions on stackoverflow for providing various answers. I've been unable to find the question in this answer, but it got me the answer I was looking for: openmap! However, the latitude longitude conversion to mgrs points seems to be faulty, but the reverse works perfectly fine; and that's what I needed.

我在库 com.bbn.openmap.proj.coords 中找到了一个名为 OpenMap 的库,他们有一个名为 mgrs point 的类。提供有关 stackoverflow 的几个问题的道具,以提供各种答案。我一直无法在这个答案中找到问题,但它让我找到了我正在寻找的答案:openmap!然而,纬度经度转换为 mgrs 点似乎有问题,但反向工作完全正常;这就是我所需要的。

回答by John Jorsett

Another post for posterity, since my search turned up this page as well. There's a Java coordinate conversion class that includes MGRS as well as UTM and lat/lon described and downloadable here: http://www.ibm.com/developerworks/java/library/j-coordconvert/

后人的另一篇文章,因为我的搜索也出现了这个页面。有一个 Java 坐标转换类,其中包括 MGRS 以及 UTM 和 lat/lon 描述和下载:http: //www.ibm.com/developerworks/java/library/j-coordconvert/

A note says, "The download also includes MGRS methods, but conversion from MGRS is incorrect." Perhaps it would serve as a starting point and be fixable.

一个注释说,“下载还包括 MGRS 方法,但从 MGRS 转换是不正确的。” 也许它可以作为一个起点并且是可以修复的。

回答by Steve Brooker

I developed the following (admittedly javascript) functions combining bits from two primary sources plus others on the internet. I have optimised them for speed not understanding!

我开发了以下(无可否认的 javascript)函数,结合了来自两个主要来源和互联网上其他来源的位。我已经优化了它们的速度不理解!

My two primary sources were: http://www.movable-type.co.uk/scripts/latlong-utm-mgrs.htmlhttp://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html

我的两个主要来源是:http: //www.movable-type.co.uk/scripts/latlong-utm-mgrs.html http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html

Example usage : MGRSString (51.172,-1.779) returns "30U WB 85358 69660"

用法示例:MGRSString (51.172,-1.779) 返回“30U WB 85358 69660”

function MGRSString (Lat, Long)
{ 
if (Lat < -80) return 'Too far South' ; if (Lat > 84) return 'Too far North' ;
var c = 1 + Math.floor ((Long+180)/6);
var e = c*6 - 183 ;
var k = Lat*Math.PI/180;
var l = Long*Math.PI/180;
var m = e*Math.PI/180;
var n = Math.cos (k);
var o = 0.006739496819936062*Math.pow (n,2);
var p = 40680631590769/(6356752.314*Math.sqrt(1 + o));
var q = Math.tan (k);
var r = q*q;
var s = (r*r*r) - Math.pow (q,6);
var t = l - m;
var u = 1.0 - r + o;
var v = 5.0 - r + 9*o + 4.0*(o*o);
var w = 5.0 - 18.0*r + (r*r) + 14.0*o - 58.0*r*o;
var x = 61.0 - 58.0*r + (r*r) + 270.0*o - 330.0*r*o;
var y = 61.0 - 479.0*r + 179.0*(r*r) - (r*r*r);
var z = 1385.0 - 3111.0*r + 543.0*(r*r) - (r*r*r);
var aa = p*n*t + (p/6.0*Math.pow (n,3)*u*Math.pow (t,3)) + (p/120.0*Math.pow (n,5)*w*Math.pow (t,5)) + (p/5040.0*Math.pow (n,7)*y*Math.pow (t,7));
var ab = 6367449.14570093*(k - (0.00251882794504*Math.sin (2*k)) + (0.00000264354112*Math.sin (4*k)) - (0.00000000345262*Math.sin (6*k)) + (0.000000000004892*Math.sin (8*k))) + (q/2.0*p*Math.pow (n,2)*Math.pow (t,2)) + (q/24.0*p*Math.pow (n,4)*v*Math.pow (t,4)) + (q/720.0*p*Math.pow (n,6)*x*Math.pow (t,6)) + (q/40320.0*p*Math.pow (n,8)*z*Math.pow (t,8));
aa = aa*0.9996 + 500000.0;
ab = ab*0.9996; if (ab < 0.0) ab += 10000000.0;
var ad = 'CDEFGHJKLMNPQRSTUVWXX'.charAt (Math.floor (Lat/8 + 10));
var ae = Math.floor (aa/100000);
var af = ['ABCDEFGH','JKLMNPQR','STUVWXYZ'][(c-1)%3].charAt (ae-1);
var ag = Math.floor (ab/100000)%20;
var ah = ['ABCDEFGHJKLMNPQRSTUV','FGHJKLMNPQRSTUVABCDE'][(c-1)%2].charAt (ag);
function pad (val) {if (val < 10) {val = '0000' + val} else if (val < 100) {val = '000' + val} else if (val < 1000) {val = '00' + val} else if (val < 10000) {val = '0' + val};return val};
aa = Math.floor (aa%100000); aa = pad (aa);
ab = Math.floor (ab%100000); ab = pad (ab);
return c + ad + ' ' + af + ah + ' ' + aa + ' ' + ab;
};

To convert back from mgrs to lat long use the following function. The input string must have metre (i.e. 5 digit) easing and northing resolution and have spaces as output by the above function.

要将 mgrs 转换回 lat long,请使用以下函数。输入字符串必须具有米(即 5 位)缓动和北移分辨率,并具有作为上述函数输出的空格。

function LatLongFromMGRSstring (a) 
{
var b = a.trim();
b = b.match(/\S+/g);
if (b == null || b.length != 4) return [false,null,null];
var c = (b[0].length < 3) ? b[0][0] : b[0].slice(0,2);
var d = (b[0].length < 3) ? b[0][1] : b[0][2];
var e = (c*6-183)*Math.PI / 180;
var f = ["ABCDEFGH","JKLMNPQR","STUVWXYZ"][(c-1) % 3].indexOf(b[1][0]) + 1;
var g = "CDEFGHJKLMNPQRSTUVWXX".indexOf(d);
var h = ["ABCDEFGHJKLMNPQRSTUV","FGHJKLMNPQRSTUVABCDE"][(c-1) % 2].indexOf(b[1][1]);
var i = [1.1,2.0,2.8,3.7,4.6,5.5,6.4,7.3,8.2,9.1,0,0.8,1.7,2.6,3.5,4.4,5.3,6.2,7.0,7.9];
var j = [0,2,2,2,4,4,6,6,8,8,0,0,0,2,2,4,4,6,6,6];
var k = i[g];
var l = Number(j[g]) + h / 10;
if (l < k) l += 2;
var m = f*100000.0 + Number(b[2]);
var n = l*1000000 + Number(b[3]);
m -= 500000.0;
if (d < 'N') n -= 10000000.0;
m /= 0.9996; n /= 0.9996;
var o = n / 6367449.14570093;
var p = o + (0.0025188266133249035*Math.sin(2.0*o)) + (0.0000037009491206268*Math.sin(4.0*o)) + (0.0000000074477705265*Math.sin(6.0*o)) + (0.0000000000170359940*Math.sin(8.0*o));
var q = Math.tan(p);
var r = q*q;
var s = r*r;
var t = Math.cos(p);
var u = 0.006739496819936062*Math.pow(t,2);
var v = 40680631590769 / (6356752.314*Math.sqrt(1 + u));
var w = v;
var x = 1.0 / (w*t); w *= v;
var y = q / (2.0*w); w *= v;
var z = 1.0 / (6.0*w*t); w *= v;
var aa = q / (24.0*w); w *= v;
var ab = 1.0 / (120.0*w*t); w *= v;
var ac = q / (720.0*w); w *= v;
var ad = 1.0 / (5040.0*w*t); w *= v;
var ae = q / (40320.0*w);
var af = -1.0-u;
var ag = -1.0-2*r-u;
var ah = 5.0 + 3.0*r + 6.0*u-6.0*r*u-3.0*(u*u)-9.0*r*(u*u);
var ai = 5.0 + 28.0*r + 24.0*s + 6.0*u + 8.0*r*u;
var aj = -61.0-90.0*r-45.0*s-107.0*u + 162.0*r*u;
var ak = -61.0-662.0*r-1320.0*s-720.0*(s*r);
var al = 1385.0 + 3633.0*r + 4095.0*s + 1575*(s*r);
var lat = p + y*af*(m*m) + aa*ah*Math.pow(m,4) + ac*aj*Math.pow(m,6) + ae*al*Math.pow(m,8);
var lng = e + x*m + z*ag*Math.pow(m,3) + ab*ai*Math.pow(m,5) + ad*ak*Math.pow(m,7);
lat = lat*180 / Math.PI;
lng = lng*180 / Math.PI;
return [true,lat,lng];
}

回答by IdaM

I support @Richard Clayton answer about NASA WorldWind library. For those who do not want the entire library take a look at minimalized version of Berico Technologies which have extracted the MGRS conversion. https://github.com/Berico-Technologies/Geo-Coordinate-Conversion-Java

我支持@Richard Clayton 关于 NASA WorldWind 图书馆的回答。对于那些不想要整个库的人,请查看已提取 MGRS 转换的 Berico Technologies 的最小化版本。 https://github.com/Berico-Technologies/Geo-Coordinate-Conversion-Java