-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Brick\Geo\Projector; | ||
|
||
use Brick\Geo\CoordinateSystem; | ||
use Brick\Geo\Point; | ||
|
||
/** | ||
* Rounds coordinates to a given precision. | ||
* This projector is typically used to simplify the WKT representation of geometries. | ||
*/ | ||
final class RoundCoordinatesProjector implements Projector | ||
{ | ||
public function __construct( | ||
private readonly int $precision, | ||
) { | ||
} | ||
|
||
public function project(Point $point): Point | ||
{ | ||
$coords = array_map( | ||
fn (float $coord): float => round($coord, $this->precision), | ||
$point->toArray(), | ||
); | ||
|
||
return new Point($point->coordinateSystem(), ...$coords); | ||
} | ||
|
||
public function getTargetCoordinateSystem(CoordinateSystem $sourceCoordinateSystem): CoordinateSystem | ||
{ | ||
return $sourceCoordinateSystem; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Brick\Geo\Tests\Projector; | ||
|
||
use Brick\Geo\CoordinateSystem; | ||
use Brick\Geo\Point; | ||
use Brick\Geo\Projector\RoundCoordinatesProjector; | ||
use Brick\Geo\Tests\AbstractTestCase; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
|
||
/** | ||
* Unit tests for class RoundCoordinatesProjector. | ||
*/ | ||
class RoundCoordinatesProjectorTest extends AbstractTestCase | ||
{ | ||
#[DataProvider('providerProject')] | ||
public function testProject(float $x, float $y, int $srid, int $precision, float $expectedX, float $expectedY): void | ||
{ | ||
$projector = new RoundCoordinatesProjector($precision); | ||
|
||
$point = Point::xy($x, $y, $srid); | ||
$projected = $projector->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)], | ||
]; | ||
} | ||
} |