OSTNOSGM15Grid   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getHeightAdjustment() 0 5 1
A applyForwardHorizontalAdjustment() 0 8 1
A applyReverseHorizontalAdjustment() 0 22 3
A getRecord() 0 9 1
A getValues() 0 5 1
A __construct() 0 10 1
1
<?php
2
3
/**
4
 * PHPCoord.
5
 *
6
 * @author Doug Wright
7
 */
8
declare(strict_types=1);
9
10
namespace PHPCoord\CoordinateOperation;
11
12
use PHPCoord\CoordinateReferenceSystem\Projected;
13
use PHPCoord\CoordinateSystem\Cartesian;
14
use PHPCoord\Datum\Datum;
15
use PHPCoord\Point\ProjectedPoint;
16
use PHPCoord\UnitOfMeasure\Length\Metre;
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
    public function __construct(string $filename)
27
    {
28
        $this->gridFile = new GridFile($filename);
29
30
        $this->startX = 0;
31
        $this->startY = 0;
32
        $this->columnGridInterval = 1000;
33
        $this->rowGridInterval = 1000;
34
        $this->numberOfColumns = 700;
35
        $this->numberOfRows = 1250;
36
    }
37
38
    public function applyForwardHorizontalAdjustment(ProjectedPoint $point): ProjectedPoint
39
    {
40
        $adjustment = $this->getValues($point->getEasting()->asMetres()->getValue(), $point->getNorthing()->asMetres()->getValue());
41
42
        $easting = $point->getEasting()->add($adjustment[0]);
43
        $northing = $point->getNorthing()->add($adjustment[1]);
44
45
        return ProjectedPoint::createFromEastingNorthing(Projected::fromSRID(Projected::EPSG_OSGB36_BRITISH_NATIONAL_GRID), $easting, $northing, $point->getCoordinateEpoch());
46
    }
47
48
    public function applyReverseHorizontalAdjustment(ProjectedPoint $point): ProjectedPoint
49
    {
50
        $osgb36NationalGrid = Projected::fromSRID(Projected::EPSG_OSGB36_BRITISH_NATIONAL_GRID);
51
        $etrs89NationalGrid = new Projected(
52
            'ETRS89 / National Grid',
53
            Cartesian::fromSRID(Cartesian::EPSG_2D_AXES_EASTING_NORTHING_E_N_ORIENTATIONS_EAST_NORTH_UOM_M),
54
            Datum::fromSRID(Datum::EPSG_EUROPEAN_TERRESTRIAL_REFERENCE_SYSTEM_1989_ENSEMBLE),
55
            $osgb36NationalGrid->getBoundingArea()
56
        );
57
58
        $adjustment = [new Metre(0), new Metre(0)];
59
        $easting = $point->getEasting();
60
        $northing = $point->getNorthing();
61
62
        do {
63
            $prevAdjustment = $adjustment;
64
            $adjustment = $this->getValues($easting->asMetres()->getValue(), $northing->asMetres()->getValue());
65
            $easting = $point->getEasting()->subtract($adjustment[0]);
66
            $northing = $point->getNorthing()->subtract($adjustment[1]);
67
        } while (abs($adjustment[0]->subtract($prevAdjustment[0])->getValue()) > self::ITERATION_CONVERGENCE && abs($adjustment[1]->subtract($prevAdjustment[1])->getValue()) > self::ITERATION_CONVERGENCE);
68
69
        return ProjectedPoint::createFromEastingNorthing($etrs89NationalGrid, $easting, $northing, $point->getCoordinateEpoch());
70
    }
71
72
    public function getHeightAdjustment(ProjectedPoint $point): Metre
73
    {
74
        $adjustment = $this->getValues($point->getEasting()->asMetres()->getValue(), $point->getNorthing()->asMetres()->getValue());
75
76
        return $adjustment[2];
77
    }
78
79
    /**
80
     * @return Metre[]
81
     */
82
    public function getValues(float $x, float $y): array
83
    {
84
        $offsets = $this->interpolate($x, $y);
85
86
        return [new Metre($offsets[0]), new Metre($offsets[1]), new Metre($offsets[2])];
87
    }
88
89
    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
        $record = $northIndex * 701 + $eastIndex + 1;
92
93
        $this->gridFile->seek($record);
94
        /** @var array<int, string> $rawData */
95
        $rawData = $this->gridFile->fgetcsv();
96
97
        return new GridValues((float) $rawData[1], (float) $rawData[2], [(float) $rawData[3], (float) $rawData[4], (float) $rawData[5]]);
98
    }
99
}
100