From 37af97c04c6c121dae0b9865bb53e672d8210aea Mon Sep 17 00:00:00 2001 From: Doug Wright Date: Thu, 15 Aug 2024 19:28:45 +0100 Subject: [PATCH] #66 Truncate, don't round --- CHANGELOG.md | 3 +++ src/Point/BritishNationalGridPoint.php | 5 ++--- src/Point/IrishGridPoint.php | 5 ++--- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cfb53eacb..2e875d331 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ - Updates to data for USA and WGS84 - Support for Irish polynomial transformation in the ETRS89 to TM75 direction (TM75 to ETRS89 was already supported) +### Fixed +- British and Irish Grid references were rounding rather than truncating causing a sometimes off-by-1 error + ## [5.9.0] - 2024-08-04 ### Changed - Updates to data for Czechia, Denmark, ETRS89, Germany, Martinique, Portugal, St Helena, UK and WGS84 diff --git a/src/Point/BritishNationalGridPoint.php b/src/Point/BritishNationalGridPoint.php index bcb3552fe..0af77b356 100644 --- a/src/Point/BritishNationalGridPoint.php +++ b/src/Point/BritishNationalGridPoint.php @@ -21,7 +21,6 @@ use function strlen; use function strpos; use function substr; -use function round; use const STR_PAD_LEFT; @@ -97,8 +96,8 @@ protected function gridReference(int $length): array $x = $this->easting->asMetres()->getValue(); $y = $this->northing->asMetres()->getValue(); - $easting = str_pad((string) round($x), $halfLength, '0', STR_PAD_LEFT); - $northing = str_pad((string) round($y), $halfLength, '0', STR_PAD_LEFT); + $easting = str_pad((string) (int) $x, $halfLength, '0', STR_PAD_LEFT); + $northing = str_pad((string) (int) $y, $halfLength, '0', STR_PAD_LEFT); $adjustedX = $x + 1000000; $adjustedY = $y + 500000; diff --git a/src/Point/IrishGridPoint.php b/src/Point/IrishGridPoint.php index d9208ceca..fc70d8d8b 100644 --- a/src/Point/IrishGridPoint.php +++ b/src/Point/IrishGridPoint.php @@ -21,7 +21,6 @@ use function strlen; use function strpos; use function substr; -use function round; use const STR_PAD_LEFT; @@ -96,8 +95,8 @@ protected function gridReference(int $length): array $x = $this->easting->asMetres()->getValue(); $y = $this->northing->asMetres()->getValue(); - $easting = str_pad((string) round($x), $halfLength, '0', STR_PAD_LEFT); - $northing = str_pad((string) round($y), $halfLength, '0', STR_PAD_LEFT); + $easting = str_pad((string) (int) $x, $halfLength, '0', STR_PAD_LEFT); + $northing = str_pad((string) (int) $y, $halfLength, '0', STR_PAD_LEFT); // second (minor) letter is 100km grid sq, origin at 0,0 of this square $minorSquaresEast = (int) $easting[0] % 5;