?? mgrspoint.java
字號(hào):
public int getAccuracy() { return accuracy; } /** * Set the UTM parameters from a MGRS string. * * @param mgrsString an UPPERCASE coordinate string is expected. */ protected void decode(String mgrsString) throws NumberFormatException { if (mgrsString == null || mgrsString.length() == 0) { throw new NumberFormatException("MGRSPoint coverting from nothing"); } int length = mgrsString.length(); String hunK = null; StringBuffer sb = new StringBuffer(); char testChar; int i = 0; // get Zone number while (!Character.isLetter(testChar = mgrsString.charAt(i))) { if (i > 2) { throw new NumberFormatException("MGRSPoint bad conversion from: " + mgrsString); } sb.append(testChar); i++; } zone_number = Integer.parseInt(sb.toString()); if (i == 0 || i + 3 > length) { // A good MGRS string has to be 4-5 digits long, // ##AAA/#AAA at least. throw new NumberFormatException("MGRSPoint bad conversion from: " + mgrsString); } zone_letter = mgrsString.charAt(i++); // Should we check the zone letter here? Why not. if (zone_letter <= 'A' || zone_letter == 'B' || zone_letter == 'Y' || zone_letter >= 'Z' || zone_letter == 'I' || zone_letter == 'O') { throw new NumberFormatException("MGRSPoint zone letter " + (char) zone_letter + " not handled: " + mgrsString); } hunK = mgrsString.substring(i, i += 2); int set = get100kSetForZone(zone_number); float east100k = getEastingFromChar(hunK.charAt(0), set); float north100k = getNorthingFromChar(hunK.charAt(1), set); // We have a bug where the northing may be 2000000 too low. // How // do we know when to roll over? while (north100k < getMinNorthing(zone_letter)) { north100k += 2000000; } // calculate the char index for easting/northing separator int remainder = length - i; if (remainder % 2 != 0) { throw new NumberFormatException("MGRSPoint has to have an even number \nof digits after the zone letter and two 100km letters - front \nhalf for easting meters, second half for \nnorthing meters" + mgrsString); } int sep = remainder / 2; float sepEasting = 0f; float sepNorthing = 0f; if (sep > 0) { if (DEBUG) Debug.output(" calculating e/n from " + mgrs.substring(i)); float accuracyBonus = 100000f / (float) Math.pow(10, sep); if (DEBUG) Debug.output(" calculated accuracy bonus as " + accuracyBonus); String sepEastingString = mgrsString.substring(i, i + sep); if (DEBUG) Debug.output(" parsed easting as " + sepEastingString); sepEasting = Float.parseFloat(sepEastingString) * accuracyBonus; String sepNorthingString = mgrsString.substring(i + sep); if (DEBUG) Debug.output(" parsed northing as " + sepNorthingString); sepNorthing = Float.parseFloat(sepNorthingString) * accuracyBonus; } easting = sepEasting + east100k; northing = sepNorthing + north100k; if (DEBUG) { Debug.output("Decoded " + mgrsString + " as zone number: " + zone_number + ", zone letter: " + zone_letter + ", easting: " + easting + ", northing: " + northing + ", 100k: " + hunK); } } /** * Create the mgrs string based on the internal UTM settings, * using the accuracy set in the MGRSPoint. */ protected void resolve() { resolve(accuracy); } /** * Create the mgrs string based on the internal UTM settings. * * @param digitAccuracy The number of digits to use for the * northing and easting numbers. 5 digits reflect a 1 meter * accuracy, 4 - 10 meter, 3 - 100 meter, 2 - 1000 meter, 1 - * 10,000 meter. */ protected void resolve(int digitAccuracy) { if (zone_letter == 'Z') { mgrs = "Latitude limit exceeded"; } else { StringBuffer sb = new StringBuffer(zone_number + "" + (char) zone_letter + get100kID(easting, northing, zone_number)); StringBuffer seasting = new StringBuffer(Integer.toString((int) easting)); StringBuffer snorthing = new StringBuffer(Integer.toString((int) northing)); if (DEBUG) { Debug.output(" Resolving MGRS from easting: " + seasting + " derived from " + easting + ", and northing: " + snorthing + " derived from " + northing); } while (digitAccuracy + 1 > seasting.length()) { seasting.insert(0, '0'); } // We have to be careful here, the 100k values shouldn't // be // used for calculating stuff here. while (digitAccuracy + 1 > snorthing.length()) { snorthing.insert(0, '0'); } while (snorthing.length() > 6) { snorthing.deleteCharAt(0); } if (DEBUG) { Debug.output(" -- modified easting: " + seasting + " and northing: " + snorthing); } try { sb.append(seasting.substring(1, digitAccuracy + 1) + snorthing.substring(1, digitAccuracy + 1)); mgrs = sb.toString(); } catch (IndexOutOfBoundsException ioobe) { mgrs = null; } } } /** * Given a UTM zone number, figure out the MGRS 100K set it is in. */ protected int get100kSetForZone(int i) { int set = i % NUM_100K_SETS; if (set == 0) set = NUM_100K_SETS; return set; } /** * Provided so that extensions to this class can provide different * origin letters, in case of different ellipsoids. The int[] * represents all of the first letters in the bottom left corner * of each set box, as shown in an MGRS 100K box layout. */ protected int[] getOriginColumnLetters() { return originColumnLetters; } /** * Provided so that extensions to this class can provide different * origin letters, in case of different ellipsoids. The int[] * represents all of the first letters in the bottom left corner * of each set box, as shown in an MGRS 100K box layout. */ protected void setOriginColumnLetters(int[] letters) { originColumnLetters = letters; } /** * Provided so that extensions to this class can provide different * origin letters, in case of different ellipsoids. The int[] * represents all of the second letters in the bottom left corner * of each set box, as shown in an MGRS 100K box layout. */ protected int[] getOriginRowLetters() { return originRowLetters; } /** * Provided so that extensions to this class can provide different * origin letters, in case of different ellipsoids. The int[] * represents all of the second letters in the bottom left corner * of each set box, as shown in an MGRS 100K box layout. */ protected void setOriginRowLetters(int[] letters) { originRowLetters = letters; } /** * Get the two letter 100k designator for a given UTM easting, * northing and zone number value. */ protected String get100kID(float easting, float northing, int zone_number) { int set = get100kSetForZone(zone_number); int setColumn = ((int) easting / 100000); int setRow = ((int) northing / 100000) % 20; return get100kID(setColumn, setRow, set); } /** * Given the first letter from a two-letter MGRS 100k zone, and * given the MGRS table set for the zone number, figure out the * easting value that should be added to the other, secondary * easting value. */ protected float getEastingFromChar(char e, int set) { int baseCol[] = getOriginColumnLetters(); // colOrigin is the letter at the origin of the set for the // column int curCol = baseCol[set - 1]; float eastingValue = 100000f; boolean rewindMarker = false; while (curCol != e) { curCol++; if (curCol == I) curCol++; if (curCol == O) curCol++; if (curCol > Z) { if (rewindMarker) { throw new NumberFormatException("Bad character: " + e); } curCol = A; rewindMarker = true; } eastingValue += 100000f; } if (DEBUG) { Debug.output("Easting value for " + (char) e + " from set: " + set + ", col: " + curCol + " is " + eastingValue); } return eastingValue; } /** * Given the second letter from a two-letter MGRS 100k zone, and * given the MGRS table set for the zone number, figure out the * northing value that should be added to the other, secondary * northing value. You have to remember that Northings are * determined from the equator, and the vertical cycle of letters * mean a 2000000 additional northing meters. This happens approx. * every 18 degrees of latitude. This method does *NOT* count any * additional northings. You have to figure out how many 2000000 * meters need to be added for the zone letter of the MGRS * coordinate. * * @param n second letter of the MGRS 100k zone * @param set the MGRS table set number, which is dependent on the * UTM zone number. */ protected float getNorthingFromChar(char n, int set) { if (n > 'V') { throw new NumberFormatException("MGRSPoint given invalid Northing " + n); } int baseRow[] = getOriginRowLetters(); // rowOrigin is the letter at the origin of the set for the // column int curRow = baseRow[set - 1]; float northingValue = 0f; boolean rewindMarker = false; while (curRow != n) { curRow++; if (curRow == I) curRow++; if (curRow == O) curRow++; // fixing a bug making whole application hang in this loop // when 'n' is a wrong character if (curRow > V) { if (rewindMarker) { // making sure that this loop ends throw new NumberFormatException("Bad character: " + n); } curRow = A; rewindMarker = true; } northingValue += 100000f; } if (DEBUG) { Debug.output("Northing value for " + (char) n + " from set: " + set + ", row: " + curRow + " is " + northingValue); } return northingValue; } /** * Get the two-letter MGRS 100k designator given information * translated from the UTM northing, easting and zone number. * * @param setColumn the column index as it relates to the MGRS * 100k set spreadsheet, created from the UTM easting. * Values are 1-8. * @param setRow the row index as it relates to the MGRS 100k set * spreadsheet, created from the UTM northing value. Values * are from 0-19. * @param set the set block, as it relates to the MGRS 100k set * spreadsheet, created from the UTM zone. Values are from * 1-60. * @return two letter MGRS 100k code. */ protected String get100kID(int setColumn, int setRow, int set) { if (DEBUG) { System.out.println("set (" + set + ") column = " + setColumn + ", row = " + setRow); } int baseCol[] = getOriginColumnLetters(); int baseRow[] = getOriginRowLetters(); // colOrigin and rowOrigin are the letters at the origin of // the set int colOrigin = baseCol[set - 1]; int rowOrigin = baseRow[set - 1]; if (DEBUG) { System.out.println("starting at = " + (char) colOrigin + (char) rowOrigin); } // colInt and rowInt are the letters to build to return int colInt = colOrigin + setColumn - 1; int rowInt = rowOrigin + setRow; boolean rollover = false; if (colInt > Z) { colInt = colInt - Z + A - 1; rollover = true; if (DEBUG) System.out.println("rolling over col, new value: " + (char) colInt); }
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -