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

WestingSouthingPoint   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
eloc 23
c 1
b 0
f 0
dl 0
loc 68
rs 10

5 Methods

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