long Convert_OSSIM_MGRS_To_Geodetic (const char* OSSIM_MGRS, double *Latitude, double *Longitude) /* * OSSIM_MGRS : OSSIM_MGRS coordinate string (output) * Latitude : Latitude in radians (input) * Longitude : Longitude in radians (input) * */ { /* Convert_OSSIM_MGRS_To_Geodetic */ long error_code = OSSIM_MGRS_NO_ERROR; long Zone; long Letters[OSSIM_MGRS_LETTERS]; char Hemisphere; double Easting; double Northing; long In_Precision; error_code = Break_OSSIM_MGRS_String (OSSIM_MGRS, &Zone, Letters, &Easting, &Northing, &In_Precision); if (!error_code) { if (Zone) { error_code |= Convert_OSSIM_MGRS_To_UTM (OSSIM_MGRS, &Zone, &Hemisphere, &Easting, &Northing); Set_UTM_Parameters (OSSIM_MGRS_a, OSSIM_MGRS_f, 0); error_code |= Convert_UTM_To_Geodetic (Zone, Hemisphere, Easting, Northing, Latitude, Longitude); } else { error_code |= Convert_OSSIM_MGRS_To_UPS (OSSIM_MGRS, &Hemisphere, &Easting, &Northing); Set_UPS_Parameters (OSSIM_MGRS_a, OSSIM_MGRS_f); error_code |= Convert_UPS_To_Geodetic (Hemisphere, Easting, Northing, Latitude, Longitude); } } return (error_code); } /* END OF Convert_OSSIM_MGRS_To_Geodetic */
void GRID_UPS (long *Letters, char *Hemisphere, double *Easting, double *Northing, long *Error) { /* BEGIN GRID_UPS */ double feltr=0.0; /* False easting for 2nd letter */ double fnltr=0.0; /* False northing for 3rd letter */ // double sleast=0.0; /* Longitude east limit - UTM */ // double slwest=0.0; /* Longitude west limit -UTM */ // double spnor=0.0; /* North latitude limits based on 1st letter */ double spsou=0.0; /* South latitude limits based on 1st letter */ double x=0.0; /* easting */ double xltr=0.0; /* easting for 100,000 meter grid square */ double xnum=0.0; /* easting part of MGRS */ double y=0.0; /* northing */ double yltr=0.0; /* northing for 100,000 meter grid square */ double ynum=0.0; /* northing part of MGRS */ // long izone=0; /* Zone number */ long ltrhi=0; /* 2nd letter range - high number */ long ltrhy=0; /* 3rd letter range - high number (UPS) */ long ltrlow=0; /* 2nd letter range - low number */ long sign=0; double sphi=0.0; double slam=0.0; if ((Letters[0] == LETTER_Y) || (Letters[0] == LETTER_Z)) { spsou = MAX_UTM_LAT; sign = 1; } else { spsou = MIN_UTM_LAT; sign = -1; } slam = PI / 2.e0; if ((Letters[0] == LETTER_Y) || (Letters[0] == LETTER_A)) { slam = -slam; } // izone = 0; sphi = spsou; Set_UPS_Parameters(OSSIM_MGRS_a,OSSIM_MGRS_f); Convert_Geodetic_To_UPS(sphi,slam,Hemisphere,&x,&y); // spnor = sphi; // sleast = slam; // slwest = slam; UPSSET(Letters[0], <rlow, <rhi, &feltr, &fnltr, <rhy); LTR2UPS(Letters, ltrlow, ltrhi, ltrhy, Error, &xltr, &yltr, fnltr, feltr, &x, &y, sign); xnum = *Easting; ynum = *Northing; y = (yltr + ynum); x = xltr + xnum; *Easting = x; *Northing = y; return; } /* END OF GRID_UPS */
long Convert_Geodetic_To_OSSIM_MGRS (double Latitude, double Longitude, long Precision, char* OSSIM_MGRS) /* * Latitude : Latitude in radians (input) * Longitude : Longitude in radians (input) * OSSIM_MGRS : OSSIM_MGRS coordinate string (output) * */ { /* Convert_Geodetic_To_OSSIM_MGRS */ long error_code = OSSIM_MGRS_NO_ERROR; long zone; char hemisphere; double easting; double northing; if ((Latitude < -PI_OVER_2) || (Latitude > PI_OVER_2)) { /* Latitude out of range */ error_code |= OSSIM_MGRS_LAT_ERROR; } if ((Longitude < -PI) || (Longitude > (2*PI))) { /* Longitude out of range */ error_code |= OSSIM_MGRS_LON_ERROR; } if ((Precision < 0) || (Precision > MAX_PRECISION)) error_code |= OSSIM_MGRS_PRECISION_ERROR; if (!error_code) { if ((Latitude < MIN_UTM_LAT) || (Latitude > MAX_UTM_LAT)) { Set_UPS_Parameters (OSSIM_MGRS_a, OSSIM_MGRS_f); error_code |= Convert_Geodetic_To_UPS (Latitude, Longitude, &hemisphere, &easting, &northing); error_code |= Convert_UPS_To_OSSIM_MGRS (hemisphere, easting, northing, Precision, OSSIM_MGRS); } else { Set_UTM_Parameters (OSSIM_MGRS_a, OSSIM_MGRS_f, 0); error_code |= Convert_Geodetic_To_UTM (Latitude, Longitude, &zone, &hemisphere, &easting, &northing); error_code |= Convert_UTM_To_OSSIM_MGRS (zone, hemisphere, easting, northing, Precision, OSSIM_MGRS); } } return (error_code); } /* Convert_Geodetic_To_OSSIM_MGRS */
long Convert_MGRS_To_Geodetic (char* MGRS, double *Latitude, double *Longitude) /* * The function Convert_MGRS_To_Geodetic converts an MGRS coordinate string * to Geodetic (latitude and longitude) coordinates * according to the current ellipsoid parameters. If any errors occur, * the error code(s) are returned by the function, otherwise UTM_NO_ERROR * is returned. * * MGRS : MGRS coordinate string (input) * Latitude : Latitude in radians (output) * Longitude : Longitude in radians (output) * */ { /* Convert_MGRS_To_Geodetic */ long zone; char hemisphere; double easting; double northing; long zone_exists; long temp_error_code = MGRS_NO_ERROR; long error_code = MGRS_NO_ERROR; error_code = Check_Zone(MGRS, &zone_exists); if (!error_code) { if (zone_exists) { error_code |= Convert_MGRS_To_UTM (MGRS, &zone, &hemisphere, &easting, &northing); if(!error_code || (error_code & MGRS_LAT_WARNING)) { temp_error_code = Set_UTM_Parameters (MGRS_a, MGRS_f, 0); if(!temp_error_code) { temp_error_code = Convert_UTM_To_Geodetic (zone, hemisphere, easting, northing, Latitude, Longitude); if(temp_error_code) { if((temp_error_code & UTM_ZONE_ERROR) || (temp_error_code & UTM_HEMISPHERE_ERROR)) error_code |= MGRS_STRING_ERROR; if(temp_error_code & UTM_EASTING_ERROR) error_code |= MGRS_EASTING_ERROR; if(temp_error_code & UTM_NORTHING_ERROR) error_code |= MGRS_NORTHING_ERROR; } } else { if(temp_error_code & UTM_A_ERROR) error_code |= MGRS_A_ERROR; if(temp_error_code & UTM_INV_F_ERROR) error_code |= MGRS_INV_F_ERROR; if(temp_error_code & UTM_ZONE_OVERRIDE_ERROR) error_code |= MGRS_ZONE_ERROR; } } } else { error_code |= Convert_MGRS_To_UPS (MGRS, &hemisphere, &easting, &northing); if(!error_code) { temp_error_code = Set_UPS_Parameters (MGRS_a, MGRS_f); if(!temp_error_code) { temp_error_code = Convert_UPS_To_Geodetic (hemisphere, easting, northing, Latitude, Longitude); if(temp_error_code) { if(temp_error_code & UPS_HEMISPHERE_ERROR) error_code |= MGRS_STRING_ERROR; if(temp_error_code & UPS_EASTING_ERROR) error_code |= MGRS_EASTING_ERROR; if(temp_error_code & UPS_LAT_ERROR) error_code |= MGRS_NORTHING_ERROR; } } else { if(temp_error_code & UPS_A_ERROR) error_code |= MGRS_A_ERROR; if(temp_error_code & UPS_INV_F_ERROR) error_code |= MGRS_INV_F_ERROR; } } } } return (error_code); } /* END OF Convert_MGRS_To_Geodetic */
long Convert_Geodetic_To_MGRS (double Latitude, double Longitude, long Precision, char* MGRS) /* * The function Convert_Geodetic_To_MGRS converts Geodetic (latitude and * longitude) coordinates to an MGRS coordinate string, according to the * current ellipsoid parameters. If any errors occur, the error code(s) * are returned by the function, otherwise MGRS_NO_ERROR is returned. * * Latitude : Latitude in radians (input) * Longitude : Longitude in radians (input) * Precision : Precision level of MGRS string (input) * MGRS : MGRS coordinate string (output) * */ { /* Convert_Geodetic_To_MGRS */ long zone; char hemisphere; double easting; double northing; long temp_error_code = MGRS_NO_ERROR; long error_code = MGRS_NO_ERROR; if ((Latitude < -PI_OVER_2) || (Latitude > PI_OVER_2)) { /* Latitude out of range */ error_code |= MGRS_LAT_ERROR; } if ((Longitude < -PI) || (Longitude > (2*PI))) { /* Longitude out of range */ error_code |= MGRS_LON_ERROR; } if ((Precision < 0) || (Precision > MAX_PRECISION)) error_code |= MGRS_PRECISION_ERROR; if (!error_code) { if ((Latitude < MIN_UTM_LAT) || (Latitude > MAX_UTM_LAT)) { temp_error_code = Set_UPS_Parameters (MGRS_a, MGRS_f); if(!temp_error_code) { temp_error_code = Convert_Geodetic_To_UPS (Latitude, Longitude, &hemisphere, &easting, &northing); if(!temp_error_code) { error_code |= Convert_UPS_To_MGRS (hemisphere, easting, northing, Precision, MGRS); } else { if(temp_error_code & UPS_LAT_ERROR) error_code |= MGRS_LAT_ERROR; if(temp_error_code & UPS_LON_ERROR) error_code |= MGRS_LON_ERROR; } } else { if(temp_error_code & UPS_A_ERROR) error_code |= MGRS_A_ERROR; if(temp_error_code & UPS_INV_F_ERROR) error_code |= MGRS_INV_F_ERROR; } } else { temp_error_code = Set_UTM_Parameters (MGRS_a, MGRS_f, 0); if(!temp_error_code) { temp_error_code = Convert_Geodetic_To_UTM (Latitude, Longitude, &zone, &hemisphere, &easting, &northing); if(!temp_error_code) error_code |= UTM_To_MGRS (zone, hemisphere, Longitude, Latitude, easting, northing, Precision, MGRS); else { if(temp_error_code & UTM_LAT_ERROR) error_code |= MGRS_LAT_ERROR; if(temp_error_code & UTM_LON_ERROR) error_code |= MGRS_LON_ERROR; if(temp_error_code & UTM_ZONE_OVERRIDE_ERROR) error_code |= MGRS_ZONE_ERROR; if(temp_error_code & UTM_EASTING_ERROR) error_code |= MGRS_EASTING_ERROR; if(temp_error_code & UTM_NORTHING_ERROR) error_code |= MGRS_NORTHING_ERROR; } } else { if(temp_error_code & UTM_A_ERROR) error_code |= MGRS_A_ERROR; if(temp_error_code & UTM_INV_F_ERROR) error_code |= MGRS_INV_F_ERROR; if(temp_error_code & UTM_ZONE_OVERRIDE_ERROR) error_code |= MGRS_ZONE_ERROR; } } } return (error_code); } /* Convert_Geodetic_To_MGRS */