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

WestingSouthingPoint   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Test Coverage

Coverage 95.83%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSouthing() 0 3 1
A __toString() 0 14 4
A getWesting() 0 3 1
A calculateDistance() 0 11 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\Projected;
13
use PHPCoord\CoordinateSystem\Axis;
14
use PHPCoord\Exception\UnknownAxisException;
15
use PHPCoord\UnitOfMeasure\Length\Length;
16
use PHPCoord\UnitOfMeasure\Length\Metre;
17
use PHPCoord\UnitOfMeasure\UnitOfMeasureFactory;
18
19
/**
20
 * Coordinate representing a point on a map projection.
21
 * @author Doug Wright
22
 */
23
class WestingSouthingPoint extends ProjectedPoint
24
{
25
    /**
26
     * Westing.
27
     * @var Length
28
     */
29
    protected $westing;
30
31
    /**
32
     * Southing.
33
     * @var Length
34
     */
35
    protected $southing;
36
37
    /**
38
     * Constructor.
39
     * @param Length $westing refer to CRS for preferred unit of measure, but any length unit accepted
40
     */
41 3
    public function __construct(Length $westing, Length $northing, Projected $crs)
42
    {
43 3
        parent::__construct($crs);
44 3
        $this->westing = UnitOfMeasureFactory::convertLength($westing, $this->getAxisByName(Axis::WESTING)->getUnitOfMeasureId());
45 3
        $this->southing = UnitOfMeasureFactory::convertLength($northing, $this->getAxisByName(Axis::SOUTHING)->getUnitOfMeasureId());
46 3
    }
47
48 3
    public function getWesting(): Length
49
    {
50 3
        return $this->westing;
51
    }
52
53 3
    public function getSouthing(): Length
54
    {
55 3
        return $this->southing;
56
    }
57
58
    /**
59
     * Calculate distance between two points.
60
     * Because this is a simple grid, we can use Pythagoras.
61
     */
62 1
    public function calculateDistance(Point $to): Length
63
    {
64 1
        if ($to->getCRS()->getEpsgCode() !== $this->crs->getEpsgCode()) {
65
            throw new InvalidArgumentException('Can only calculate distances between two points in the same CRS');
66
        }
67
68
        /* @var WestingSouthingPoint $to */
69 1
        return new Metre(
70 1
            sqrt(
71 1
                ($to->getWesting()->getValue() - $this->getWesting()->getValue()) ** 2 +
72 1
                ($to->getSouthing()->getValue() - $this->getSouthing()->getValue()) ** 2
73
            )
74
        );
75
    }
76
77 1
    public function __toString(): string
78
    {
79 1
        $values = [];
80 1
        foreach ($this->getCRS()->getCoordinateSystem()->getAxes() as $axis) {
81 1
            if ($axis->getName() === Axis::WESTING) {
82 1
                $values[] = $this->westing;
83 1
            } elseif ($axis->getName() === Axis::SOUTHING) {
84 1
                $values[] = $this->southing;
85
            } else {
86
                throw new UnknownAxisException(); // @codeCoverageIgnore
87
            }
88
        }
89
90 1
        return '(' . implode(', ', $values) . ')';
91
    }
92
}
93