Passed
Push — master ( 0baa7a...f13449 )
by Doug
50:51
created

OSTNOSGM15Grid::getValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
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;
12
use PHPCoord\CoordinateSystem\Cartesian;
13
use PHPCoord\Datum\Datum;
14
use PHPCoord\Point\ProjectedPoint;
15
use PHPCoord\UnitOfMeasure\Length\Metre;
16
17
use function abs;
18
19
class OSTNOSGM15Grid extends Grid
20
{
21
    use BilinearInterpolation;
22
23
    private const ITERATION_CONVERGENCE = 0.0001;
24
25
    public function __construct(string $filename)
26
    {
27
        $this->gridFile = new GridFile($filename);
28
29
        $this->startX = 0;
30
        $this->startY = 0;
31
        $this->columnGridInterval = 1000;
32
        $this->rowGridInterval = 1000;
33
        $this->numberOfColumns = 700;
34
        $this->numberOfRows = 1250;
35
    }
36
37
    public function applyForwardHorizontalAdjustment(ProjectedPoint $point): ProjectedPoint
38
    {
39
        $adjustment = $this->getValues($point->getEasting()->asMetres()->getValue(), $point->getNorthing()->asMetres()->getValue());
40
41
        $easting = $point->getEasting()->add($adjustment[0]);
42
        $northing = $point->getNorthing()->add($adjustment[1]);
43
44
        return ProjectedPoint::createFromEastingNorthing(Projected::fromSRID(Projected::EPSG_OSGB36_BRITISH_NATIONAL_GRID), $easting, $northing, $point->getCoordinateEpoch());
45
    }
46
47
    public function applyReverseHorizontalAdjustment(ProjectedPoint $point): ProjectedPoint
48
    {
49
        $osgb36NationalGrid = Projected::fromSRID(Projected::EPSG_OSGB36_BRITISH_NATIONAL_GRID);
50
        $etrs89NationalGrid = new Projected(
51
            'ETRS89 / National Grid',
52
            Cartesian::fromSRID(Cartesian::EPSG_2D_AXES_EASTING_NORTHING_E_N_ORIENTATIONS_EAST_NORTH_UOM_M),
53
            Datum::fromSRID(Datum::EPSG_EUROPEAN_TERRESTRIAL_REFERENCE_SYSTEM_1989_ENSEMBLE),
54
            $osgb36NationalGrid->getBoundingArea()
55
        );
56
57
        $adjustment = [new Metre(0), new Metre(0)];
58
        $easting = $point->getEasting();
59
        $northing = $point->getNorthing();
60
61
        do {
62
            $prevAdjustment = $adjustment;
63
            $adjustment = $this->getValues($easting->asMetres()->getValue(), $northing->asMetres()->getValue());
64
            $easting = $point->getEasting()->subtract($adjustment[0]);
65
            $northing = $point->getNorthing()->subtract($adjustment[1]);
66
        } while (abs($adjustment[0]->subtract($prevAdjustment[0])->getValue()) > self::ITERATION_CONVERGENCE && abs($adjustment[1]->subtract($prevAdjustment[1])->getValue()) > self::ITERATION_CONVERGENCE);
67
68
        return ProjectedPoint::createFromEastingNorthing($etrs89NationalGrid, $easting, $northing, $point->getCoordinateEpoch());
69
    }
70
71
    public function getHeightAdjustment(ProjectedPoint $point): Metre
72
    {
73
        $adjustment = $this->getValues($point->getEasting()->asMetres()->getValue(), $point->getNorthing()->asMetres()->getValue());
74
75
        return $adjustment[2];
76
    }
77
78
    /**
79
     * @return Metre[]
80
     */
81
    public function getValues(float $x, float $y): array
82
    {
83
        $offsets = $this->interpolate($x, $y);
84
85
        return [new Metre($offsets[0]), new Metre($offsets[1]), new Metre($offsets[2])];
86
    }
87
88
    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...
89
    {
90
        $record = $northIndex * 701 + $eastIndex + 1;
91
92
        $this->gridFile->seek($record);
93
        /** @var array<int, string> $rawData */
94
        $rawData = $this->gridFile->fgetcsv();
95
96
        return new GridValues((float) $rawData[1], (float) $rawData[2], [(float) $rawData[3], (float) $rawData[4], (float) $rawData[5]]);
97
    }
98
}
99