Skip to content

Commit 9817675

Browse files
committed
formatting
1 parent c53faef commit 9817675

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

java/src/main/java/com/google/openlocationcode/OpenLocationCode.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -200,29 +200,37 @@ public OpenLocationCode(double latitude, double longitude, int codeLength) {
200200
this.code = encodeIntegers(integers[0], integers[1], codeLength);
201201
}
202202

203-
private static String encodeIntegers(long latVal, long lngVal, int codeLength) {
203+
/**
204+
* Encode a location specified with integer values and return the code.
205+
*
206+
* @param lat The latitude as a positive integer.
207+
* @param lng The longitude as a positive integer.
208+
* @param codeLength The requested number of digits.
209+
* @return The OLC for the location.
210+
*/
211+
private static String encodeIntegers(long lat, long lng, int codeLength) {
204212
// Store the code - we build it in reverse and reorder it afterwards.
205213
StringBuilder revCodeBuilder = new StringBuilder();
206214
// Compute the grid part of the code if necessary.
207215
if (codeLength > PAIR_CODE_LENGTH) {
208216
for (int i = 0; i < GRID_CODE_LENGTH; i++) {
209-
long latDigit = latVal % GRID_ROWS;
210-
long lngDigit = lngVal % GRID_COLUMNS;
217+
long latDigit = lat % GRID_ROWS;
218+
long lngDigit = lng % GRID_COLUMNS;
211219
int ndx = (int) (latDigit * GRID_COLUMNS + lngDigit);
212220
revCodeBuilder.append(CODE_ALPHABET.charAt(ndx));
213-
latVal /= GRID_ROWS;
214-
lngVal /= GRID_COLUMNS;
221+
lat /= GRID_ROWS;
222+
lng /= GRID_COLUMNS;
215223
}
216224
} else {
217-
latVal = (long) (latVal / Math.pow(GRID_ROWS, GRID_CODE_LENGTH));
218-
lngVal = (long) (lngVal / Math.pow(GRID_COLUMNS, GRID_CODE_LENGTH));
225+
lat = (long) (lat / Math.pow(GRID_ROWS, GRID_CODE_LENGTH));
226+
lng = (long) (lng / Math.pow(GRID_COLUMNS, GRID_CODE_LENGTH));
219227
}
220228
// Compute the pair section of the code.
221229
for (int i = 0; i < PAIR_CODE_LENGTH / 2; i++) {
222-
revCodeBuilder.append(CODE_ALPHABET.charAt((int) (lngVal % ENCODING_BASE)));
223-
revCodeBuilder.append(CODE_ALPHABET.charAt((int) (latVal % ENCODING_BASE)));
224-
latVal /= ENCODING_BASE;
225-
lngVal /= ENCODING_BASE;
230+
revCodeBuilder.append(CODE_ALPHABET.charAt((int) (lng % ENCODING_BASE)));
231+
revCodeBuilder.append(CODE_ALPHABET.charAt((int) (lat % ENCODING_BASE)));
232+
lat /= ENCODING_BASE;
233+
lng /= ENCODING_BASE;
226234
// If we are at the separator position, add the separator.
227235
if (i == 0) {
228236
revCodeBuilder.append(SEPARATOR);
@@ -647,6 +655,14 @@ public static boolean isShortCode(String code) {
647655

648656
// Private static methods.
649657

658+
/**
659+
* Convert latitude and longitude in degrees into the integer values needed for reliable encoding.
660+
* (To avoid floating point precision errors.)
661+
*
662+
* @param latitude The latitude in decimal degrees.
663+
* @param longitude The longitude in decimal degrees.
664+
* @return A list of [latitude, longitude] in clipped, normalised integer values.
665+
*/
650666
private static long[] degreesToIntegers(double latitude, double longitude) {
651667
long lat = (long) roundAwayFromZero(latitude * LAT_INTEGER_MULTIPLIER);
652668
long lng = (long) roundAwayFromZero(longitude * LNG_INTEGER_MULTIPLIER);
@@ -666,7 +682,7 @@ private static long[] degreesToIntegers(double latitude, double longitude) {
666682
} else if (lng >= 2 * LONGITUDE_MAX * LNG_INTEGER_MULTIPLIER) {
667683
lng = lng % (2 * LONGITUDE_MAX * LNG_INTEGER_MULTIPLIER);
668684
}
669-
return new long[]{lat, lng};
685+
return new long[] {lat, lng};
670686
}
671687

672688
/**

0 commit comments

Comments
 (0)