Passed
Push — master ( 6be4bd...2c19f0 )
by Doug
51:47
created

OSTNOSGM15Grid   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 37
c 2
b 0
f 0
dl 0
loc 77
ccs 0
cts 41
cp 0
rs 10
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 8 1
A getValues() 0 5 1
A __construct() 0 10 1
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
    public function __construct($filename)
27
    {
28
        $this->gridFile = new SplFileObject($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
        $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