Passed
Push — master ( 2c19f0...d4552d )
by Doug
46:35
created

CompoundPoint   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Test Coverage

Coverage 98.55%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 65
dl 0
loc 178
ccs 68
cts 69
cp 0.9855
rs 10
c 3
b 0
f 0
wmc 23

12 Methods

Rating   Name   Duplication   Size   Complexity  
A getCRS() 0 3 1
A getVerticalPoint() 0 3 1
A getCoordinateEpoch() 0 3 1
A create() 0 3 1
A getHorizontalPoint() 0 3 1
B convert() 0 29 9
A __toString() 0 3 1
A geographic2DWithHeightOffsets() 0 11 1
A __construct() 0 10 2
A calculateDistance() 0 13 3
A geographic3DTo2DPlusGravityHeightOSGM15() 0 20 1
A geographic3DTo2DPlusGravityHeightFromGrid() 0 10 1
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord;
10
11
use DateTime;
12
use DateTimeImmutable;
13
use DateTimeInterface;
14
use PHPCoord\CoordinateOperation\AutoConversion;
15
use PHPCoord\CoordinateOperation\ConvertiblePoint;
16
use PHPCoord\CoordinateOperation\GeographicGeoidHeightGrid;
17
use PHPCoord\CoordinateOperation\OSTNOSGM15Grid;
18
use PHPCoord\CoordinateReferenceSystem\Compound;
19
use PHPCoord\CoordinateReferenceSystem\CoordinateReferenceSystem;
20
use PHPCoord\CoordinateReferenceSystem\Geocentric;
21
use PHPCoord\CoordinateReferenceSystem\Geographic2D;
22
use PHPCoord\CoordinateReferenceSystem\Geographic3D;
23
use PHPCoord\CoordinateReferenceSystem\Projected;
0 ignored issues
show
Bug introduced by
The type PHPCoord\CoordinateReferenceSystem\Projected was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
24
use PHPCoord\CoordinateReferenceSystem\Vertical;
25
use PHPCoord\CoordinateSystem\Cartesian;
26
use PHPCoord\Datum\Datum;
27
use PHPCoord\Exception\InvalidCoordinateReferenceSystemException;
28
use PHPCoord\Exception\UnknownConversionException;
29
use PHPCoord\UnitOfMeasure\Angle\Angle;
30
use PHPCoord\UnitOfMeasure\Angle\Degree;
31
use PHPCoord\UnitOfMeasure\Length\Length;
32
use PHPCoord\UnitOfMeasure\Length\Metre;
33
use PHPCoord\UnitOfMeasure\Scale\Unity;
34
35
/**
36
 * Coordinate representing a point expressed in 2 different CRSs (2D horizontal + 1D Vertical).
37
 */
