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

WestingSouthingPoint   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 68
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 68
ccs 24
cts 24
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getSouthing() 0 3 1
A __toString() 0 14 4
A getWesting() 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 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 4
    public function __construct(Length $westing, Length $northing, Projected $crs)
41
    {
42 4
        parent::__construct($crs);
43 4
        $this->westing = UnitOfMeasureFactory::convertLength($westing, $this->getAxisByName(Axis::WESTING)->getUnitOfMeasureId());
44 4
        $this->southing = UnitOfMeasureFactory::convertLength($northing, $this->getAxisByName(Axis::SOUTHING)->getUnitOfMeasureId());
45 4
    }
46
47 3
    public function getWesting(): Length
48
    {
49 3
        return $this->westing;
50
    }
51
52 3
    public function getSouthing(): Length
53
    {
54 3
        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 2
    public function calculateDistance(Point $to): Length
62
    {
63 2
        if ($to->getCRS()->getEpsgCode() !== $this->crs->getEpsgCode()) {
64 1
            throw new InvalidCoordinateReferenceSystemException('Can only calculate distances between two points in the same CRS');
65
        }
66
67
        /* @var WestingSouthingPoint $to */
68 1
        return new Metre(
69 1
            sqrt(
70 1
                ($to->getWesting()->getValue() - $this->getWesting()->getValue()) ** 2 +
71 1
                ($to->getSouthing()->getValue() - $this->getSouthing()->getValue()) ** 2
72
            )
73
        );
74
    }
75
76 1
    public function __toString(): string
77
    {
78 1
        $values = [];
79 1
        foreach ($this->getCRS()->getCoordinateSystem()->getAxes() as $axis) {
80 1
            if ($axis->getName() === Axis::WESTING) {
81 1
                $values[] = $this->westing;
82 1
            } elseif ($axis->getName() === Axis::SOUTHING) {
83 1
                $values[] = $this->southing;
84
            } else {
85
                throw new UnknownAxisException(); // @codeCoverageIgnore
86
            }
87
        }
88
89 1
        return '(' . implode(', ', $values) . ')';
90
    }
91
}
92