Mgrs.fromUtm constructor
Mgrs.fromUtm(
- Utm utm
Creates a MGRS grid reference from projected UTM coordinates.
May throw a FormatException if conversion fails.
Examples:
final utmCoord = Utm(31, 'N', 448251, 5411932);
final mgrsRef = Mgrs.fromUtm(utmCoord); // 31U DQ 48251 11932
Implementation
factory Mgrs.fromUtm(Utm utm) {
// MGRS zone is same as UTM zone
final lonZone = utm.zone.lonZone;
// convert UTM to lat/long to get latitude to determine band
final latlong = utm.toGeographic();
// grid zones are 8° tall, 0°N is 10th band
final band = _latBands[(latlong.lat / 8 + 10)
.floor()
.clamp(0, _latBands.length - 1)]; // latitude band
// columns in zone 1 are A-H, zone 2 J-R, zone 3 S-Z, then repeating every
// 3rd zone
final col = (utm.easting / 100000).floor();
// (note -1 because eastings start at 166e3 due to 500km false origin)
final e100k = _columnLetters[(lonZone - 1) % 3][col - 1];
// rows in even zones are A-V, in odd zones are F-E
final row = (utm.northing / 100000).floor() % 20;
final n100k = _rowLetters[(lonZone - 1) % 2][row];
// truncate easting/northing to within 100km grid square & round to 1-metre
// precision
final easting = (utm.easting % 100000).floor();
final northing = (utm.northing % 100000).floor();
return Mgrs(
lonZone,
band,
e100k,
n100k,
easting,
northing,
datum: utm.datum,
);
}