Skip to content

Commit

Permalink
Do axis by name once up front and cache
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Aug 25, 2021
1 parent 3ec0cfa commit c87b30d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 10 deletions.
14 changes: 14 additions & 0 deletions src/CoordinateSystem/CoordinateSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,21 @@ abstract class CoordinateSystem
*/
protected array $axes;

/**
* @var Axis[]
*/
protected array $axesByName;

public function __construct(
string $srid,
array $axes
) {
$this->srid = $srid;
$this->axes = $axes;

foreach ($this->axes as $axis) {
$this->axesByName[$axis->getName()] = $axis;
}
}

public function getSRID(): string
Expand All @@ -47,4 +56,9 @@ public function getAxes(): array
{
return $this->axes;
}

public function getAxisByName(string $name): ?Axis
{
return $this->axesByName[$name] ?? null;
}
}
6 changes: 3 additions & 3 deletions src/GeocentricPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ class GeocentricPoint extends Point implements ConvertiblePoint
protected function __construct(Length $x, Length $y, Length $z, Geocentric $crs, ?DateTimeInterface $epoch = null)
{
$this->crs = $crs;
$this->x = Length::convert($x, $this->getAxisByName(Axis::GEOCENTRIC_X)->getUnitOfMeasureId());
$this->y = Length::convert($y, $this->getAxisByName(Axis::GEOCENTRIC_Y)->getUnitOfMeasureId());
$this->z = Length::convert($z, $this->getAxisByName(Axis::GEOCENTRIC_Z)->getUnitOfMeasureId());
$this->x = Length::convert($x, $this->crs->getCoordinateSystem()->getAxisByName(Axis::GEOCENTRIC_X)->getUnitOfMeasureId());
$this->y = Length::convert($y, $this->crs->getCoordinateSystem()->getAxisByName(Axis::GEOCENTRIC_Y)->getUnitOfMeasureId());
$this->z = Length::convert($z, $this->crs->getCoordinateSystem()->getAxisByName(Axis::GEOCENTRIC_Z)->getUnitOfMeasureId());

if ($epoch instanceof DateTime) {
$epoch = DateTimeImmutable::createFromMutable($epoch);
Expand Down
6 changes: 3 additions & 3 deletions src/GeographicPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ protected function __construct(Angle $latitude, Angle $longitude, ?Length $heigh
$latitude = $this->normaliseLatitude($latitude);
$longitude = $this->normaliseLongitude($longitude);

$this->latitude = Angle::convert($latitude, $this->getAxisByName(Axis::GEODETIC_LATITUDE)->getUnitOfMeasureId());
$this->longitude = Angle::convert($longitude, $this->getAxisByName(Axis::GEODETIC_LONGITUDE)->getUnitOfMeasureId());
$this->latitude = Angle::convert($latitude, $this->crs->getCoordinateSystem()->getAxisByName(Axis::GEODETIC_LATITUDE)->getUnitOfMeasureId());
$this->longitude = Angle::convert($longitude, $this->crs->getCoordinateSystem()->getAxisByName(Axis::GEODETIC_LONGITUDE)->getUnitOfMeasureId());

if ($height) {
$this->height = Length::convert($height, $this->getAxisByName(Axis::ELLIPSOIDAL_HEIGHT)->getUnitOfMeasureId());
$this->height = Length::convert($height, $this->crs->getCoordinateSystem()->getAxisByName(Axis::ELLIPSOIDAL_HEIGHT)->getUnitOfMeasureId());
} else {
$this->height = null;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Point.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,9 @@ protected static function resolveParamsByOperation(string $operationSrid, string
return $params;
}

/**
* @deprecated
*/
protected function getAxisByName(string $name): ?Axis
{
foreach ($this->getCRS()->getCoordinateSystem()->getAxes() as $axis) {
Expand Down
8 changes: 4 additions & 4 deletions src/ProjectedPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,10 @@ protected function __construct(?Length $easting, ?Length $northing, ?Length $wes
{
$this->crs = $crs;

$eastingAxis = $this->getAxisByName(Axis::EASTING);
$westingAxis = $this->getAxisByName(Axis::WESTING);
$northingAxis = $this->getAxisByName(Axis::NORTHING);
$southingAxis = $this->getAxisByName(Axis::SOUTHING);
$eastingAxis = $this->crs->getCoordinateSystem()->getAxisByName(Axis::EASTING);
$westingAxis = $this->crs->getCoordinateSystem()->getAxisByName(Axis::WESTING);
$northingAxis = $this->crs->getCoordinateSystem()->getAxisByName(Axis::NORTHING);
$southingAxis = $this->crs->getCoordinateSystem()->getAxisByName(Axis::SOUTHING);

if ($easting && $eastingAxis) {
$this->easting = Length::convert($easting, $eastingAxis->getUnitOfMeasureId());
Expand Down

0 comments on commit c87b30d

Please sign in to comment.