Passed
Push — master ( fd93b5...48e916 )
by Doug
40:26 queued 29:39
created

OSTNOSGM15Grid::getRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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