Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Note: you may refer to `README.md` for description of features.

## Dev (WIP)

Fixed decoding of OLC producing out-of-bounds coordinates (https://github.com/Vectorial1024/open-location-code-php/pull/6).

## 1.1.3 (2026-04-21)

GitHub security advisory (https://github.com/advisories/GHSA-qrr6-mg7r-m243)
Expand Down
2 changes: 1 addition & 1 deletion src/CodeCalculator/CodeCalculatorFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ protected function generateCodeArea(string $strippedCode): CodeArea
// Define the place value for the digits. We'll divide this down as we work through the code.
$latPlaceVal = self::LAT_MSP_VALUE;
$lngPlaceVal = self::LNG_MSP_VALUE;
for ($i = OpenLocationCode::PAIR_CODE_LENGTH; $i < min(strlen($strippedCode), OpenLocationCode::MAX_DIGIT_COUNT); $i += 2) {
for ($i = 0; $i < min(strlen($strippedCode), OpenLocationCode::MAX_DIGIT_COUNT); $i += 2) {
$latPlaceVal = floor($latPlaceVal / OpenLocationCode::ENCODING_BASE);
$lngPlaceVal = floor($lngPlaceVal / OpenLocationCode::ENCODING_BASE);
$latVal += strpos(OpenLocationCode::CODE_ALPHABET, $strippedCode[$i]) * $latPlaceVal;
Expand Down
4 changes: 2 additions & 2 deletions src/CodeCalculator/CodeCalculatorInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ protected function generateCodeArea(string $strippedCode): CodeArea
// Define the place value for the digits. We'll divide this down as we work through the code.
$latPlaceVal = self::LAT_MSP_VALUE;
$lngPlaceVal = self::LNG_MSP_VALUE;
for ($i = OpenLocationCode::PAIR_CODE_LENGTH; $i < min(strlen($strippedCode), OpenLocationCode::MAX_DIGIT_COUNT); $i += 2) {
for ($i = 0; $i < min(strlen($strippedCode), OpenLocationCode::MAX_DIGIT_COUNT); $i += 2) {
$latPlaceVal = intdiv($latPlaceVal, OpenLocationCode::ENCODING_BASE);
$lngPlaceVal = intdiv($lngPlaceVal, OpenLocationCode::ENCODING_BASE);
$latVal += strpos(OpenLocationCode::CODE_ALPHABET, $strippedCode[$i]) * $latPlaceVal;
$lngVal = strpos(OpenLocationCode::CODE_ALPHABET, $strippedCode[$i + 1]) * $lngPlaceVal;
$lngVal += strpos(OpenLocationCode::CODE_ALPHABET, $strippedCode[$i + 1]) * $lngPlaceVal;
}
unset($i);
for ($i = OpenLocationCode::PAIR_CODE_LENGTH; $i < min(strlen($strippedCode), OpenLocationCode::MAX_DIGIT_COUNT); $i++) {
Expand Down
14 changes: 13 additions & 1 deletion test/OpenLocationCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function testCorrectCodeFromCoordinates(float $latitude, float $longitude
$resultCode = $floatCal->encode($latitude, $longitude, OpenLocationCode::CODE_PRECISION_NORMAL);
$this->assertEquals($resultCode, $expectedCode);
$resultCodeArea = $floatCal->decode($expectedCode);
$this->confirmCoordinatesValidity($resultCodeArea->northLatitude, $resultCodeArea->eastLongitude);
$this->confirmCoordinatesValidity($resultCodeArea->southLatitude, $resultCodeArea->westLongitude);
$this->assertTrue($resultCodeArea->contains($latitude, $longitude));
if (PHP_INT_SIZE >= 8) {
// at least 64-bit, which means we can use "long" ints here
Expand All @@ -57,6 +59,16 @@ public function testCorrectCodeFromCoordinates(float $latitude, float $longitude
}
}

private function confirmCoordinatesValidity(float $latitude, float $longitude): void
{
// check latitude
$this->assertGreaterThanOrEqual(-90, $latitude);
$this->assertLessThanOrEqual(90, $latitude);
// check longitude
$this->assertGreaterThanOrEqual(-180, $longitude);
$this->assertLessThanOrEqual(180, $longitude);
}

public static function codeValidityProvider(): array
{
return [
Expand All @@ -76,7 +88,7 @@ public static function encodingProvider(): array
"Changi Airport, Singapore" => [1.357063, 103.988563, "6PH59X4Q+RC"],
"International Antarctic Centre, Christchurch" => [-43.489063, 172.547188, "4V8JGG6W+9V"],
"Christo Redentor, Rio de Janeiro" => [-22.951937, -43.210437, "589R2QXQ+6R"],
"New Chitose Airport, Chitose" => [42.786062,141.680937, "8RJ3QMPJ+C9"],
"New Chitose Airport, Chitose" => [42.786062, 141.680937, "8RJ3QMPJ+C9"],
"Berling Strait" => [65.759937, -169.149437, "92QGQV52+X6"],
"Null point" => [0, 0, "6FG22222+22"],
];
Expand Down
Loading