Passed
Push — 4.0.x ( 414ad0...5c8c8d )
by Doug
05:04 queued 12s
created

GeographicValue::asGeocentricValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 14
ccs 11
cts 11
cp 1
rs 9.9332
cc 2
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\CoordinateOperation;
10
11
use function cos;
12
use PHPCoord\Datum\Datum;
13
use PHPCoord\UnitOfMeasure\Angle\Angle;
14
use PHPCoord\UnitOfMeasure\Angle\Degree;
15
use PHPCoord\UnitOfMeasure\Angle\Radian;
16
use PHPCoord\UnitOfMeasure\Length\Length;
17
use PHPCoord\UnitOfMeasure\Length\Metre;
18
use function sin;
19
use function sqrt;
20
21
/**
22
 * A geographic point w/out a CRS.
23
 * @internal
24
 */
25
class GeographicValue
26
{
27
    private $latitude;
28
29
    private $longitude;
30
31
    private $height;
32
33
    private $datum;
34
35 50
    public function __construct(Angle $latitude, Angle $longitude, ?Length $height, Datum $datum)
36
    {
37 50
        $this->latitude = $this->normaliseLatitude($latitude)->asRadians();
38 50
        $this->longitude = $this->normaliseLongitude($longitude)->asRadians();
39 50
        $this->datum = $datum;
40
41 50
        $this->height = $height ? $height->asMetres() : null;
42 50
    }
43
44 46
    public function getLatitude(): Radian
45
    {
46 46
        return $this->latitude;
47
    }
48
49 46
    public function getLongitude(): Radian
50
    {
51 46
        return $this->longitude;
52
    }
53
54 12
    public function getHeight(): ?Metre
55
    {
56 12
        return $this->height;
57
    }
58
59 2
    public function getDatum(): Datum
60
    {
61 2
        return $this->datum;
62
    }
63
64 28
    public function asGeocentricValue(): GeocentricValue
65
    {
66 28
        $a = $this->datum->getEllipsoid()->getSemiMajorAxis()->asMetres()->getValue();
67 28
        $e2 = $this->datum->getEllipsoid()->getEccentricitySquared();
68 28
        $latitude = $this->latitude->getValue();
69 28
        $longitude = $this->longitude->getValue() - $this->datum->getPrimeMeridian()->getGreenwichLongitude()->asRadians()->getValue();
70 28
        $h = isset($this->height) ? $this->height->getValue() : 0;
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on null. ( Ignorable by Annotation )

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

70
        $h = isset($this->height) ? $this->height->/** @scrutinizer ignore-call */ getValue() : 0;

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...
71
72 28
        $nu = $a / sqrt(1 - $e2 * (sin($latitude) ** 2));
73 28
        $x = ($nu + $h) * cos($latitude) * cos($longitude);
74 28
        $y = ($nu + $h) * cos($latitude) * sin($longitude);
75 28
        $z = ((1 - $e2) * $nu + $h) * sin($latitude);
76
77 28
        return new GeocentricValue(new Metre($x), new Metre($y), new Metre($z), $this->datum);
78
    }
79
80 50
    protected function normaliseLatitude(Angle $latitude): Angle
81
    {
82 50
        if ($latitude->asDegrees()->getValue() > 90) {
83
            return new Degree(90);
84
        }
85 50
        if ($latitude->asDegrees()->getValue() < -90) {
86
            return new Degree(-90);
87
        }
88
89 50
        return $latitude;
90
    }
91
92 50
    protected function normaliseLongitude(Angle $longitude): Angle
93
    {
94 50
        while ($longitude->asDegrees()->getValue() > 180) {
95 1
            $longitude = $longitude->subtract(new Degree(360));
96
        }
97 50
        while ($longitude->asDegrees()->getValue() <= -180) {
98
            $longitude = $longitude->add(new Degree(360));
99
        }
100
101 50
        return $longitude;
102
    }
103
}
104