Skip to content

Commit f131a35

Browse files
authored
Merge pull request #96 from google/dart-recovery
Fix recovery bug in dart
2 parents decd954 + dbe1131 commit f131a35

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

dart/lib/src/open_location_code.dart

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ num clipLatitude(num latitude) => latitude.clamp(-90.0, 90.0);
136136
/// columns than rows.
137137
int computeLatitudePrecision(int codeLength) {
138138
if (codeLength <= 10) {
139-
return pow(20, (codeLength ~/ -2) + 2);
139+
return pow(encodingBase, (codeLength ~/ -2) + 2);
140140
}
141-
return pow(20, -3) ~/ pow(gridRows, codeLength - 10);
141+
return pow(encodingBase, -3) ~/ pow(gridRows, codeLength - 10);
142142
}
143143

144144
/// Normalize a [longitude] into the range -180 to 180, not including 180.
@@ -322,9 +322,9 @@ String recoverNearest(
322322
// Compute the number of digits we need to recover.
323323
var paddingLength = separatorPosition - shortCode.indexOf(separator);
324324
// The resolution (height and width) of the padded area in degrees.
325-
var resolution = pow(20, 2 - (paddingLength / 2));
325+
var resolution = pow(encodingBase, 2 - (paddingLength / 2));
326326
// Distance from the center to an edge (in degrees).
327-
var areaToEdge = resolution / 2.0;
327+
var halfResolution = resolution / 2.0;
328328

329329
// Use the reference location to pad the supplied short code and decode it.
330330
var codeArea = decode(
@@ -334,23 +334,24 @@ String recoverNearest(
334334
var centerLongitude = codeArea.center.longitude;
335335

336336
// How many degrees latitude is the code from the reference? If it is more
337-
// than half the resolution, we need to move it east or west.
338-
var degreesDifference = centerLatitude - referenceLatitude;
339-
if (degreesDifference > areaToEdge) {
340-
// If the center of the short code is more than half a cell east,
341-
// then the best match will be one position west.
337+
// than half the resolution, we need to move it north or south but keep it
338+
// within -90 to 90 degrees.
339+
if (referenceLatitude + halfResolution < centerLatitude &&
340+
centerLatitude - resolution >= -latitudeMax) {
341+
// If the proposed code is more than half a cell north of the reference location,
342+
// it's too far, and the best match will be one cell south.
342343
centerLatitude -= resolution;
343-
} else if (degreesDifference < -areaToEdge) {
344-
// If the center of the short code is more than half a cell west,
345-
// then the best match will be one position east.
344+
} else if (referenceLatitude - halfResolution > centerLatitude &&
345+
centerLatitude + resolution <= latitudeMax) {
346+
// If the proposed code is more than half a cell south of the reference location,
347+
// it's too far, and the best match will be one cell north.
346348
centerLatitude += resolution;
347349
}
348350

349351
// How many degrees longitude is the code from the reference?
350-
degreesDifference = codeArea.center.longitude - referenceLongitude;
351-
if (degreesDifference > areaToEdge) {
352+
if (referenceLongitude + halfResolution < centerLongitude) {
352353
centerLongitude -= resolution;
353-
} else if (degreesDifference < -areaToEdge) {
354+
} else if (referenceLongitude - halfResolution > centerLongitude) {
354355
centerLongitude += resolution;
355356
}
356357

dart/test/all_test.dart

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,15 @@ checkShortCode(String csvLine) {
5757
num lat = double.parse(elements[1]);
5858
num lng = double.parse(elements[2]);
5959
String shortCode = elements[3];
60-
String short = olc.shorten(code, lat, lng);
61-
expect(short, equals(shortCode));
62-
String expanded = olc.recoverNearest(short, lat, lng);
63-
expect(expanded, equals(code));
60+
String testType = elements[4];
61+
if (testType == "B" || testType == "S") {
62+
String short = olc.shorten(code, lat, lng);
63+
expect(short, equals(shortCode));
64+
}
65+
if (testType == "B" || testType == "R") {
66+
String expanded = olc.recoverNearest(shortCode, lat, lng);
67+
expect(expanded, equals(code));
68+
}
6469
}
6570

6671
List<String> getCsvLines(String fileName) {

0 commit comments

Comments
 (0)