forked from NASAWorldWind/WorldWindJava
-
Notifications
You must be signed in to change notification settings - Fork 15
MGRS conversion fixes #68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,7 +37,7 @@ class MGRSCoordConverter | |
private static final int MGRS_ZONE_ERROR = 0x0100; | ||
private static final int MGRS_HEMISPHERE_ERROR = 0x0200; | ||
private static final int MGRS_LAT_WARNING = 0x0400; | ||
private static final int MGRS_NOZONE_WARNING = 0x0800; | ||
// private static final int MGRS_NOZONE_WARNING = 0x0800; | ||
private static final int MGRS_UTM_ERROR = 0x1000; | ||
private static final int MGRS_UPS_ERROR = 0x2000; | ||
|
||
|
@@ -62,7 +62,7 @@ class MGRSCoordConverter | |
// Ellipsoid parameters, default to WGS 84 | ||
private double MGRS_a = 6378137.0; // Semi-major axis of ellipsoid in meters | ||
private double MGRS_f = 1 / 298.257223563; // Flattening of ellipsoid | ||
private double MGRS_recpf = 298.257223563; | ||
// private double MGRS_recpf = 298.257223563; | ||
private String MGRS_Ellipsoid_Code = "WE"; | ||
|
||
private Globe globe; | ||
|
@@ -255,9 +255,11 @@ public long convertMGRSToGeodetic(String MGRSString) | |
{ | ||
latitude = 0; | ||
longitude = 0; | ||
long error_code = checkZone(MGRSString); | ||
if (error_code == MGRS_NO_ERROR) | ||
{ | ||
MGRSComponents mgrs = breakMGRSString(MGRSString); | ||
if (mgrs == null) return last_error; | ||
|
||
long error_code = MGRS_NO_ERROR; | ||
if (mgrs.zone != 0) { | ||
UTMCoord UTM = convertMGRSToUTM(MGRSString); | ||
if (UTM != null) | ||
{ | ||
|
@@ -267,9 +269,8 @@ public long convertMGRSToGeodetic(String MGRSString) | |
else | ||
error_code = MGRS_UTM_ERROR; | ||
} | ||
else if (error_code == MGRS_NOZONE_WARNING) | ||
else | ||
{ | ||
// TODO: polar conversion | ||
UPSCoord UPS = convertMGRSToUPS(MGRSString); | ||
if (UPS != null) | ||
{ | ||
|
@@ -314,17 +315,15 @@ private MGRSComponents breakMGRSString(String MGRSString) | |
long northing = 0; | ||
int precision = 0; | ||
|
||
while (i < MGRSString.length() && MGRSString.charAt(i) == ' ') | ||
{ | ||
i++; /* skip any leading blanks */ | ||
} | ||
MGRSString = MGRSString.toUpperCase().replaceAll("\\s", ""); | ||
j = i; | ||
while (i < MGRSString.length() && Character.isDigit(MGRSString.charAt(i))) | ||
while (i < MGRSString.length() && Character.isDigit(' ')) | ||
{ | ||
i++; | ||
} | ||
num_digits = i - j; | ||
if (num_digits <= 2) | ||
{ | ||
if (num_digits > 0) | ||
{ | ||
/* get zone */ | ||
|
@@ -333,7 +332,11 @@ private MGRSComponents breakMGRSString(String MGRSString) | |
error_code |= MGRS_STRING_ERROR; | ||
} | ||
else | ||
error_code |= MGRS_STRING_ERROR; | ||
{ | ||
zone = 0; | ||
} | ||
} | ||
|
||
j = i; | ||
|
||
while (i < MGRSString.length() && Character.isLetter(MGRSString.charAt(i))) | ||
|
@@ -394,40 +397,6 @@ private MGRSComponents breakMGRSString(String MGRSString) | |
return null; | ||
} | ||
|
||
/** | ||
* The function Check_Zone receives an MGRS coordinate string. If a zone is given, MGRS_NO_ERROR is returned. | ||
* Otherwise, MGRS_NOZONE_WARNING. is returned. | ||
* | ||
* @param MGRSString the MGRS coordinate string. | ||
* | ||
* @return the error code. | ||
*/ | ||
private long checkZone(String MGRSString) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @gbburkhardt, is it ok for us to drop this |
||
{ | ||
int i = 0; | ||
int j = 0; | ||
int num_digits = 0; | ||
long error_code = MGRS_NO_ERROR; | ||
|
||
/* skip any leading blanks */ | ||
while (i < MGRSString.length() && MGRSString.charAt(i) == ' ') | ||
{ | ||
i++; | ||
} | ||
j = i; | ||
while (i < MGRSString.length() && Character.isDigit(MGRSString.charAt(i))) | ||
{ | ||
i++; | ||
} | ||
num_digits = i - j; | ||
if (num_digits > 2) | ||
error_code |= MGRS_STRING_ERROR; | ||
else if (num_digits <= 0) | ||
error_code |= MGRS_NOZONE_WARNING; | ||
|
||
return error_code; | ||
} | ||
|
||
/** | ||
* The function Get_Latitude_Band_Min_Northing receives a latitude band letter and uses the Latitude_Band_Table to | ||
* determine the minimum northing for that latitude band letter. Updates min_northing. | ||
|
@@ -503,13 +472,9 @@ else if ((letter >= LETTER_P) && (letter <= LETTER_X)) | |
*/ | ||
private UTMCoord convertMGRSToUTM(String MGRSString) | ||
{ | ||
double scaled_min_northing; | ||
double grid_easting; /* Easting for 100,000 meter grid square */ | ||
double grid_northing; /* Northing for 100,000 meter grid square */ | ||
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; | ||
|
||
|
@@ -622,10 +587,6 @@ private UTMCoord convertMGRSToUTM(String MGRSString) | |
*/ | ||
public long convertGeodeticToMGRS(double latitude, double longitude, int precision) | ||
{ | ||
String Hemisphere = AVKey.NORTH; | ||
double Easting = 0.0; | ||
double Northing = 0.0; | ||
|
||
MGRSString = ""; | ||
|
||
long error_code = MGRS_NO_ERROR; | ||
|
@@ -861,7 +822,7 @@ private long convertUTMToMGRS(long Zone, double Latitude, double Easting, double | |
* The function Get_Grid_Values sets the letter range used for the 2nd letter in the MGRS coordinate string, based | ||
* on the set number of the utm zone. It also sets the false northing using a value of A for the second letter of | ||
* the grid square, based on the grid pattern and set number of the utm zone. | ||
* <p> | ||
* <p></p> | ||
* Key values that are set in this function include: ltr2_low_value, ltr2_high_value, and false_northing. | ||
* | ||
* @param zone Zone number | ||
|
@@ -1077,84 +1038,90 @@ private UPSCoord convertMGRSToUPS(String MGRS) | |
|
||
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) | ||
} | ||
else | ||
{ | ||
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 | ||
if (mgrs.zone > 0) | ||
{ | ||
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; | ||
error_code |= MGRS_STRING_ERROR; | ||
} | ||
|
||
// 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; | ||
easting = mgrs.easting; | ||
northing = mgrs.northing; | ||
|
||
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.latitudeBand >= LETTER_Y) | ||
{ | ||
if (mgrs.squareLetter1 > LETTER_L) | ||
grid_easting = grid_easting - 300000.0; | ||
|
||
if (mgrs.squareLetter1 > LETTER_U) | ||
grid_easting = grid_easting - 200000.0; | ||
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 | ||
{ | ||
if (mgrs.squareLetter1 > LETTER_C) | ||
grid_easting = grid_easting - 200000.0; | ||
|
||
if (mgrs.squareLetter1 > LETTER_I) | ||
grid_easting = grid_easting - ONEHT; | ||
hemisphere = AVKey.SOUTH; | ||
|
||
if (mgrs.squareLetter1 > LETTER_L) | ||
grid_easting = grid_easting - 300000.0; | ||
ltr2_low_value = upsConstants[mgrs.latitudeBand][1]; //.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; | ||
} | ||
|
||
easting = grid_easting + easting; | ||
northing = grid_northing + northing; | ||
return UPSCoord.fromUPS(hemisphere, easting, northing, globe); | ||
// 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, globe); | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gbburkhardt, I'm not sure this change is correct. The
Character.isDigit(' ')
call will always return false.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can drop this while loop from the look of it.