Mgrs constructor

Mgrs(
  1. int lonZone,
  2. String band,
  3. String column,
  4. String row,
  5. int easting,
  6. int northing, {
  7. Datum datum = Datum.WGS84,
})

Creates a MGRS grid reference with lonZone, band, column, row, easting, northing based on the datum (used in the UTM projection).

The lonZone represents UTM 6° longitudinal zone (1..60 covering 180°W..180°E).

The band represents 8° latitudinal band (C..X covering 80°S..84°N).

The column (or "e100k") represents the first letter (E) of a 100km grid square. Allowed letter characters are A..Z, omitting I and O.

The row (or "n100k") represents the second letter (N) of a 100km grid square. Allowed letter characters are A..V, omitting I and O.

The easting (x) in metres within a 100km grid square.

The northing (y) in metres within a 100km grid square.

Use datum to set the datum for calculations with a reference ellipsoid and datum transformation parameters.

Throws a FormatException if MGRS zone, band, column, row, easting or northing is invalid.

Examples:

  // MGRS grid reference with zone 31, band U, 100 km grid DQ and WGS84
  // datum (easting 48251, northing 11932 within the grid cell).
  //
  // This equals to the MGRS grid reference '31U DQ 48251 11932'.
  final mgrsRef = Mgrs(31, 'U', 'D', 'Q', 48251, 11932);

Implementation

factory Mgrs(
  int lonZone,
  String band,
  String column,
  String row,
  int easting,
  int northing, {
  Datum datum = Datum.WGS84,
}) {
  // create 100 km grid square, validating zone, band and 100km grid square
  // letters (column + row)
  final gridSquare = MgrsGridSquare(
    lonZone,
    band,
    column, // e100k
    row, // n100k
  );

  // validate easting and northing
  final errors = <String>[];
  if (easting < 0 || easting > 99999) {
    errors.add('invalid MGRS easting `$easting`');
  }
  if (northing < 0 || northing > 99999) {
    errors.add('invalid MGRS northing `$northing`');
  }
  if (errors.isNotEmpty) {
    throw FormatException(errors.join(', '));
  }

  return Mgrs._coordinates(
    gridSquare,
    easting,
    northing,
    datum: datum,
  );
}