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

NTv2SubGrid   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 45
ccs 23
cts 23
cp 1
rs 10
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getRecord() 0 12 1
A __construct() 0 22 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 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