Passed
Push — master ( a784d9...14494d )
by Doug
60:49
created

CompoundPoint   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 173
Duplicated Lines 0 %

Test Coverage

Coverage 92%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 60
dl 0
loc 173
ccs 46
cts 50
cp 0.92
rs 10
c 2
b 0
f 0
wmc 23

11 Methods

Rating   Name   Duplication   Size   Complexity  
B convert() 0 31 10
A getCRS() 0 3 1
A getVerticalPoint() 0 3 1
A __toString() 0 3 1
A getCoordinateEpoch() 0 3 1
A geographic3DTo2DPlusGravityHeightOSGM15() 0 21 1
A calculateDistance() 0 13 3
A create() 0 3 1
A getHorizontalPoint() 0 3 1
A geographic2DWithHeightOffsets() 0 11 1
A __construct() 0 10 2
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\OSTNOSGM15Grid;
17
use PHPCoord\CoordinateReferenceSystem\Compound;
18
use PHPCoord\CoordinateReferenceSystem\CoordinateReferenceSystem;
19
use PHPCoord\CoordinateReferenceSystem\Geographic2D;
20
use PHPCoord\CoordinateReferenceSystem\Geographic3D;
21
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...
22
use PHPCoord\CoordinateReferenceSystem\Vertical;
23
use PHPCoord\CoordinateSystem\Cartesian;
24
use PHPCoord\Datum\Datum;
25
use PHPCoord\Exception\InvalidCoordinateReferenceSystemException;
26
use PHPCoord\Exception\UnknownConversionException;
27
use PHPCoord\UnitOfMeasure\Angle\Angle;
28
use PHPCoord\UnitOfMeasure\Angle\Degree;
29
use PHPCoord\UnitOfMeasure\Length\Length;
30
use PHPCoord\UnitOfMeasure\Length\Metre;
31
use PHPCoord\UnitOfMeasure\Scale\Unity;
32
33
/**
34
 * Coordinate representing a point expressed in 2 different CRSs (2D horizontal + 1D Vertical).
35
 */
36
class CompoundPoint extends Point implements ConvertiblePoint
37
{
38
    use AutoConversion {
39
        convert as protected autoConvert;
40
    }
41
42
    /**
43
     * Horizontal point.
44
     * @var GeographicPoint|ProjectedPoint
45
     */
46
    protected Point $horizontalPoint;
47
48
    /**
49
     * Vertical point.
50
     */
51
    protected VerticalPoint $verticalPoint;
52
53
    /**
54
     * Coordinate reference system.
55
     */
56
    protected Compound $crs;
57
58
    /**
59
     * Coordinate epoch (date for which the specified coordinates represented this point).
60
     */
61 72
    protected ?DateTimeImmutable $epoch;
62
63 72
    /**
64 72
     * Constructor.
65 72
     * @param GeographicPoint|ProjectedPoint $horizontalPoint
66
     */
67 72
    protected function __construct(Point $horizontalPoint, VerticalPoint $verticalPoint, Compound $crs, ?DateTimeInterface $epoch = null)
68 9
    {
69
        $this->horizontalPoint = $horizontalPoint;
0 ignored issues
show
Documentation Bug introduced by
$horizontalPoint is of type PHPCoord\Point, but the property $horizontalPoint was declared to be of type PHPCoord\GeographicPoint|PHPCoord\ProjectedPoint. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
70 72
        $this->verticalPoint = $verticalPoint;
71 72
        $this->crs = $crs;
72
73
        if ($epoch instanceof DateTime) {
74
            $epoch = DateTimeImmutable::createFromMutable($epoch);
75
        }
76 72
        $this->epoch = $epoch;
77
    }
78 72
79
    /**
80
     * @param GeographicPoint|ProjectedPoint $horizontalPoint
81 63
     */
82
    public static function create(Point $horizontalPoint, VerticalPoint $verticalPoint, Compound $crs, ?DateTimeInterface $epoch = null)
83 63
    {
84
        return new static($horizontalPoint, $verticalPoint, $crs, $epoch);
85
    }
86 54
87
    public function getHorizontalPoint(): Point
88 54
    {
89
        return $this->horizontalPoint;
90
    }
91 72
92
    public function getVerticalPoint(): VerticalPoint
93 72
    {
94
        return $this->verticalPoint;
95
    }
96 54
97
    public function getCRS(): Compound
98 54
    {
99
        return $this->crs;
100
    }
101
102
    public function getCoordinateEpoch(): ?DateTimeImmutable
103
    {
104 18
        return $this->epoch;
105
    }
106
107 18
    /**
108 18
     * Calculate distance between two points.
109
     */
110
    public function calculateDistance(Point $to): Length
111 18
    {
112 9
        try {
113
            if ($to instanceof ConvertiblePoint) {
114
                $to = $to->convert($this->crs);
115
            }
116 9
        } finally {
117
            if ($to->getCRS()->getSRID() !== $this->crs->getSRID()) {
118
                throw new InvalidCoordinateReferenceSystemException('Can only calculate distances between two points in the same CRS');
119
            }
120 45
121
            /* @var CompoundPoint $to */
122
            return $this->horizontalPoint->calculateDistance($to->horizontalPoint);
123 45
        }
124 18
    }
125 9
126
    public function convert(CoordinateReferenceSystem $to, bool $ignoreBoundaryRestrictions = false): Point
127 9
    {
128
        try {
129
            return $this->autoConvert($to, $ignoreBoundaryRestrictions);
130
        } catch (UnknownConversionException $e) {
131
            if ($this->getHorizontalPoint() instanceof ConvertiblePoint) {
132 9
                // if 2D target, try again with just the horizontal component
133 9
                if (($to instanceof Geographic2D || $to instanceof Projected)) {
134
                    return $this->getHorizontalPoint()->convert($to, $ignoreBoundaryRestrictions);
135 9
                }
136 9
137
                // try separate horizontal + vertical conversions and stitch results together
138 9
                if ($to instanceof Compound) {
139 9
                    $newHorizontalPoint = $this->getHorizontalPoint()->convert($to->getHorizontal());
140 9
141 9
                    if ($this->getCRS()->getVertical()->getSRID() !== $to->getVertical()->getSRID()) {
142 9
                        $path = $this->findOperationPath($this->getCRS()->getVertical(), $to->getVertical(), $ignoreBoundaryRestrictions);
143
144
                        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...
145 9
                            $newVerticalPoint = $this->getVerticalPoint();
146
                            foreach ($path as $step) {
147
                                $target = CoordinateReferenceSystem::fromSRID($step['in_reverse'] ? $step['source_crs'] : $step['target_crs']);
148
                                $newVerticalPoint = $newVerticalPoint->performOperation($step['operation'], $target, $step['in_reverse'], ['horizontalPoint' => $newHorizontalPoint]);
149
                            }
150
151
                            return static::create($newHorizontalPoint, $newVerticalPoint, $to, $this->epoch);
152
                        }
153
                    }
154 27
                }
155
            }
156 27
            throw $e;
157
        }
158
    }
