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

EastingNorthingPoint   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getEasting() 0 3 1
A __construct() 0 5 1
A __toString() 0 14 4
A getNorthing() 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 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 EastingNorthingPoint extends ProjectedPoint
23
{
24
    /**
25
     * Easting.
26
     * @var Length
27
     */
28
    protected $easting;
29
30
    /**
31
     * Northing.
32
     * @var Length
33
     */
34
    protected $northing;
35
36
    /**
37
     * Constructor.
38
     * @param Length $easting  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 10
    public function __construct(Length $easting, Length $northing, Projected $crs)
42
    {
43 10
        parent::__construct($crs);
44 10
        $this->easting = UnitOfMeasureFactory::convertLength($easting, $this->getAxisByName(Axis::EASTING)->getUnitOfMeasureId());
45 10
        $this->northing = UnitOfMeasureFactory::convertLength($northing, $this->getAxisByName(Axis::NORTHING)->getUnitOfMeasureId());
46 10
    }
47
48 5
    public function getEasting(): Length
49
    {
50 5
        return $this->easting;
51
    }
52
53 5
    public function getNorthing(): Length
54
    {
55 5
        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 3
    public function calculateDistance(Point $to): Length
63
    {
64 3
        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 EastingNorthingPoint $to */
69 2
        return new Metre(
70 2
            sqrt(
71 2
                ($to->getEasting()->getValue() - $this->getEasting()->getValue()) ** 2 +
72 2
                ($to->getNorthing()->getValue() - $this->getNorthing()->getValue()) ** 2
73
            )
74
        );
75
    }
76
77 2
    public function __toString(): string
78
    {
79 2
        $values = [];
80 2
        foreach ($this->getCRS()->getCoordinateSystem()->getAxes() as $axis) {
81 2
            if ($axis->getName() === Axis::EASTING) {
82 2
                $values[] = $this->easting;
83 2
            } elseif ($axis->getName() === Axis::NORTHING) {
84 2
                $values[] = $this->northing;
85
            } else {
86
                throw new UnknownAxisException(); // @codeCoverageIgnore
87
            }
88
        }
89
90 2
        return '(' . implode(', ', $values) . ')';
91
    }
92
}
93