Passed
Push — 4.x ( 739b15...17e0c5 )
by Doug
04:36 queued 02:58
created

GeocentricPoint   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 21
c 1
b 0
f 0
dl 0
loc 84
ccs 23
cts 24
cp 0.9583
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCRS() 0 3 1
A getX() 0 3 1
A __toString() 0 3 1
A getY() 0 3 1
A getZ() 0 3 1
A calculateDistance() 0 12 2
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord;
10
11
use InvalidArgumentException;
12
use PHPCoord\CoordinateReferenceSystem\CoordinateReferenceSystem;
13
use PHPCoord\CoordinateReferenceSystem\Geocentric;
14
use PHPCoord\CoordinateSystem\Axis;
15
use PHPCoord\UnitOfMeasure\Length\Length;
16
use PHPCoord\UnitOfMeasure\Length\Metre;
17
use PHPCoord\UnitOfMeasure\UnitOfMeasureFactory;
18
19
/**
20
 * Coordinate representing a point in ECEF geocentric form.
21
 * @author Doug Wright
22
 */
23
class GeocentricPoint extends Point
24
{
25
    /**
26
     * X co-ordinate.
27
     * @var Length
28
     */
29
    protected $x;
30
31
    /**
32
     * Y co-ordinate.
33
     * @var Length
34
     */
35
    protected $y;
36
37
    /**
38
     * Z co-ordinate.
39
     * @var Length
40
     */
41
    protected $z;
42
43
    /**
44
     * Coordinate reference system.
45
     * @var CoordinateReferenceSystem
46
     */
47
    protected $crs;
48
49
    /**
50
     * Constructor.
51
     * @param Length $x refer to CRS for preferred unit of measure, but any length unit accepted
52
     * @param Length $y refer to CRS for preferred unit of measure, but any length unit accepted
53
     * @param Length $z refer to CRS for preferred unit of measure, but any length unit accepted
54
     */
55 3
    public function __construct(Length $x, Length $y, Length $z, Geocentric $crs)
56
    {
57 3
        $this->crs = $crs;
58 3
        $this->x = UnitOfMeasureFactory::convertLength($x, $this->getAxisByName(Axis::GEOCENTRIC_X)->getUnitOfMeasureId());
59 3
        $this->y = UnitOfMeasureFactory::convertLength($y, $this->getAxisByName(Axis::GEOCENTRIC_Y)->getUnitOfMeasureId());
60 3
        $this->z = UnitOfMeasureFactory::convertLength($z, $this->getAxisByName(Axis::GEOCENTRIC_Z)->getUnitOfMeasureId());
61 3
    }
62
63 3
    public function getX(): Length
64
    {
65 3
        return $this->x;
66
    }
67
68 3
    public function getY(): Length
69
    {
70 3
        return $this->y;
71
    }
72
73 3
    public function getZ(): Length
74
    {
75 3
        return $this->z;
76
    }
77
78 3
    public function getCRS(): CoordinateReferenceSystem
79
    {
80 3
        return $this->crs;
81
    }
82
83
    /**
84
     * Calculate surface distance between two points.
85
     * Note: this implementation is currently not accurate over long distances, it is the straight line distance, not
86
     * the surface distance.
87
     */
88 1
    public function calculateDistance(Point $to): Length
89
    {
90 1
        if ($to->getCRS()->getEpsgCode() !== $this->crs->getEpsgCode()) {
91
            throw new InvalidArgumentException('Can only calculate distances between two points in the same CRS');
92
        }
93
94
        /* @var GeocentricPoint $to */
95 1
        return new Metre(
96 1
            sqrt(
97 1
                ($to->getX()->getValue() - $this->x->getValue()) ** 2 +
98 1
                ($to->getY()->getValue() - $this->y->getValue()) ** 2 +
99 1
                ($to->getZ()->getValue() - $this->z->getValue()) ** 2
100
            )
101
        );
102
    }
103
104 1
    public function __toString(): string
105
    {
106 1
        return "({$this->x}, {$this->y}, {$this->z})";
107
    }
108
}
109