Skip to content

Commit

Permalink
Merge pull request #122 from clickbar/backport-precision-fix
Browse files Browse the repository at this point in the history
[1.x] Backport: Fix missing precision in WKT coordinates
  • Loading branch information
saibotk authored Jan 3, 2025
2 parents d37f0d5 + 4e2a8fb commit c35811a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- Fixed only using a precision of 6 decimal digits in WKT, now uses the maximum precision

## [1.7.0](https://github.com/clickbar/laravel-magellan/tree/1.7.0) - 2024-12-27

### Added
Expand Down
6 changes: 4 additions & 2 deletions src/Data/Geometries/GeometryHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public static function assertValidGeometryInput(int $minCount, string $class, ar

public static function stringifyFloat($float): string
{
// normalized output among locales
return trim(trim(rtrim(sprintf('%15F', $float), '0'), '.'));
// Use json_encode to use serialization precision instead of
// PHP's default format precision.
// See https://wiki.php.net/rfc/precise_float_value
return json_encode($float);
}
}
9 changes: 9 additions & 0 deletions tests/Generator/WKT/WKTGeneratorPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,12 @@

expect($pointWKT)->toBe('SRID=4326;POINT ZM(8.12345 50.12345 10 20)');
})->group('WKT Point');

// Test the float precision of the WKT generator
test('can generate 2D WKT Point with high float precision', function () {
$point = Point::make(8.1234567890123456789, 50.123456789012344);

$pointWKT = $this->generator->generate($point);

expect($pointWKT)->toBe('POINT(8.123456789012346 50.123456789012344)');
})->group('WKT Point');

0 comments on commit c35811a

Please sign in to comment.