Passed
Push — master ( aca0dd...3e5121 )
by Doug
22:36
created

NTv2SubGrid::getRecord()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 12
ccs 10
cts 10
cp 1
crap 1
rs 9.9666
1
<?php
2
/**
3
 * PHPCoord.
4
 *
5
 * @author Doug Wright
6
 */
7
declare(strict_types=1);
8
9
namespace PHPCoord\CoordinateOperation;
10
11
use SplFileObject;
12
use function unpack;
13
14
class NTv2SubGrid extends SplFileObject
15
{
16
    use BilinearInterpolation;
17
18
    private const RECORD_SIZE = 16;
19
20
    private int $offsetStart;
21
    private string $floatFormatChar;
22
23 5
    public function __construct(
24
        string $filename,
25
        int $offsetStart,
26
        float $minLatitude,
27
        float $maxLatitude,
28
        float $minLongitude,
29
        float $maxLongitude,
30
        float $latitudeInterval,
31
        float $longitudeInterval,
32
        string $floatFormatChar
33
    ) {
34 5
        parent::__construct($filename);
35 5
        $this->offsetStart = $offsetStart;
36 5
        $this->startX = $minLongitude;
37 5
        $this->endX = $maxLongitude;
38 5
        $this->startY = $minLatitude;
39 5
        $this->endY = $maxLatitude;
40 5
        $this->columnGridInterval = $longitudeInterval;
41 5
        $this->rowGridInterval = $latitudeInterval;
42 5
        $this->floatFormatChar = $floatFormatChar;
43 5
        $this->numberOfColumns = (int) (($this->endX - $this->startX) / $this->columnGridInterval);
44 5
        $this->numberOfRows = (int) (($this->endY - $this->startY) / $this->rowGridInterval);
45 5
    }
46
47 5
    public function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues
48
    {
49 5
        $recordIndex = $latitudeIndex * ($this->numberOfColumns + 1) + $longitudeIndex;
50 5
        $recordOffset = $this->offsetStart + ((11 + $recordIndex) * self::RECORD_SIZE);
51 5
        $this->fseek($recordOffset);
52 5
        $rawRecord = $this->fread(self::RECORD_SIZE);
53 5
        $shifts = unpack("{$this->floatFormatChar}LATITUDE_SHIFT/{$this->floatFormatChar}LONGITUDE_SHIFT/{$this->floatFormatChar}LATITUDE_ACCURACY/{$this->floatFormatChar}LONGITUDE_ACCURACY", $rawRecord);
54
55 5
        return new GridValues(
56 5
            $longitudeIndex * $this->columnGridInterval + $this->startX,
57 5
            $latitudeIndex * $this->rowGridInterval + $this->startY,
58 5
            [$shifts['LATITUDE_SHIFT'], $shifts['LONGITUDE_SHIFT']]
59
        );
60
    }
61
}
62