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

NADCON5Grid::getForwardAdjustment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
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 SplFileObject;
12
use UnexpectedValueException;
13
use function unpack;
14
15
class NADCON5Grid extends Grid
16
{
17
    use BiquadraticInterpolation;
18
19
    private string $gridDataType;
20
21 7
    public function __construct($filename)
22
    {
23 7
        $this->gridFile = new SplFileObject($filename);
24
25 7
        $header = $this->getHeader();
26 7
        $this->startX = $header['xlonsw'];
27 7
        $this->startY = $header['xlatsw'];
28 7
        $this->columnGridInterval = $header['dlon'];
29 7
        $this->rowGridInterval = $header['dlat'];
30 7
        $this->numberOfColumns = $header['nlon'];
31 7
        $this->numberOfRows = $header['nlat'];
32
33 7
        $this->gridDataType = match ($header['ikind']) {
34 7
            1 => 'G',
35
            default => throw new UnexpectedValueException("Unknown ikind: {$header['ikind']}"),
36
        };
37 7
    }
38
39 7
    protected function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues
40
    {
41 7
        $startBytes = 52;
42 7
        $dataTypeBytes = $this->gridDataType === 'n' ? 2 : 4;
43 7
        $recordLength = 4 + $this->numberOfColumns * $dataTypeBytes + 4;
44
45 7
        $this->gridFile->fseek($startBytes + $recordLength * $latitudeIndex);
46 7
        $rawRow = $this->gridFile->fread($recordLength);
47 7
        $row = unpack("Gstartbuffer/{$this->gridDataType}{$this->numberOfColumns}lon/Gendbuffer", $rawRow);
48
49 7
        return new GridValues(
50 7
            $longitudeIndex * $this->columnGridInterval + $this->startX,
51 7
            $latitudeIndex * $this->rowGridInterval + $this->startY,
52 7
            [$row['lon' . ($longitudeIndex + 1)]]
53
        );
54
    }
55
56 7
    private function getHeader(): array
57
    {
58 7
        $this->gridFile->fseek(0);
59 7
        $rawData = $this->gridFile->fread(52);
60 7
        $data = unpack('Gstartbuffer/Exlatsw/Exlonsw/Edlat/Edlon/Nnlat/Nnlon/Nikind/Gendbuffer/', $rawData);
61
62 7
        return $data;
63
    }
64
65 7
    public function getValues(float $x, float $y): array
66
    {
67 7
        if ($x < 0) {
68 7
            $x += 360; // NADCON5 uses 0 = 360 = Greenwich
69
        }
70
71 7
        return $this->interpolate($x, $y);
72
    }
73
}
74