159
160
    public function __toString(): string
161
    {
162
        return "({$this->horizontalPoint}, {$this->verticalPoint})";
163
    }
164 18
165
    /**
166
     * Geographic2D with Height Offsets.
167
     * This transformation allows calculation of coordinates in the target system by adding the parameter value to the
168
     * coordinate values of the point in the source system.
169
     */
170 18
    public function geographic2DWithHeightOffsets(
171 18
        Geographic3D $to,
172 18
        Angle $latitudeOffset,
173
        Angle $longitudeOffset,
174 18
        Length $geoidUndulation
175
    ): GeographicPoint {
176
        $toLatitude = $this->getHorizontalPoint()->getLatitude()->add($latitudeOffset);
0 ignored issues
show
Bug introduced by
The method getLatitude() does not exist on PHPCoord\Point. It seems like you code against a sub-type of PHPCoord\Point such as PHPCoord\GeographicPoint. ( Ignorable by Annotation )

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

176
        $toLatitude = $this->getHorizontalPoint()->/** @scrutinizer ignore-call */ getLatitude()->add($latitudeOffset);
Loading history...
177
        $toLongitude = $this->getHorizontalPoint()->getLongitude()->add($longitudeOffset);
0 ignored issues
show
Bug introduced by
The method getLongitude() does not exist on PHPCoord\Point. It seems like you code against a sub-type of PHPCoord\Point such as PHPCoord\GeographicPoint. ( Ignorable by Annotation )

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

177
        $toLongitude = $this->getHorizontalPoint()->/** @scrutinizer ignore-call */ getLongitude()->add($longitudeOffset);
Loading history...
178
        $toHeight = $this->getVerticalPoint()->getHeight()->add($geoidUndulation);
179
180
        return GeographicPoint::create($toLatitude, $toLongitude, $toHeight, $to, $this->epoch);
181
    }
182
183
    /**
184
     * Geog3D to Geog2D+GravityRelatedHeight (OSGM-GB).
185
     * Uses ETRS89 / National Grid as an intermediate coordinate system for bi-linear interpolation of gridded grid
186
     * coordinate differences.
187
     */
188
    public function geographic3DTo2DPlusGravityHeightOSGM15(
189
        Geographic3D $to,
190
        OSTNOSGM15Grid $geoidHeightCorrectionModelFile,
191
        string $EPSGCodeForInterpolationCRS
0 ignored issues
show
Unused Code introduced by
The parameter $EPSGCodeForInterpolationCRS is not used and could be removed. ( Ignorable by Annotation )

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

191
        /** @scrutinizer ignore-unused */ string $EPSGCodeForInterpolationCRS

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
192
    ): GeographicPoint {
193
        $osgb36NationalGrid = Projected::fromSRID(Projected::EPSG_OSGB36_BRITISH_NATIONAL_GRID);
194
        $etrs89NationalGrid = new Projected(
195
            'ETRS89 / National Grid',
196
            Cartesian::fromSRID(Cartesian::EPSG_2D_AXES_EASTING_NORTHING_E_N_ORIENTATIONS_EAST_NORTH_UOM_M),
197
            Datum::fromSRID(Datum::EPSG_EUROPEAN_TERRESTRIAL_REFERENCE_SYSTEM_1989_ENSEMBLE),
198
            $osgb36NationalGrid->getBoundingArea()
199
        );
200
201
        $projected = $this->horizontalPoint->transverseMercator($etrs89NationalGrid, new Degree(49), new Degree(-2), new Unity(0.9996012717), new Metre(400000), new Metre(-100000));
0 ignored issues
show
Bug introduced by
The method transverseMercator() does not exist on PHPCoord\Point. It seems like you code against a sub-type of PHPCoord\Point such as PHPCoord\GeographicPoint or 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

201
        /** @scrutinizer ignore-call */ 
202
        $projected = $this->horizontalPoint->transverseMercator($etrs89NationalGrid, new Degree(49), new Degree(-2), new Unity(0.9996012717), new Metre(400000), new Metre(-100000));
Loading history...
202
203
        return GeographicPoint::create(
204
            $this->horizontalPoint->getLatitude(),
205
            $this->horizontalPoint->getLongitude(),
206
            $this->verticalPoint->getHeight()->add($geoidHeightCorrectionModelFile->getVerticalAdjustment($projected)),
207
            $to,
208
            $this->getCoordinateEpoch()
209
        );
210
    }
211
}
212