Skip to content

Commit 0c48551

Browse files
committed
Clean up the constants
1 parent d8e1d21 commit 0c48551

File tree

3 files changed

+17
-23
lines changed

3 files changed

+17
-23
lines changed

src/CodeCalculator/AbstractCodeCalculator.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
*/
1111
abstract class AbstractCodeCalculator
1212
{
13+
// Value to multiple latitude degrees to convert it to an integer with the maximum encoding
14+
// precision. I.e. ENCODING_BASE**3 * GRID_ROWS**GRID_CODE_LENGTH
15+
protected const int LAT_INTEGER_MULTIPLIER = 8000 * 3125;
16+
17+
// Value to multiple longitude degrees to convert it to an integer with the maximum encoding
18+
// precision. I.e. ENCODING_BASE**3 * GRID_COLUMNS**GRID_CODE_LENGTH
19+
protected const int LNG_INTEGER_MULTIPLIER = 8000 * 1024;
20+
1321
/**
1422
* Assuming the given latitude and longitude are valid, encode these coordinates into an Open Location Code.
1523
* @param float $latitude The latitude in decimal degrees.

src/CodeCalculator/CodeCalculatorFloat.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ protected function generateRevOlcCode(float $latitude, float $longitude, int $co
3737
// Multiply values by their precision and convert to positive.
3838
// Rounding avoids/minimizes errors due to floating-point precision.
3939
// Since the numbers are positive, floor() is equivalent to intval().
40-
$latVal = floor(round(($latitude + OpenLocationCode::LATITUDE_MAX) * OpenLocationCode::LAT_INTEGER_MULTIPLIER * 1e6) / 1e6);
41-
$lngVal = floor(round(($longitude + OpenLocationCode::LONGITUDE_MAX) * OpenLocationCode::LNG_INTEGER_MULTIPLIER * 1e6) / 1e6);
40+
$latVal = floor(round(($latitude + OpenLocationCode::LATITUDE_MAX) * self::LAT_INTEGER_MULTIPLIER * 1e6) / 1e6);
41+
$lngVal = floor(round(($longitude + OpenLocationCode::LONGITUDE_MAX) * self::LNG_INTEGER_MULTIPLIER * 1e6) / 1e6);
4242

4343
// Compute the grid part of the code if necessary.
4444
if ($codeLength > OpenLocationCode::PAIR_CODE_LENGTH) {
@@ -76,8 +76,8 @@ public function decode(string $strippedCode): CodeArea
7676
// Initialize the values.
7777
// We will assume these values are floats to ensure 32bit PHP compatibility.
7878
// See relevant comments in encode() above.
79-
$latVal = -OpenLocationCode::LATITUDE_MAX * OpenLocationCode::LAT_INTEGER_MULTIPLIER;
80-
$lngVal = -OpenLocationCode::LONGITUDE_MAX * OpenLocationCode::LNG_INTEGER_MULTIPLIER;
79+
$latVal = -OpenLocationCode::LATITUDE_MAX * self::LAT_INTEGER_MULTIPLIER;
80+
$lngVal = -OpenLocationCode::LONGITUDE_MAX * self::LNG_INTEGER_MULTIPLIER;
8181
// Define the place value for the digits. We'll divide this down as we work through the code.
8282
$latPlaceVal = self::LAT_MSP_VALUE;
8383
$lngPlaceVal = self::LNG_MSP_VALUE;
@@ -99,10 +99,10 @@ public function decode(string $strippedCode): CodeArea
9999
unset($digit);
100100
}
101101
unset($i);
102-
$latitudeLo = $latVal / OpenLocationCode::LAT_INTEGER_MULTIPLIER;
103-
$longitudeLo = $lngVal / OpenLocationCode::LNG_INTEGER_MULTIPLIER;
104-
$latitudeHi = ($latVal + $latPlaceVal) / OpenLocationCode::LAT_INTEGER_MULTIPLIER;
105-
$longitudeHi = ($lngVal + $lngPlaceVal) / OpenLocationCode::LNG_INTEGER_MULTIPLIER;
102+
$latitudeLo = $latVal / self::LAT_INTEGER_MULTIPLIER;
103+
$longitudeLo = $lngVal / self::LNG_INTEGER_MULTIPLIER;
104+
$latitudeHi = ($latVal + $latPlaceVal) / self::LAT_INTEGER_MULTIPLIER;
105+
$longitudeHi = ($lngVal + $lngPlaceVal) / self::LNG_INTEGER_MULTIPLIER;
106106
return new CodeArea(
107107
$latitudeLo,
108108
$longitudeLo,

src/OpenLocationCode.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,21 +54,7 @@ final class OpenLocationCode implements Stringable
5454
// Number of rows in the grid refinement method.
5555
public const int GRID_ROWS = 5;
5656

57-
// Value to multiple latitude degrees to convert it to an integer with the maximum encoding
58-
// precision. I.e. ENCODING_BASE**3 * GRID_ROWS**GRID_CODE_LENGTH
59-
public const int LAT_INTEGER_MULTIPLIER = 8000 * 3125;
60-
61-
// Value to multiple longitude degrees to convert it to an integer with the maximum encoding
62-
// precision. I.e. ENCODING_BASE**3 * GRID_COLUMNS**GRID_CODE_LENGTH
63-
public const int LNG_INTEGER_MULTIPLIER = 8000 * 1024;
64-
65-
// Value of the most significant latitude digit after it has been converted to an integer.
66-
// Note: to ensure 32bit PHP compatibility, this is now a precisely-represented float.
67-
private const float LAT_MSP_VALUE = self::LAT_INTEGER_MULTIPLIER * self::ENCODING_BASE * self::ENCODING_BASE;
68-
69-
// Value of the most significant longitude digit after it has been converted to an integer.
70-
// Note: to ensure 32bit PHP compatibility, this is now a precisely-represented float.
71-
private const float LNG_MSP_VALUE = self::LNG_INTEGER_MULTIPLIER * self::ENCODING_BASE * self::ENCODING_BASE;
57+
// Note: constants that are only for encoding/decoding are moved to AbstractCodeCalculator.
7258

7359
// The 360 degree circle information to normalize longitudes.
7460
private const int CIRCLE_DEG = 2 * self::LONGITUDE_MAX;

0 commit comments

Comments
 (0)