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

NTv2SubGrid::getValues()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
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 PHPCoord\UnitOfMeasure\Angle\ArcSecond;
12
use SplFileObject;
13
use function unpack;
14
15
class NTv2SubGrid extends Grid
16
{
17
    use BilinearInterpolation;
18
19
    private const RECORD_SIZE = 16;
20
21
    private int $offsetStart;
22
    private string $floatFormatChar;
23
24 5
    public function __construct(
25
        string $filename,
26
        int $offsetStart,
27
        float $minLatitude,
28
        float $maxLatitude,
29
        float $minLongitude,
30
        float $maxLongitude,
31
        float $latitudeInterval,
32
        float $longitudeInterval,
33
        string $floatFormatChar
34
    ) {
35 5
        $this->gridFile = new SplFileObject($filename);
36 5
        $this->offsetStart = $offsetStart;
37 5
        $this->startX = $minLongitude;
38 5
        $this->endX = $maxLongitude;
39 5
        $this->startY = $minLatitude;
40 5
        $this->endY = $maxLatitude;
41 5
        $this->columnGridInterval = $longitudeInterval;
42 5
        $this->rowGridInterval = $latitudeInterval;
43 5
        $this->floatFormatChar = $floatFormatChar;
44 5
        $this->numberOfColumns = (int) (($this->endX - $this->startX) / $this->columnGridInterval);
45 5
        $this->numberOfRows = (int) (($this->endY - $this->startY) / $this->rowGridInterval);
46 5
    }
47
48
    /**
49
     * @return ArcSecond[]
50
     */
51 5
    public function getValues(float $x, float $y): array
52
    {
53 5
        $shifts = $this->interpolate($x, $y);
54
55 5
        return [new ArcSecond($shifts[0]), new ArcSecond(-$shifts[1])]; // NTv2 is longitude positive *west*
56
    }
57
58 5
    protected function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues
59
    {
60 5
        $recordIndex = $latitudeIndex * ($this->numberOfColumns + 1) + $longitudeIndex;
61 5
        $recordOffset = $this->offsetStart + ((11 + $recordIndex) * self::RECORD_SIZE);
62 5
        $this->gridFile->fseek($recordOffset);
63 5
        $rawRecord = $this->gridFile->fread(self::RECORD_SIZE);
64 5
        $shifts = unpack("{$this->floatFormatChar}LATITUDE_SHIFT/{$this->floatFormatChar}LONGITUDE_SHIFT/{$this->floatFormatChar}LATITUDE_ACCURACY/{$this->floatFormatChar}LONGITUDE_ACCURACY", $rawRecord);
65
66 5
        return new GridValues(
67 5
            $longitudeIndex * $this->columnGridInterval + $this->startX,
68 5
            $latitudeIndex * $this->rowGridInterval + $this->startY,
69 5
            [$shifts['LATITUDE_SHIFT'], $shifts['LONGITUDE_SHIFT']]
70
        );
71
    }
72
}
73