Passed
Push — 4.x ( c37faa...bd875a )
by Doug
12:53
created

WestingNorthingPoint::__toString()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 14
rs 9.9666
cc 4
nc 4
nop 0
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord;
10
11
use PHPCoord\CoordinateReferenceSystem\Projected;
12
use PHPCoord\CoordinateSystem\Axis;
13
use PHPCoord\Exception\UnknownAxisException;
14
use PHPCoord\UnitOfMeasure\Length\Length;
15
use PHPCoord\UnitOfMeasure\Length\Metre;
16
use PHPCoord\UnitOfMeasure\UnitOfMeasureFactory;
17
18
/**
19
 * Coordinate representing a point on a map projection.
20
 * @author Doug Wright
21
 */
22
class WestingNorthingPoint extends ProjectedPoint
23
{
24
    /**
25
     * Westing.
26
     * @var Length
27
     */
28
    protected $westing;
29
30
    /**
31
     * Northing.
32
     * @var Length
33
     */
34
    protected $northing;
35
36
    /**
37
     * Constructor.
38
     * @param Length $westing  refer to CRS for preferred unit of measure, but any length unit accepted
39
     * @param Length $northing refer to CRS for preferred unit of measure, but any length unit accepted
40
     */
41
    public function __construct(Length $westing, Length $northing, Projected $crs)
42
    {
43
        parent::__construct($crs);
44
        $this->westing = UnitOfMeasureFactory::convertLength($westing, $this->getAxisByName(Axis::WESTING)->getUnitOfMeasureId());
45
        $this->northing = UnitOfMeasureFactory::convertLength($northing, $this->getAxisByName(Axis::NORTHING)->getUnitOfMeasureId());
46
    }
47
48
    public function getWesting(): Length
49
    {
50
        return $this->westing;
51
    }
52
53
    public function getNorthing(): Length
54
    {
55
        return $this->northing;
56
    }
57
58
    /**
59
     * Calculate distance between two points.
60
     * Because this is a simple grid, we can use Pythagoras.
61
     */
62
    public function calculateSurfaceDistance(Point $to): Length
63
    {
64
        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 WestingNorthingPoint $to */
69
        return new Metre(
70
            sqrt(
71
                ($to->getWesting()->getValue() - $this->getWesting()->getValue()) ** 2 +
72
                ($to->getNorthing()->getValue() - $this->getNorthing()->getValue()) ** 2
73
            )
74
        );
75
    }
76
77
    public function __toString(): string
78
    {
79
        $values = [];
80
        foreach ($this->getCRS()->getCoordinateSystem()->getAxes() as $axis) {
81
            if ($axis->getName() === Axis::WESTING) {
82
                $values[] = $this->westing;
83
            } elseif ($axis->getName() === Axis::NORTHING) {
84
                $values[] = $this->northing;
85
            } else {
86
                throw new UnknownAxisException(); // @codeCoverageIgnore
87
            }
88
        }
89
90
        return '(' . implode(', ', $values) . ')';
91
    }
92
}
93