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

WestingNorthingPoint::getNorthing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 WestingNorthingPoint extends ProjectedPoint
24
{
25
    /**
26
     * Westing.
27
     * @var Length
28
     */
29
    protected $westing;
30
31
    /**
32
     * Northing.
33
     * @var Length
34
     */
35
    protected $northing;
36
37
    /**
38
     * Constructor.
39
     * @param Length $westing  refer to CRS for preferred unit of measure, but any length unit accepted
40
     * @param Length $northing refer to CRS for preferred unit of measure, but any length unit accepted
41
     */
42 3
    public function __construct(Length $westing, Length $northing, Projected $crs)
43
    {
44 3
        parent::__construct($crs);
45 3
        $this->westing = UnitOfMeasureFactory::convertLength($westing, $this->getAxisByName(Axis::WESTING)->getUnitOfMeasureId());
46 3
        $this->northing = UnitOfMeasureFactory::convertLength($northing, $this->getAxisByName(Axis::NORTHING)->getUnitOfMeasureId());
47 3
    }
48
49 3
    public function getWesting(): Length
50
    {
51 3
        return $this->westing;
52
    }
53
54 3
    public function getNorthing(): Length
55
    {
56 3
        return $this->northing;
57
    }
58
59
    /**
60
     * Calculate distance between two points.
61
     * Because this is a simple grid, we can use Pythagoras.
62
     */
63 1
    public function calculateDistance(Point $to): Length
64
    {
65 1
        if ($to->getCRS()->getEpsgCode() !== $this->crs->getEpsgCode()) {
66
            throw new InvalidArgumentException('Can only calculate distances between two points in the same CRS');
67
        }
68
69
        /* @var WestingNorthingPoint $to */
70 1
        return new Metre(
71 1
            sqrt(
72 1
                ($to->getWesting()->getValue() - $this->getWesting()->getValue()) ** 2 +
73 1
                ($to->getNorthing()->getValue() - $this->getNorthing()->getValue()) ** 2
74
            )
75
        );
76
    }
77
78 1
    public function __toString(): string
79
    {
80 1
        $values = [];
81 1
        foreach ($this->getCRS()->getCoordinateSystem()->getAxes() as $axis) {
82 1
            if ($axis->getName() === Axis::WESTING) {
83 1
                $values[] = $this->westing;
84 1
            } elseif ($axis->getName() === Axis::NORTHING) {
85 1
                $values[] = $this->northing;
86
            } else {
87
                throw new UnknownAxisException(); // @codeCoverageIgnore
88
            }
89
        }
90
91 1
        return '(' . implode(', ', $values) . ')';
92
    }
93
}
94