LLtoUTM static method

UTM LLtoUTM(
  1. double lat,
  2. double lon
)

Converts a set of Longitude and Latitude co-ordinates to UTM using the WGS84 ellipsoid.

@private @param {object} ll Object literal with lat and lon properties representing the WGS84 coordinate to be converted. @return {object} Object literal containing the UTM value with easting, northing, zoneNumber and zoneLetter properties, and an optional accuracy property in digits. Returns null if the conversion failed.

Implementation

static UTM LLtoUTM(double lat, double lon) {
  var Lat = lat;
  var Long = lon;
  const a = 6378137; //ellip.radius;
  const eccSquared = 0.00669438; //ellip.eccsq;
  const k0 = 0.9996;
  var LatRad = degToRad(Lat);
  var LongRad = degToRad(Long);
  var ZoneNumber = ((Long + 180) / 6).floor() + 1;
  //Make sure the longitude 180 is in Zone 60
  if (Long == 180) {
    ZoneNumber = 60;
  }
  // Special zone for Norway
  if (Lat >= 56 && Lat < 64 && Long >= 3 && Long < 12) {
    ZoneNumber = 32;
  }
  // Special zones for Svalbard
  if (Lat >= 72 && Lat < 84) {
    if (Long >= 0 && Long < 9) {
      ZoneNumber = 31;
    } else if (Long >= 9 && Long < 21) {
      ZoneNumber = 33;
    } else if (Long >= 21 && Long < 33) {
      ZoneNumber = 35;
    } else if (Long >= 33 && Long < 42) {
      ZoneNumber = 37;
    }
  }
  var LongOrigin =
      ((ZoneNumber - 1) * 6 - 180 + 3).toDouble(); // +3 puts origin
  // in middle of zone
  var LongOriginRad = degToRad(LongOrigin);
  var eccPrimeSquared = (eccSquared) / (1 - eccSquared);
  var N = a / math.sqrt(1 - eccSquared * math.sin(LatRad) * math.sin(LatRad));
  var T = math.tan(LatRad) * math.tan(LatRad);
  var C = eccPrimeSquared * math.cos(LatRad) * math.cos(LatRad);
  var A = math.cos(LatRad) * (LongRad - LongOriginRad);
  var M = a *
      ((1 -
                  eccSquared / 4 -
                  3 * eccSquared * eccSquared / 64 -
                  5 * eccSquared * eccSquared * eccSquared / 256) *
              LatRad -
          (3 * eccSquared / 8 +
                  3 * eccSquared * eccSquared / 32 +
                  45 * eccSquared * eccSquared * eccSquared / 1024) *
              math.sin(2 * LatRad) +
          (15 * eccSquared * eccSquared / 256 +
                  45 * eccSquared * eccSquared * eccSquared / 1024) *
              math.sin(4 * LatRad) -
          (35 * eccSquared * eccSquared * eccSquared / 3072) *
              math.sin(6 * LatRad));
  var UTMEasting = (k0 *
          N *
          (A +
              (1 - T + C) * A * A * A / 6 +
              (5 - 18 * T + T * T + 72 * C - 58 * eccPrimeSquared) *
                  A *
                  A *
                  A *
                  A *
                  A /
                  120) +
      500000);
  var UTMNorthing = (k0 *
      (M +
          N *
              math.tan(LatRad) *
              (A * A / 2 +
                  (5 - T + 9 * C + 4 * C * C) * A * A * A * A / 24 +
                  (61 - 58 * T + T * T + 600 * C - 330 * eccPrimeSquared) *
                      A *
                      A *
                      A *
                      A *
                      A *
                      A /
                      720)));
  if (Lat < 0) {
    UTMNorthing += 10000000; // 10000000 meter offset for
    // southern hemisphere
  }
  var utm = UTM(
    easting: UTMEasting.truncateToDouble(),
    northing: UTMNorthing.truncateToDouble(),
    zoneNumber: ZoneNumber,
    zoneLetter: getLetterDesignator(Lat),
  );
  return utm;
}