38
class CompoundPoint extends Point implements ConvertiblePoint
39
{
40
    use AutoConversion {
41
        convert as protected autoConvert;
42
    }
43
44
    /**
45
     * Horizontal point.
46
     */
47
    protected GeographicPoint|ProjectedPoint $horizontalPoint;
48
49
    /**
50
     * Vertical point.
51
     */
52
    protected VerticalPoint $verticalPoint;
53
54
    /**
55
     * Coordinate reference system.
56
     */
57
    protected Compound $crs;
58
59
    /**
60
     * Coordinate epoch (date for which the specified coordinates represented this point).
61
     */
62
    protected ?DateTimeImmutable $epoch;
63
64 92
    protected function __construct(Compound $crs, GeographicPoint|ProjectedPoint $horizontalPoint, VerticalPoint $verticalPoint, ?DateTimeInterface $epoch = null)
65
    {
66 92
        $this->horizontalPoint = $horizontalPoint;
67 92
        $this->verticalPoint = $verticalPoint;
68 92
        $this->crs = $crs;
69
70 92
        if ($epoch instanceof DateTime) {
71 9
            $epoch = DateTimeImmutable::createFromMutable($epoch);
72
        }
73 92
        $this->epoch = $epoch;
74
    }
75
76 92
    public static function create(Compound $crs, GeographicPoint|ProjectedPoint $horizontalPoint, VerticalPoint $verticalPoint, ?DateTimeInterface $epoch = null): self
77
    {
78 92
        return new static($crs, $horizontalPoint, $verticalPoint, $epoch);
79
    }
80
81 86
    public function getHorizontalPoint(): GeographicPoint|ProjectedPoint
82
    {
83 86
        return $this->horizontalPoint;
84
    }
85
86 67
    public function getVerticalPoint(): VerticalPoint
87
    {
88 67
        return $this->verticalPoint;
89
    }
90
91 73
    public function getCRS(): Compound
92
    {
93 73
        return $this->crs;
94
    }
95
96 34
    public function getCoordinateEpoch(): ?DateTimeImmutable
97
    {
98 34
        return $this->epoch;
99
    }
100
101
    /**
102
     * Calculate distance between two points.
103
     */
104 18
    public function calculateDistance(Point $to): Length
105
    {
106
        try {
107 18
            if ($to instanceof ConvertiblePoint) {
108 18
                $to = $to->convert($this->horizontalPoint->getCRS());
109
            }
110
        } finally {
111 18
            if ($to->getCRS()->getSRID() !== $this->horizontalPoint->getCRS()->getSRID()) {
112 9
                throw new InvalidCoordinateReferenceSystemException('Can only calculate distances between two points in the same CRS');
113
            }
114
115
            /* @var CompoundPoint $to */
116 9
            return $this->horizontalPoint->calculateDistance($to);
117
        }
118
    }
119
120 46
    public function convert(Compound|Geocentric|Geographic2D|Geographic3D|Projected|Vertical $to, bool $ignoreBoundaryRestrictions = false): Point
121
    {
122
        try {
123 46
            return $this->autoConvert($to, $ignoreBoundaryRestrictions);
124 27
        } catch (UnknownConversionException $e) {
125
            // if 2D target, try again with just the horizontal component
126 27
            if ($to instanceof Geographic2D || $to instanceof Projected) {
127 18
                return $this->getHorizontalPoint()->convert($to, $ignoreBoundaryRestrictions);
128
            }
129
130
            // try separate horizontal + vertical conversions and stitch results together
131 9
            if ($to instanceof Compound) {
132 9
                $newHorizontalPoint = $this->getHorizontalPoint()->convert($to->getHorizontal());
133
134 9
                if ($this->getCRS()->getVertical()->getSRID() !== $to->getVertical()->getSRID()) {
135 9
                    $path = $this->findOperationPath($this->getCRS()->getVertical(), $to->getVertical(), $ignoreBoundaryRestrictions);
136
137 9
                    if ($path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $path of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
138 9
                        $newVerticalPoint = $this->getVerticalPoint();
139 9
                        foreach ($path as $step) {
140 9
                            $target = CoordinateReferenceSystem::fromSRID($step['in_reverse'] ? $step['source_crs'] : $step['target_crs']);
141 9
                            $newVerticalPoint = $newVerticalPoint->performOperation($step['operation'], $target, $step['in_reverse'], ['horizontalPoint' => $newHorizontalPoint]);
142
                        }
143
144 9
                        return static::create($to, $newHorizontalPoint, $newVerticalPoint, $this->epoch);
145
                    }
146
                }
147
            }
148
            throw $e;
149
        }
150
    }
151
152 27
    public function __toString(): string
153
    {
154 27
        return "({$this->horizontalPoint}, {$this->verticalPoint})";
155
    }
156
157
    /**
158
     * Geographic2D with Height Offsets.
159
     * This transformation allows calculation of coordinates in the target system by adding the parameter value to the
160
     * coordinate values of the point in the source system.
161
     */
162 18
    public function geographic2DWithHeightOffsets(
163
        Geographic3D $to,
164
        Angle $latitudeOffset,
165
        Angle $longitudeOffset,
166
        Length $geoidUndulation
167
    ): GeographicPoint {
168 18
        $toLatitude = $this->getHorizontalPoint()->getLatitude()->add($latitudeOffset);
0 ignored issues
show
Bug introduced by
The method getLatitude() does not exist on PHPCoord\ProjectedPoint. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

168
        $toLatitude = $this->getHorizontalPoint()->/** @scrutinizer ignore-call */ getLatitude()->add($latitudeOffset);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
169 18
        $toLongitude = $this->getHorizontalPoint()->getLongitude()->add($longitudeOffset);
0 ignored issues
show
Bug introduced by
The method getLongitude() does not exist on PHPCoord\ProjectedPoint. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

169
        $toLongitude = $this->getHorizontalPoint()->/** @scrutinizer ignore-call */ getLongitude()->add($longitudeOffset);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
170 18
        $toHeight = $this->getVerticalPoint()->getHeight()->add($geoidUndulation);
171
172 18
        return GeographicPoint::create($to, $toLatitude, $toLongitude, $toHeight, $this->epoch);
173
    }
174
175
    /**
176
     * Geog3D to Geog2D+GravityRelatedHeight (OSGM-GB).
177
     * Uses ETRS89 / National Grid as an intermediate coordinate system for bi-linear interpolation of gridded grid
178
     * coordinate differences.
179
     */
180 1
    public function geographic3DTo2DPlusGravityHeightOSGM15(
181
        Geographic3D $to,
182
        OSTNOSGM15Grid $geoidHeightCorrectionModelFile
183
    ): GeographicPoint {
184 1
        $osgb36NationalGrid = Projected::fromSRID(Projected::EPSG_OSGB36_BRITISH_NATIONAL_GRID);
185 1
        $etrs89NationalGrid = new Projected(
186 1
            'ETRS89 / National Grid',
187 1
            Cartesian::fromSRID(Cartesian::EPSG_2D_AXES_EASTING_NORTHING_E_N_ORIENTATIONS_EAST_NORTH_UOM_M),
188 1
            Datum::fromSRID(Datum::EPSG_EUROPEAN_TERRESTRIAL_REFERENCE_SYSTEM_1989_ENSEMBLE),
189 1
            $osgb36NationalGrid->getBoundingArea()
190 1
        );
191
192 1
        $projected = $this->horizontalPoint->transverseMercator($etrs89NationalGrid, new Degree(49), new Degree(-2), new Unity(0.9996012717), new Metre(400000), new Metre(-100000));
193
194 1
        return GeographicPoint::create(
195 1
            $to,
196 1
            $this->horizontalPoint->getLatitude(),
197 1
            $this->horizontalPoint->getLongitude(),
198 1
            $this->verticalPoint->getHeight()->add($geoidHeightCorrectionModelFile->getHeightAdjustment($projected)),
0 ignored issues
show
Bug introduced by
It seems like $projected can also be of type PHPCoord\GeographicPoint; however, parameter $point of PHPCoord\CoordinateOpera...::getHeightAdjustment() does only seem to accept PHPCoord\ProjectedPoint, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

198
            $this->verticalPoint->getHeight()->add($geoidHeightCorrectionModelFile->getHeightAdjustment(/** @scrutinizer ignore-type */ $projected)),
Loading history...
199 1
            $this->getCoordinateEpoch()
200 1
        );
201
    }
202
203
    /**
204
     * Geog3D to Geog2D+GravityRelatedHeight.
205
     */
206 6
    public function geographic3DTo2DPlusGravityHeightFromGrid(
207
        Geographic3D $to,
208
        GeographicGeoidHeightGrid $geoidHeightCorrectionModelFile
209
    ): GeographicPoint {
210 6
        return GeographicPoint::create(
211 6
            $to,
212 6
            $this->horizontalPoint->getLatitude(),
213 6
            $this->horizontalPoint->getLongitude(),
214 6
            $this->verticalPoint->getHeight()->add($geoidHeightCorrectionModelFile->getHeightAdjustment($this->horizontalPoint)),
0 ignored issues
show
Bug introduced by
It seems like $this->horizontalPoint can also be of type PHPCoord\ProjectedPoint; however, parameter $location of PHPCoord\CoordinateOpera...::getHeightAdjustment() does only seem to accept PHPCoord\GeographicPoint, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

214
            $this->verticalPoint->getHeight()->add($geoidHeightCorrectionModelFile->getHeightAdjustment(/** @scrutinizer ignore-type */ $this->horizontalPoint)),
Loading history...
215 6
            $this->getCoordinateEpoch()
216 6
        );
217
    }
218
}
219