Passed
Push — 4.x ( 17e0c5...e9b635 )
by Doug
07:44
created

WestingNorthingPoint::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 3
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 PHPCoord\CoordinateReferenceSystem\Projected;
0 ignored issues
show
Bug introduced by
The type PHPCoord\CoordinateReferenceSystem\Projected was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use PHPCoord\CoordinateSystem\Axis;
13
use PHPCoord\Exception\InvalidCoordinateReferenceSystemException;
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
 */
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 4
    public function __construct(Length $westing, Length $northing, Projected $crs)
42
    {
43 4
        parent::__construct($crs);
44 4
        $this->westing = UnitOfMeasureFactory::convertLength($westing, $this->getAxisByName(Axis::WESTING)->getUnitOfMeasureId());
45 4
        $this->northing = UnitOfMeasureFactory::convertLength($northing, $this->getAxisByName(Axis::NORTHING)->getUnitOfMeasureId());
46 4
    }
47
48 3
    public function getWesting(): Length
49
    {
50 3
        return $this->westing;
51
    }
52
53 3
    public function getNorthing(): Length
54
    {
55 3
        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 2
    public function calculateDistance(Point $to): Length
63
    {
64 2
        if ($to->getCRS()->getEpsgCode() !== $this->crs->getEpsgCode()) {
65 1
            throw new InvalidCoordinateReferenceSystemException('Can only calculate distances between two points in the same CRS');
66
        }
67
68
        /* @var WestingNorthingPoint $to */
69 1
        return new Metre(
70 1
            sqrt(
71 1
                ($to->getWesting()->getValue() - $this->getWesting()->getValue()) ** 2 +
72 1
                ($to->getNorthing()->getValue() - $this->getNorthing()->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::NORTHING) {
84 1
                $values[] = $this->northing;
85
            } else {
86
                throw new UnknownAxisException(); // @codeCoverageIgnore
87
            }
88
        }
89
90 1
        return '(' . implode(', ', $values) . ')';
91
    }
92
}
93