
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How to convert Military Grid Reference System (MGRS) to Latitude,Longitude?
MGRS | LATITUDE | LONGITUDE |
14SPB800720 | 33.172 | -97.069 |
14SQB084140 | 32.644 | -96.778 |
14SQB091410 | 32.888 | -96.764 |
14SQB086520 | 32.987 | -96.767 |
14SQB074850 | 33.285 | -96.772 |
14SQB056720 | 33.168 | -96.795 |
14SQB047900 | 33.330 | -96.800 |
14SQB047890 | 33.321 | -96.800 |
14SQB047270 | 32.762 | -96.814 |
context.myResult = String.valueOf(cs.convertMGRSToGeodetic("14SPB800720"));
* The function ConvertMGRSToGeodetic 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.
*
* @rchinta MGRSString MGRS coordinate string.
*
* @return the error code.
*/
public long convertMGRSToGeodetic(String MGRSString)
{
latitude = 0;
longitude = 0;
long error_code = checkZone(MGRSString);
if (error_code == MGRS_NO_ERROR)
{
UTMCoord UTM = convertMGRSToUTM(MGRSString);
if (UTM != null)
{
latitude = UTM.getLatitude().radians;
longitude = UTM.getLongitude().radians;
}
else
error_code = MGRS_UTM_ERROR;
}
else if (error_code == MGRS_NOZONE_WARNING)
{
UPSCoord UPS = convertMGRSToUPS(MGRSString);
if (UPS != null)
{
latitude = UPS.getLatitude().radians;
longitude = UPS.getLongitude().radians;
}
else
error_code = MGRS_UPS_ERROR;
}
return (error_code);
}
public double getLatitude()
{
return latitude;
}
public double getLongitude()
{
return longitude;
}
Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
After adding the parts in bold font the radians result is now converted to degrees:
System.out.println( "Output LAT/LONG:"+ String.format("%s, %s", cs.getLatitude()*180/3.14159265, cs.getLongitude()*180/3.14159265));

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
you have to import your library first.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Am I doing something wrong?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am finally able call the routine when I did this:
With the job CLOSED, right click the job in the repository.
Then click "Setup routine dependencies".
Select the custom routine.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I am getting output now, but the results are in radians instead of degrees:
Input MGRS: 14SQC6530479576
Output LAT/LONG: 0.5955656176832095, -1.6776699450841455
#Results I am trying to get:
input = 14SQC6530479576
lat lon = 34.123,-96.123
#tJava:
MGRSCoordConverter cs = new MGRSCoordConverter();
context.myResult = String.valueOf(cs.convertMGRSToGeodetic("14SQC6530479576"));
System.out.println(String.format("%s, %s", cs.getLatitude(), cs.getLongitude() ));
#routine
package routines;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.geom.Angle;
import gov.nasa.worldwind.geom.coords.UPSCoord;
To see the whole post, download it here
OriginalPost.pdf

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
{
double grid_easting; /* Easting for 100,000 meter grid square */
double grid_northing; /* Northing for 100,000 meter grid square */
double latitude = 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; // smithjl commented out + false_northing;
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)
{
/*smithjl Deleted code here and added this*/
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;
/* smithjl End of added code */
easting = grid_easting + MGRS.easting;
northing = grid_northing + MGRS.northing;
try
{
UTM = UTMCoord.fromUTM(MGRS.zone, hemisphere, easting, northing);
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;
} /* Convert_MGRS_To_UTM */
public long convertGeodeticToMGRS(double latitude, double longitude, int precision)
{
MGRSString = "";
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 == MGRS_NO_ERROR)
{
if ((latitude < MIN_UTM_LAT) || (latitude > MAX_UTM_LAT))
{
try
{
UPSCoord UPS =
UPSCoord.fromLatLon(Angle.fromRadians(latitude), Angle.fromRadians(longitude));
error_code |= convertUPSToMGRS(UPS.getHemisphere(), UPS.getEasting(),
UPS.getNorthing(), precision);
}
catch (Exception e)
{
error_code = MGRS_UPS_ERROR;
}
}
else
{
try
{
UTMCoord UTM =
UTMCoord.fromLatLon(Angle.fromRadians(latitude), Angle.fromRadians(longitude));
error_code |= convertUTMToMGRS(UTM.getZone(), latitude, UTM.getEasting(),
UTM.getNorthing(), precision);
}
catch (Exception e)
{
error_code = MGRS_UTM_ERROR;
}
}
}
return error_code;
}
/** @return converted MGRS string */
public String getMGRSString()
{
return MGRSString;
}
private long convertUPSToMGRS(String Hemisphere, Double Easting, Double Northing, long Precision)
{
double false_easting; /* False easting for 2nd letter */
double false_northing; /* False northing for 3rd letter */
double grid_easting; /* Easting used to derive 2nd letter of MGRS */
double grid_northing; /* Northing used to derive 3rd letter of MGRS */
int ltr2_low_value; /* 2nd letter range - low number */
long[] letters = new long[MGRS_LETTERS]; /* Number location of 3 letters in alphabet */
double divisor;
int index;
long error_code = MGRS_NO_ERROR;
if (!AVKey.NORTH.equals(Hemisphere) && !AVKey.SOUTH.equals(Hemisphere))
error_code |= MGRS_HEMISPHERE_ERROR;
if ((Easting < MIN_EAST_NORTH) || (Easting > MAX_EAST_NORTH))
error_code |= MGRS_EASTING_ERROR;
if ((Northing < MIN_EAST_NORTH) || (Northing > MAX_EAST_NORTH))
error_code |= MGRS_NORTHING_ERROR;
if ((Precision < 0) || (Precision > MAX_PRECISION))
error_code |= MGRS_PRECISION_ERROR;
if (error_code == MGRS_NO_ERROR)
{
divisor = Math.pow(10.0, (5 - Precision));
Easting = roundMGRS(Easting / divisor) * divisor;
Northing = roundMGRS(Northing / divisor) * divisor;
if (AVKey.NORTH.equals(Hemisphere))
{
if (Easting >= TWOMIL)
letters[0] = LETTER_Z;
else
letters[0] = LETTER_Y;
index = (int) letters[0] - 22;
// ltr2_low_value = UPS_Constant_Table.get(index).ltr2_low_value;
// false_easting = UPS_Constant_Table.get(index).false_easting;
// false_northing = UPS_Constant_Table.get(index).false_northing;
ltr2_low_value = (int) upsConstants[index][1];
false_easting = (double) upsConstants[index][4];
false_northing = (double) upsConstants[index][5];
}
else // AVKey.SOUTH.equals(Hemisphere)
{
if (Easting >= TWOMIL)
letters[0] = LETTER_B;
else
letters[0] = LETTER_A;
// ltr2_low_value = UPS_Constant_Table.get((int) letters[0]).ltr2_low_value;
// false_easting = UPS_Constant_Table.get((int) letters[0]).false_easting;
// false_northing = UPS_Constant_Table.get((int) letters[0]).false_northing;
ltr2_low_value = (int) upsConstants[(int) letters[0]][1];
false_easting = (double) upsConstants[(int) letters[0]][4];
false_northing = (double) upsConstants[(int) letters[0]][5];
}
grid_northing = Northing;
grid_northing = grid_northing - false_northing;
letters[2] = (int) (grid_northing / ONEHT);
if (letters[2] > LETTER_H)
letters[2] = letters[2] + 1;
if (letters[2] > LETTER_N)
letters[2] = letters[2] + 1;
grid_easting = Easting;
grid_easting = grid_easting - false_easting;
letters[1] = (int) ltr2_low_value + ((int) (grid_easting / ONEHT));
if (Easting < TWOMIL)
{
if (letters[1] > LETTER_L)
letters[1] = letters[1] + 3;
if (letters[1] > LETTER_U)
letters[1] = letters[1] + 2;
}
else
{
if (letters[1] > LETTER_C)
letters[1] = letters[1] + 2;
if (letters[1] > LETTER_H)
letters[1] = letters[1] + 1;
if (letters[1] > LETTER_L)
letters[1] = letters[1] + 3;
}
makeMGRSString(0, letters, Easting, Northing, Precision);
}
return (error_code);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
{
double grid_easting; /* Easting used to derive 2nd letter of MGRS */
double grid_northing; /* Northing used to derive 3rd letter of MGRS */
long[] letters = new long[MGRS_LETTERS]; /* Number location of 3 letters in alphabet */
double divisor;
long error_code;
/* Round easting and northing values */
divisor = Math.pow(10.0, (5 - Precision));
Easting = roundMGRS(Easting / divisor) * divisor;
Northing = roundMGRS(Northing / divisor) * divisor;
getGridValues(Zone);
error_code = getLatitudeLetter(Latitude);
letters[0] = getLastLetter();
if (error_code == MGRS_NO_ERROR)
{
grid_northing = Northing;
if (grid_northing == 1.e7)
grid_northing = grid_northing - 1.0;
while (grid_northing >= TWOMIL)
{
grid_northing = grid_northing - TWOMIL;
}
grid_northing = grid_northing + false_northing; //smithjl
if (grid_northing >= TWOMIL) //smithjl
grid_northing = grid_northing - TWOMIL; //smithjl
letters[2] = (long) (grid_northing / ONEHT);
if (letters[2] > LETTER_H)
letters[2] = letters[2] + 1;
if (letters[2] > LETTER_N)
letters[2] = letters[2] + 1;
grid_easting = Easting;
if (((letters[0] == LETTER_V) && (Zone == 31)) && (grid_easting == 500000.0))
grid_easting = grid_easting - 1.0; /* SUBTRACT 1 METER */
letters[1] = ltr2_low_value + ((long) (grid_easting / ONEHT) - 1);
if ((ltr2_low_value == LETTER_J) && (letters[1] > LETTER_N))
letters[1] = letters[1] + 1;
makeMGRSString(Zone, letters, Easting, Northing, Precision);
}
return error_code;
}
private void getGridValues(long zone)
{
long set_number; /* Set number (1-6) based on UTM zone number */
long aa_pattern; /* Pattern based on ellipsoid code */
set_number = zone % 6;
if (set_number == 0)
set_number = 6;
if (MGRS_Ellipsoid_Code.compareTo(CLARKE_1866) == 0 || MGRS_Ellipsoid_Code.compareTo(CLARKE_1880) == 0 ||
MGRS_Ellipsoid_Code.compareTo(BESSEL_1841) == 0 || MGRS_Ellipsoid_Code.compareTo(BESSEL_1841_NAMIBIA) == 0)
aa_pattern = 0L;
else
aa_pattern = 1L;
if ((set_number == 1) || (set_number == 4))
{
ltr2_low_value = (long) LETTER_A;
ltr2_high_value = (long) LETTER_H;
}
else if ((set_number == 2) || (set_number == 5))
{
ltr2_low_value = (long) LETTER_J;
ltr2_high_value = (long) LETTER_R;
}
else if ((set_number == 3) || (set_number == 6))
{
ltr2_low_value = (long) LETTER_S;
ltr2_high_value = (long) LETTER_Z;
}
/* False northing at A for second letter of grid square */
if (aa_pattern == 1L)
{
if ((set_number % 2) == 0)
false_northing = 500000.0; //smithjl was 1500000
else
false_northing = 0.0;
}
else
{
if ((set_number % 2) == 0)
false_northing = 1500000.0; //smithjl was 500000
else
false_northing = 1000000.00;
}
}
private long getLatitudeLetter(double latitude)
{
double temp;
long error_code = MGRS_NO_ERROR;
double lat_deg = latitude * RAD_TO_DEG;
if (lat_deg >= 72 && lat_deg < 84.5)
lastLetter = (long) LETTER_X;
else if (lat_deg > -80.5 && lat_deg < 72)
{
temp = ((latitude + (80.0 * DEG_TO_RAD)) / (8.0 * DEG_TO_RAD)) + 1.0e-12;
// lastLetter = Latitude_Band_Table.get((int) temp).letter;
lastLetter = (long) latitudeBandConstants[(int) temp][0];
}
else
error_code |= MGRS_LAT_ERROR;
return error_code;
}
private double roundMGRS(double value)
{
double ivalue = Math.floor(value);
long ival;
double fraction = value - ivalue;
// double fraction = modf (value, &ivalue);
ival = (long) (ivalue);
if ((fraction > 0.5) || ((fraction == 0.5) && (ival % 2 == 1)))
ival++;
return (double) ival;
}
private long makeMGRSString(long Zone, long[] Letters, double Easting, double Northing, long Precision)
{
int j;
double divisor;
long east;
long north;
long error_code = MGRS_NO_ERROR;
if (Zone != 0)
MGRSString = String.format("%02d", Zone);
else
MGRSString = " ";
for (j = 0; j < 3; j++)
{
if (Letters[j] < 0 || Letters[j] > 26)
return MGRS_ZONE_ERROR;
MGRSString = MGRSString + alphabet.charAt((int) Letters[j]);
}
divisor = Math.pow(10.0, (5 - Precision));
Easting = Easting % 100000.0;
if (Easting >= 99999.5)
Easting = 99999.0;
east = (long) (Easting / divisor);
// Here we need to only use the number requesting in the precision
Integer iEast = (int) east;
String sEast = iEast.toString();
if (sEast.length() > Precision)
sEast = sEast.substring(0, (int) Precision - 1);
else
{
int i;
int length = sEast.length();
for (i = 0; i < Precision - length; i++)
{
sEast = "0" + sEast;
}
}
MGRSString = MGRSString + " " + sEast;
Northing = Northing % 100000.0;
if (Northing >= 99999.5)
Northing = 99999.0;
north = (long) (Northing / divisor);
Integer iNorth = (int) north;
String sNorth = iNorth.toString();
if (sNorth.length() > Precision)
sNorth = sNorth.substring(0, (int) Precision - 1);
else
{
int i;
int length = sNorth.length();
for (i = 0; i < Precision - length; i++)
{
sNorth = "0" + sNorth;
}
}
MGRSString = MGRSString + " " + sNorth;
return (error_code);
}
public long getError()
{
return last_error;
}
private UPSCoord convertMGRSToUPS(String MGRS)
{
long ltr2_high_value; /* 2nd letter range - high number */
long ltr3_high_value; /* 3rd letter range - high number (UPS) */
long ltr2_low_value; /* 2nd letter range - low number */
double false_easting; /* False easting for 2nd letter */
double false_northing; /* False northing for 3rd letter */
double grid_easting; /* easting for 100,000 meter grid square */
double grid_northing; /* northing for 100,000 meter grid square */
int index = 0;
long error_code = MGRS_NO_ERROR;
String hemisphere;
double easting, northing;
MGRSComponents mgrs = breakMGRSString(MGRS);
if (mgrs == null)
error_code = this.last_error;
if (mgrs != null && mgrs.zone > 0)
error_code |= MGRS_STRING_ERROR;
if (error_code == MGRS_NO_ERROR)
{
easting = mgrs.easting;
northing = mgrs.northing;
if (mgrs.latitudeBand >= LETTER_Y)
{
hemisphere = AVKey.NORTH;
index = mgrs.latitudeBand - 22;
ltr2_low_value = upsConstants[index][1]; //.ltr2_low_value;
ltr2_high_value = upsConstants[index][2]; //.ltr2_high_value;
ltr3_high_value = upsConstants[index][3]; //.ltr3_high_value;
false_easting = upsConstants[index][4]; //.false_easting;
false_northing = upsConstants[index][5]; //.false_northing;
}
else
{
hemisphere = AVKey.SOUTH;
ltr2_low_value = upsConstants[mgrs.latitudeBand][12]; //.ltr2_low_value;
ltr2_high_value = upsConstants[mgrs.latitudeBand][2]; //.ltr2_high_value;
ltr3_high_value = upsConstants[mgrs.latitudeBand][3]; //.ltr3_high_value;
false_easting = upsConstants[mgrs.latitudeBand][4]; //.false_easting;
false_northing = upsConstants[mgrs.latitudeBand][5]; //.false_northing;
}
// Check that the second letter of the MGRS string is within
// the range of valid second letter values
// Also check that the third letter is valid
if ((mgrs.squareLetter1 < ltr2_low_value) || (mgrs.squareLetter1 > ltr2_high_value) ||
((mgrs.squareLetter1 == LETTER_D) || (mgrs.squareLetter1 == LETTER_E) ||
(mgrs.squareLetter1 == LETTER_M) || (mgrs.squareLetter1 == LETTER_N) ||
(mgrs.squareLetter1 == LETTER_V) || (mgrs.squareLetter1 == LETTER_W)) ||
(mgrs.squareLetter2 > ltr3_high_value))
error_code = MGRS_STRING_ERROR;
if (error_code == MGRS_NO_ERROR)
{
grid_northing = (double) mgrs.squareLetter2 * ONEHT + false_northing;
if (mgrs.squareLetter2 > LETTER_I)
grid_northing = grid_northing - ONEHT;
if (mgrs.squareLetter2 > LETTER_O)
grid_northing = grid_northing - ONEHT;
grid_easting = (double) ((mgrs.squareLetter1) - ltr2_low_value) * ONEHT + false_easting;
if (ltr2_low_value != LETTER_A)
{
if (mgrs.squareLetter1 > LETTER_L)
grid_easting = grid_easting - 300000.0;
if (mgrs.squareLetter1 > LETTER_U)
grid_easting = grid_easting - 200000.0;
}
else
{
if (mgrs.squareLetter1 > LETTER_C)
grid_easting = grid_easting - 200000.0;
if (mgrs.squareLetter1 > LETTER_I)
grid_easting = grid_easting - ONEHT;
if (mgrs.squareLetter1 > LETTER_L)
grid_easting = grid_easting - 300000.0;
}
easting = grid_easting + easting;
northing = grid_northing + northing;
//return UPSCoord.fromUPS(hemisphere, easting, northing);
return UPSCoord.fromUPS(hemisphere, easting, northing, null);
}
}
return null;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
After adding the parts in bold font the radians result is now converted to degrees:
System.out.println( "Output LAT/LONG:"+ String.format("%s, %s", cs.getLatitude()*180/3.14159265, cs.getLongitude()*180/3.14159265));
