Skip to content

Commit

Permalink
Merge pull request #73 from clickbar/fix-precision-formatting
Browse files Browse the repository at this point in the history
fix(geometries): Fix missing precision in WKT coordinates
  • Loading branch information
saibotk authored Jan 2, 2025
2 parents cf14ab1 + 3c364e3 commit 06bcd20
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use of ST functions directly in the Laravel default builder methods
- Use of ST functions directly in Model::create array
- Renamed parameters of ST functions that can receive geometry or geography from `$geometry` to `$geometryOrGeography`
- Geometry & Box implements `Expression` and therefore can be used in `->select(...)` directly now
- Geometry & Box implements `Expression` and therefore can be used in `->select(...)` directly now

### Fixed

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

### Removed

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 06bcd20

Please sign in to comment.