Passed
Push — master ( 2c19f0...d4552d )
by Doug
46:35
created

OSTNOSGM15Grid   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 37
dl 0
loc 77
ccs 41
cts 41
cp 1
rs 10
c 2
b 0
f 0
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A applyForwardHorizontalAdjustment() 0 8 1
A getHeightAdjustment() 0 5 1
A getRecord() 0 8 1
A getValues() 0 5 1
A __construct() 0 10 1
A applyReverseHorizontalAdjustment() 0 22 3
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\CoordinateOperation;
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\Cartesian;
13
use PHPCoord\Datum\Datum;
14
use PHPCoord\ProjectedPoint;
15
use PHPCoord\UnitOfMeasure\Length\Metre;
16
use SplFileObject;
17
18
use function abs;
19
20
class OSTNOSGM15Grid extends Grid
21
{
22
    use BilinearInterpolation;
23
24
    private const ITERATION_CONVERGENCE = 0.0001;
25
26 6
    public function __construct($filename)
27
    {
28 6
        $this->gridFile = new SplFileObject($filename);
29
30 6
        $this->startX = 0;
31 6
        $this->startY = 0;
32 6
        $this->columnGridInterval = 1000;
33 6
        $this->rowGridInterval = 1000;
34 6
        $this->numberOfColumns = 700;
35 6
        $this->numberOfRows = 1250;
36
    }
37
38 3
    public function applyForwardHorizontalAdjustment(ProjectedPoint $point): ProjectedPoint
39
    {
40 3
        $adjustment = $this->getValues($point->getEasting()->asMetres()->getValue(), $point->getNorthing()->asMetres()->getValue());
41
42 3
        $easting = $point->getEasting()->add($adjustment[0]);
43 3
        $northing = $point->getNorthing()->add($adjustment[1]);
44
45 3
        return ProjectedPoint::createFromEastingNorthing(Projected::fromSRID(Projected::EPSG_OSGB36_BRITISH_NATIONAL_GRID), $easting, $northing, $point->getCoordinateEpoch());
46
    }
47
48 2
    public function applyReverseHorizontalAdjustment(ProjectedPoint $point): ProjectedPoint
49
    {
50 2
        $osgb36NationalGrid = Projected::fromSRID(Projected::EPSG_OSGB36_BRITISH_NATIONAL_GRID);
51 2
        $etrs89NationalGrid = new Projected(
52 2
            'ETRS89 / National Grid',
53 2
            Cartesian::fromSRID(Cartesian::EPSG_2D_AXES_EASTING_NORTHING_E_N_ORIENTATIONS_EAST_NORTH_UOM_M),
54 2
            Datum::fromSRID(Datum::EPSG_EUROPEAN_TERRESTRIAL_REFERENCE_SYSTEM_1989_ENSEMBLE),
55 2
            $osgb36NationalGrid->getBoundingArea()
56 2
        );
57
58 2
        $adjustment = [new Metre(0), new Metre(0)];
59 2
        $easting = $point->getEasting();
60 2
        $northing = $point->getNorthing();
61
62
        do {
63 2
            $prevAdjustment = $adjustment;
64 2
            $adjustment = $this->getValues($easting->asMetres()->getValue(), $northing->asMetres()->getValue());
65 2
            $easting = $point->getEasting()->subtract($adjustment[0]);
66 2
            $northing = $point->getNorthing()->subtract($adjustment[1]);
67 2
        } while (abs($adjustment[0]->subtract($prevAdjustment[0])->getValue()) > self::ITERATION_CONVERGENCE && abs($adjustment[1]->subtract($prevAdjustment[1])->getValue()) > self::ITERATION_CONVERGENCE);
68
69 2
        return ProjectedPoint::createFromEastingNorthing($etrs89NationalGrid, $easting, $northing, $point->getCoordinateEpoch());
70
    }
71
72 3
    public function getHeightAdjustment(ProjectedPoint $point): Metre
73
    {
74 3
        $adjustment = $this->getValues($point->getEasting()->asMetres()->getValue(), $point->getNorthing()->asMetres()->getValue());
75
76 3
        return $adjustment[2];
77
    }
78
79
    /**
80
     * @return Metre[]
81
     */
82 8
    public function getValues(float $x, float $y): array
83
    {
84 8
        $offsets = $this->interpolate($x, $y);
85
86 8
        return [new Metre($offsets[0]), new Metre($offsets[1]), new Metre($offsets[2])];
87
    }
88
89 8
    private function getRecord(int $eastIndex, int $northIndex): GridValues
0 ignored issues
show
Unused Code introduced by
The method getRecord() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
90
    {
91 8
        $record = $northIndex * 701 + $eastIndex + 1;
92
93 8
        $this->gridFile->seek($record);
94 8
        $rawData = $this->gridFile->fgetcsv();
95
96 8
        return new GridValues((float) $rawData[1], (float) $rawData[2], [(float) $rawData[3], (float) $rawData[4], (float) $rawData[5]]);
97
    }
98
}
99