Skip to content

Commit 97a4735

Browse files
Merge pull request #6 from Vectorial1024/olc_decode
Fix OLC decoding
2 parents b2b68ed + 8cea5ae commit 97a4735

4 files changed

Lines changed: 18 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Note: you may refer to `README.md` for description of features.
44

55
## Dev (WIP)
66

7+
Fixed decoding of OLC producing out-of-bounds coordinates (https://github.com/Vectorial1024/open-location-code-php/pull/6).
8+
79
## 1.1.3 (2026-04-21)
810

911
GitHub security advisory (https://github.com/advisories/GHSA-qrr6-mg7r-m243)

src/CodeCalculator/CodeCalculatorFloat.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected function generateCodeArea(string $strippedCode): CodeArea
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;
84-
for ($i = OpenLocationCode::PAIR_CODE_LENGTH; $i < min(strlen($strippedCode), OpenLocationCode::MAX_DIGIT_COUNT); $i += 2) {
84+
for ($i = 0; $i < min(strlen($strippedCode), OpenLocationCode::MAX_DIGIT_COUNT); $i += 2) {
8585
$latPlaceVal = floor($latPlaceVal / OpenLocationCode::ENCODING_BASE);
8686
$lngPlaceVal = floor($lngPlaceVal / OpenLocationCode::ENCODING_BASE);
8787
$latVal += strpos(OpenLocationCode::CODE_ALPHABET, $strippedCode[$i]) * $latPlaceVal;

src/CodeCalculator/CodeCalculatorInt.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ protected function generateCodeArea(string $strippedCode): CodeArea
8383
// Define the place value for the digits. We'll divide this down as we work through the code.
8484
$latPlaceVal = self::LAT_MSP_VALUE;
8585
$lngPlaceVal = self::LNG_MSP_VALUE;
86-
for ($i = OpenLocationCode::PAIR_CODE_LENGTH; $i < min(strlen($strippedCode), OpenLocationCode::MAX_DIGIT_COUNT); $i += 2) {
86+
for ($i = 0; $i < min(strlen($strippedCode), OpenLocationCode::MAX_DIGIT_COUNT); $i += 2) {
8787
$latPlaceVal = intdiv($latPlaceVal, OpenLocationCode::ENCODING_BASE);
8888
$lngPlaceVal = intdiv($lngPlaceVal, OpenLocationCode::ENCODING_BASE);
8989
$latVal += strpos(OpenLocationCode::CODE_ALPHABET, $strippedCode[$i]) * $latPlaceVal;
90-
$lngVal = strpos(OpenLocationCode::CODE_ALPHABET, $strippedCode[$i + 1]) * $lngPlaceVal;
90+
$lngVal += strpos(OpenLocationCode::CODE_ALPHABET, $strippedCode[$i + 1]) * $lngPlaceVal;
9191
}
9292
unset($i);
9393
for ($i = OpenLocationCode::PAIR_CODE_LENGTH; $i < min(strlen($strippedCode), OpenLocationCode::MAX_DIGIT_COUNT); $i++) {

test/OpenLocationCodeTest.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public function testCorrectCodeFromCoordinates(float $latitude, float $longitude
4646
$resultCode = $floatCal->encode($latitude, $longitude, OpenLocationCode::CODE_PRECISION_NORMAL);
4747
$this->assertEquals($resultCode, $expectedCode);
4848
$resultCodeArea = $floatCal->decode($expectedCode);
49+
$this->confirmCoordinatesValidity($resultCodeArea->northLatitude, $resultCodeArea->eastLongitude);
50+
$this->confirmCoordinatesValidity($resultCodeArea->southLatitude, $resultCodeArea->westLongitude);
4951
$this->assertTrue($resultCodeArea->contains($latitude, $longitude));
5052
if (PHP_INT_SIZE >= 8) {
5153
// at least 64-bit, which means we can use "long" ints here
@@ -57,6 +59,16 @@ public function testCorrectCodeFromCoordinates(float $latitude, float $longitude
5759
}
5860
}
5961

62+
private function confirmCoordinatesValidity(float $latitude, float $longitude): void
63+
{
64+
// check latitude
65+
$this->assertGreaterThanOrEqual(-90, $latitude);
66+
$this->assertLessThanOrEqual(90, $latitude);
67+
// check longitude
68+
$this->assertGreaterThanOrEqual(-180, $longitude);
69+
$this->assertLessThanOrEqual(180, $longitude);
70+
}
71+
6072
public static function codeValidityProvider(): array
6173
{
6274
return [
@@ -76,7 +88,7 @@ public static function encodingProvider(): array
7688
"Changi Airport, Singapore" => [1.357063, 103.988563, "6PH59X4Q+RC"],
7789
"International Antarctic Centre, Christchurch" => [-43.489063, 172.547188, "4V8JGG6W+9V"],
7890
"Christo Redentor, Rio de Janeiro" => [-22.951937, -43.210437, "589R2QXQ+6R"],
79-
"New Chitose Airport, Chitose" => [42.786062,141.680937, "8RJ3QMPJ+C9"],
91+
"New Chitose Airport, Chitose" => [42.786062, 141.680937, "8RJ3QMPJ+C9"],
8092
"Berling Strait" => [65.759937, -169.149437, "92QGQV52+X6"],
8193
"Null point" => [0, 0, "6FG22222+22"],
8294
];

0 commit comments

Comments
 (0)