diff --git a/CHANGELOG.md b/CHANGELOG.md index be0b384..09d806a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## [0.11.1](https://github.com/brick/geo/releases/tag/0.11.1) - 2024-08-19 + +✨ **New features** + +- New projector: `RoundCoordinatesProjector` + ## [0.11.0](https://github.com/brick/geo/releases/tag/0.11.0) - 2024-06-07 💥 **Breaking changes** diff --git a/README.md b/README.md index ee394ef..12911b4 100644 --- a/README.md +++ b/README.md @@ -448,6 +448,24 @@ The library supports reading and writing `Feature` and `FeatureCollection` objec GeoJSON aims to support WGS84 only, and as such all Geometries are imported using [SRID 4326](https://epsg.io/4326). +## Reducing coordinate precision + +Before exporting geometries in a text format, you may need reduce the precision of the coordinates to keep the output small, while retaining a sufficient precision. +You can use the `RoundCoordinatesProjector` for this: + +```php +use Brick\Geo\Point; +use Brick\Geo\Projector\RoundCoordinatesProjector; + +$roundProjector = new RoundCoordinatesProjector(2); + +$point = Point::xy(1.2345678, 2.3456789); +echo $point->asText(); // POINT (1.2345678 2.3456789) + +$roundedPoint = $roundProjector->project($point); +echo $roundedPoint->asText(); // POINT (1.23 2.35) +``` + ## Doctrine mappings You can use `brick/geo` types in your Doctrine entities using the [brick/geo-doctrine](https://github.com/brick/geo-doctrine) package. diff --git a/src/Projector/RoundCoordinatesProjector.php b/src/Projector/RoundCoordinatesProjector.php new file mode 100644 index 0000000..2351d55 --- /dev/null +++ b/src/Projector/RoundCoordinatesProjector.php @@ -0,0 +1,35 @@ + round($coord, $this->precision), + $point->toArray(), + ); + + return new Point($point->coordinateSystem(), ...$coords); + } + + public function getTargetCoordinateSystem(CoordinateSystem $sourceCoordinateSystem): CoordinateSystem + { + return $sourceCoordinateSystem; + } +} diff --git a/tests/Projector/RoundCoordinatesProjectorTest.php b/tests/Projector/RoundCoordinatesProjectorTest.php new file mode 100644 index 0000000..ef3b7b9 --- /dev/null +++ b/tests/Projector/RoundCoordinatesProjectorTest.php @@ -0,0 +1,55 @@ +project($point); + + $this->assertPointXYEquals($expectedX, $expectedY, $srid, $projected); + } + + public static function providerProject(): array + { + return [ + [1.234567, 2.345678, 1234, 0, 1, 2], + [1.234567, 2.345678, 2345, 1, 1.2, 2.3], + [1.234567, 2.345678, 3456, 2, 1.23, 2.35], + [1.234567, 2.345678, 4567, 3, 1.235, 2.346], + ]; + } + + #[DataProvider('providerGetTargetCoordinateSystem')] + public function testGetTargetCoordinateSystem(CoordinateSystem $sourceCoordinateSystem): void + { + $projector = new RoundCoordinatesProjector(2); + $targetCoordinateSystem = $projector->getTargetCoordinateSystem($sourceCoordinateSystem); + + $this->assertSame($sourceCoordinateSystem, $targetCoordinateSystem); + } + + public static function providerGetTargetCoordinateSystem(): array + { + return [ + [CoordinateSystem::xy(4326)], + [CoordinateSystem::xyz(2154)], + ]; + } +}