OSTNOSGM15Grid::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 2
rs 10
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