Skip to content

Commit 3f91f44

Browse files
authored
Modify integer conversion for JS Closure library (#698)
* Modify integer conversion for JS Closure library * fix markdown warnings
1 parent 9fee617 commit 3f91f44

File tree

2 files changed

+26
-38
lines changed

2 files changed

+26
-38
lines changed

js/closure/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Code should be formatted according to the
1818

1919
You can run checks on the code using `eslint`:
2020

21-
```
21+
```shell
2222
cd js
2323
npm install eslint
2424
eslint closure/*js
@@ -37,7 +37,7 @@ The test cases have been copied from the [`test_data`](https://github.com/google
3737

3838
Run the tests from the top-level github directory with:
3939

40-
```
40+
```shell
4141
$ bazel test js/closure:openlocationcode_test
4242
INFO: Found 1 test target...
4343
Target //js/closure:openlocationcode_test up-to-date:

js/closure/openlocationcode.js

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -363,28 +363,32 @@ function encode(latitude, longitude, optLength) {
363363
'IllegalArgumentException: Invalid Plus Code length');
364364
}
365365
optLength = Math.min(optLength, MAX_CODE_LEN);
366-
// Ensure that latitude and longitude are valid.
367-
latitude = clipLatitude(latitude);
368-
longitude = normalizeLongitude(longitude);
369-
// Latitude 90 needs to be adjusted to be just less, so the returned code
370-
// can also be decoded.
371-
if (latitude == 90) {
372-
latitude = latitude - computeLatitudePrecision(optLength);
373-
}
374-
var code = '';
375366

376-
// Compute the code.
377-
// This approach converts each value to an integer after multiplying it by
378-
// the final precision. This allows us to use only integer operations, so
379-
// avoiding any accumulation of floating point representation errors.
367+
// This approach converts each value to an integer after multiplying it by the final precision.
368+
// This allows us to use only integer operations, so avoiding any accumulation of floating
369+
// point representation errors.
370+
371+
// Convert latitude into a positive integer clipped into the range 0-(just under 180*2.5e7).
372+
// Latitude 90 needs to be adjusted to be just less, so the returned code can also be decoded.
373+
var latVal = Math.round(latitude * FINAL_LAT_PRECISION);
374+
latVal += LATITUDE_MAX * FINAL_LAT_PRECISION;
375+
if (latVal < 0) {
376+
latVal = 0;
377+
} else if (latVal >= 2 * LATITUDE_MAX * FINAL_LAT_PRECISION) {
378+
latVal = 2 * LATITUDE_MAX * FINAL_LAT_PRECISION - 1;
379+
}
380+
// Convert longitude into a positive integer and normalise it into the range 0-360*8.192e6.
381+
var lngVal = Math.round(longitude * FINAL_LNG_PRECISION);
382+
lngVal += LONGITUDE_MAX * FINAL_LNG_PRECISION;
383+
if (lngVal < 0) {
384+
lngVal =
385+
(lngVal % (2 * LONGITUDE_MAX * FINAL_LNG_PRECISION)) +
386+
2 * LONGITUDE_MAX * FINAL_LNG_PRECISION;
387+
} else if (lngVal >= 2 * LONGITUDE_MAX * FINAL_LNG_PRECISION) {
388+
lngVal = lngVal % (2 * LONGITUDE_MAX * FINAL_LNG_PRECISION);
389+
}
380390

381-
// Multiply values by their precision and convert to positive.
382-
// Force to integers so the division operations will have integer results.
383-
// Note: JavaScript requires rounding before truncating to ensure precision!
384-
var latVal =
385-
Math.floor(Math.round((latitude + LATITUDE_MAX) * FINAL_LAT_PRECISION * 1e6) / 1e6);
386-
var lngVal =
387-
Math.floor(Math.round((longitude + LONGITUDE_MAX) * FINAL_LNG_PRECISION * 1e6) / 1e6);
391+
var code = '';
388392

389393
// Compute the grid part of the code if necessary.
390394
if (optLength > PAIR_CODE_LENGTH) {
@@ -639,22 +643,6 @@ function clipLatitude(latitude) {
639643
return Math.min(90, Math.max(-90, latitude));
640644
}
641645

642-
/**
643-
Compute the latitude precision value for a given code length. Lengths <=
644-
10 have the same precision for latitude and longitude, but lengths > 10
645-
have different precisions due to the grid method having fewer columns than
646-
rows.
647-
@param {number} codeLength A number representing the length (digits) in a
648-
code.
649-
@return {number} The height of the code area in degrees latitude.
650-
*/
651-
function computeLatitudePrecision(codeLength) {
652-
if (codeLength <= 10) {
653-
return Math.pow(20, Math.floor(codeLength / -2 + 2));
654-
}
655-
return Math.pow(20, -3) / Math.pow(GRID_ROWS, codeLength - 10);
656-
}
657-
658646
/**
659647
Normalize a longitude into the range -180 to 180, not including 180.
660648

0 commit comments

Comments
 (0)