private UTMCoord convertMGRSToUTM(String MGRSString){
  double scaled_min_northing;
  double grid_easting;
  double grid_northing;
  double temp_grid_northing=0.0;
  double fabs_grid_northing=0.0;
  double latitude=0.0;
  double longitude=0.0;
  double divisor=1.0;
  long error_code=MGRS_NO_ERROR;
  String hemisphere=AVKey.NORTH;
  double easting=0;
  double northing=0;
  UTMCoord UTM=null;
  MGRSComponents MGRS=breakMGRSString(MGRSString);
  if (MGRS == null)   error_code|=MGRS_STRING_ERROR;
 else {
    if (error_code == MGRS_NO_ERROR) {
      if ((MGRS.latitudeBand == LETTER_X) && ((MGRS.zone == 32) || (MGRS.zone == 34) || (MGRS.zone == 36)))       error_code|=MGRS_STRING_ERROR;
 else {
        if (MGRS.latitudeBand < LETTER_N)         hemisphere=AVKey.SOUTH;
 else         hemisphere=AVKey.NORTH;
        getGridValues(MGRS.zone);
        if ((MGRS.squareLetter1 < ltr2_low_value) || (MGRS.squareLetter1 > ltr2_high_value) || (MGRS.squareLetter2 > LETTER_V))         error_code|=MGRS_STRING_ERROR;
        if (error_code == MGRS_NO_ERROR) {
          grid_northing=(double)(MGRS.squareLetter2) * ONEHT;
          grid_easting=(double)((MGRS.squareLetter1) - ltr2_low_value + 1) * ONEHT;
          if ((ltr2_low_value == LETTER_J) && (MGRS.squareLetter1 > LETTER_O))           grid_easting=grid_easting - ONEHT;
          if (MGRS.squareLetter2 > LETTER_O)           grid_northing=grid_northing - ONEHT;
          if (MGRS.squareLetter2 > LETTER_I)           grid_northing=grid_northing - ONEHT;
          if (grid_northing >= TWOMIL)           grid_northing=grid_northing - TWOMIL;
          error_code=getLatitudeBandMinNorthing(MGRS.latitudeBand);
          if (error_code == MGRS_NO_ERROR) {
            grid_northing=grid_northing - false_northing;
            if (grid_northing < 0.0)             grid_northing+=TWOMIL;
            grid_northing+=northing_offset;
            if (grid_northing < min_northing)             grid_northing+=TWOMIL;
            easting=grid_easting + MGRS.easting;
            northing=grid_northing + MGRS.northing;
            try {
              UTM=UTMCoord.fromUTM(MGRS.zone,hemisphere,easting,northing,globe);
              latitude=UTM.getLatitude().radians;
              divisor=Math.pow(10.0,MGRS.precision);
              error_code=getLatitudeRange(MGRS.latitudeBand);
              if (error_code == MGRS_NO_ERROR) {
                if (!(((south - DEG_TO_RAD / divisor) <= latitude) && (latitude <= (north + DEG_TO_RAD / divisor))))                 error_code|=MGRS_LAT_WARNING;
              }
            }
 catch (            Exception e) {
              error_code=MGRS_UTM_ERROR;
            }
          }
        }
      }
    }
  }
  last_error=error_code;
  if (error_code == MGRS_NO_ERROR || error_code == MGRS_LAT_WARNING)   return UTM;
  return null;
}

private UTMCoord convertMGRSToUTM ( String MGRSString ) { double scaled_min_northing ; double grid_easting ; double grid_northing ; double temp_grid_northing = 0.0 ; double fabs_grid_northing = 0.0 ; double latitude = 0.0 ; double longitude = 0.0 ; double divisor = 1.0 ; long error_code = MGRS_NO_ERROR ; String hemisphere = AVKey . NORTH ; double easting = 0 ; double northing = 0 ; UTMCoord UTM = null ; MGRSComponents MGRS = breakMGRSString ( MGRSString ) ; if ( MGRS == null ) error_code |= MGRS_STRING_ERROR ; else { if ( error_code == MGRS_NO_ERROR ) { if ( ( MGRS . latitudeBand == LETTER_X ) && ( ( MGRS . zone == 32 ) || ( MGRS . zone == 34 ) || ( MGRS . zone == 36 ) ) ) error_code |= MGRS_STRING_ERROR ; else { if ( MGRS . latitudeBand < LETTER_N ) hemisphere = AVKey . SOUTH ; else hemisphere = AVKey . NORTH ; getGridValues ( MGRS . zone ) ; if ( ( MGRS . squareLetter1 < ltr2_low_value ) || ( MGRS . squareLetter1 > ltr2_high_value ) || ( MGRS . squareLetter2 > LETTER_V ) ) error_code |= MGRS_STRING_ERROR ; if ( error_code == MGRS_NO_ERROR ) { grid_northing = ( double ) ( MGRS . squareLetter2 ) * ONEHT ; grid_easting = ( double ) ( ( MGRS . squareLetter1 ) - ltr2_low_value + 1 ) * ONEHT ; if ( ( ltr2_low_value == LETTER_J ) && ( MGRS . squareLetter1 > LETTER_O ) ) grid_easting = grid_easting - ONEHT ; if ( MGRS . squareLetter2 > LETTER_O ) grid_northing = grid_northing - ONEHT ; if ( MGRS . squareLetter2 > LETTER_I ) grid_northing = grid_northing - ONEHT ; if ( grid_northing >= TWOMIL ) grid_northing = grid_northing - TWOMIL ; error_code = getLatitudeBandMinNorthing ( MGRS . latitudeBand ) ; if ( error_code == MGRS_NO_ERROR ) { grid_northing = grid_northing - false_northing ; if ( grid_northing < 0.0 ) grid_northing += TWOMIL ; grid_northing += northing_offset ; if ( grid_northing < min_northing ) grid_northing += TWOMIL ; easting = grid_easting + MGRS . easting ; northing = grid_northing + MGRS . northing ; try { UTM = UTMCoord . fromUTM ( MGRS . zone , hemisphere , easting , northing , globe ) ; latitude = UTM . getLatitude ( ) . radians ; divisor = Math . pow ( 10.0 , MGRS . precision ) ; error_code = getLatitudeRange ( MGRS . latitudeBand ) ; if ( error_code == MGRS_NO_ERROR ) { if ( ! ( ( ( south - DEG_TO_RAD / divisor ) <= latitude ) && ( latitude <= ( north + DEG_TO_RAD / divisor ) ) ) ) error_code |= MGRS_LAT_WARNING ; } } catch ( Exception e ) { error_code = MGRS_UTM_ERROR ; } } } } } } last_error = error_code ; if ( error_code == MGRS_NO_ERROR || error_code == MGRS_LAT_WARNING ) return UTM ; return null ; }
the function convertmgrstoutm converts an mgrs coordinate string to utm projection ( zone , hemisphere , easting and northing ) coordinates according to the current ellipsoid parameters . updates last_error if any errors occured .
