mgrs.js 1.5 KB

12345678910111213141516171819202122232425262728
  1. (function() {
  2. assert(typeof Proj4js.util.MGRS, "object", "MGRS available in the Proj4js.util namespace");
  3. assert(typeof Proj4js.Point.fromMGRS, "function", "fromMGRS method added to Proj4js.Point prototype");
  4. assert(typeof Proj4js.Point.prototype.toMGRS, "function", "toMGRS method added to Proj4js.Point prototype");
  5. // Conversions cross-tested with http://geographiclib.sourceforge.net/cgi-bin/GeoConvert
  6. var mgrs, point;
  7. mgrs = "33UXP04";
  8. point = Proj4js.Point.fromMGRS(mgrs);
  9. assert(point.x.toPrecision(7), "16.41450", "Longitude of point from MGRS correct.");
  10. assert(point.y.toPrecision(7), "48.24949", "Latitude of point from MGRS correct.");
  11. assert(point.toMGRS(), "33UXP0500444998", "MGRS reference with highest accuracy correct.");
  12. assert(point.toMGRS(1), mgrs, "MGRS reference with 1-digit accuracy correct.");
  13. mgrs = "24XWT783908"; // near UTM zone border, so there are two ways to reference this
  14. point = Proj4js.Point.fromMGRS(mgrs);
  15. assert(point.x.toPrecision(7), "-32.66433", "Longitude of point from MGRS correct.");
  16. assert(point.y.toPrecision(7), "83.62778", "Latitude of point from MGRS correct.");
  17. assert(point.toMGRS(3), "25XEN041865", "MGRS reference with 3-digit accuracy correct.");
  18. function assert(got, expected, msg) {
  19. if (got == expected) {
  20. document.write('<div>' + msg + '</div>');
  21. } else {
  22. document.write('<div style="background-color:red">' + msg + ' - got ' + got + ', but expected ' + expected + '</div>');
  23. }
  24. }
  25. })();