Skip to content

Commit

Permalink
Merge pull request #126 from clickbar/fix-box-precision
Browse files Browse the repository at this point in the history
fix(box): use maximum precision in toString
  • Loading branch information
ahawlitschek authored Jan 3, 2025
2 parents b50a220 + e59ae5c commit 07c5507
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fixed only using a precision of 6 decimal digits in WKT, now uses the maximum precision
- Fixed only using default PHP string precision in Box `toString` methods, now uses the maximum precision

### Removed

Expand Down
6 changes: 5 additions & 1 deletion src/Data/Boxes/Box2D.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Clickbar\Magellan\Data\Boxes;

use Clickbar\Magellan\Data\Geometries\GeometryHelper;
use Illuminate\Database\Grammar;

class Box2D extends Box
Expand Down Expand Up @@ -37,7 +38,10 @@ public function getYMax(): float

public function toString(): string
{
return "BOX({$this->xMin} {$this->yMin},{$this->xMax} {$this->yMax})";
$min = GeometryHelper::stringifyFloat($this->xMin).' '.GeometryHelper::stringifyFloat($this->yMin);
$max = GeometryHelper::stringifyFloat($this->xMax).' '.GeometryHelper::stringifyFloat($this->yMax);

return "BOX({$min},{$max})";
}

public function getValue(Grammar $grammar): string
Expand Down
6 changes: 5 additions & 1 deletion src/Data/Boxes/Box3D.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Clickbar\Magellan\Data\Boxes;

use Clickbar\Magellan\Data\Geometries\GeometryHelper;
use Illuminate\Database\Grammar;

class Box3D extends Box
Expand Down Expand Up @@ -47,7 +48,10 @@ public function getZMax(): float

public function toString(): string
{
return "BOX3D({$this->xMin} {$this->yMin} {$this->zMin},{$this->xMax} {$this->yMax} {$this->zMax})";
$min = GeometryHelper::stringifyFloat($this->xMin).' '.GeometryHelper::stringifyFloat($this->yMin).' '.GeometryHelper::stringifyFloat($this->zMin);
$max = GeometryHelper::stringifyFloat($this->xMax).' '.GeometryHelper::stringifyFloat($this->yMax).' '.GeometryHelper::stringifyFloat($this->zMax);

return "BOX3D({$min},{$max})";
}

public function getValue(Grammar $grammar): string
Expand Down
16 changes: 16 additions & 0 deletions tests/Data/BoxesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Clickbar\Magellan\Data\Boxes\Box2D;
use Clickbar\Magellan\Data\Boxes\Box3D;

test('Box2D retains precision in toString', function () {
$box = Box2D::make(1.123456789012345, 2.123456789, 3.123456789, 4.123456789);

expect($box->toString())->toBe('BOX(1.123456789012345 2.123456789,3.123456789 4.123456789)');
});

test('Box3D retains precision in toString', function () {
$box = Box3D::make(1.123456789012345, 2.123456789, 2.123456789, 3.123456789, 4.123456789, 6.123456789);

expect($box->toString())->toBe('BOX3D(1.123456789012345 2.123456789 2.123456789,3.123456789 4.123456789 6.123456789)');
});

0 comments on commit 07c5507

Please sign in to comment.