Passed
Push — master ( 0baa7a...f13449 )
by Doug
50:51
created

NADCON5Grid::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1.0005

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 1
dl 0
loc 15
ccs 11
cts 12
cp 0.9167
crap 1.0005
rs 9.9
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 UnexpectedValueException;
12
13
class NADCON5Grid extends Grid
14
{
15
    use BiquadraticInterpolation;
16
17
    private string $gridDataType;
18
19 8
    public function __construct(string $filename)
20
    {
21 8
        $this->gridFile = new GridFile($filename);
22
23 8
        $header = $this->getHeader();
24 8
        $this->startX = $header['xlonsw'];
25 8
        $this->startY = $header['xlatsw'];
26 8
        $this->columnGridInterval = $header['dlon'];
27 8
        $this->rowGridInterval = $header['dlat'];
28 8
        $this->numberOfColumns = $header['nlon'];
29 8
        $this->numberOfRows = $header['nlat'];
30
31 8
        $this->gridDataType = match ($header['ikind']) {
32 8
            1 => 'G',
33
            default => throw new UnexpectedValueException("Unknown ikind: {$header['ikind']}"),
34 8
        };
35
    }
36
37 8
    protected function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues
38
    {
39 8
        $startBytes = 52;
40 8
        $dataTypeBytes = $this->gridDataType === 'n' ? 2 : 4;
41 8
        $recordLength = 4 + $this->numberOfColumns * $dataTypeBytes + 4;
42
43 8
        $this->gridFile->fseek($startBytes + $recordLength * $latitudeIndex);
44 8
        $rawRow = $this->gridFile->fread($recordLength);
45
46
        /** @var float[] $row */
47 8
        $row = $this->unpack("Gstartbuffer/{$this->gridDataType}{$this->numberOfColumns}lon/Gendbuffer", $rawRow);
48
49 8
        return new GridValues(
50 8
            $longitudeIndex * $this->columnGridInterval + $this->startX,
51 8
            $latitudeIndex * $this->rowGridInterval + $this->startY,
52 8
            [$row['lon' . ($longitudeIndex + 1)]]
53 8
        );
54
    }
55
56
    /**
57
     * @return array{xlatsw: float, xlonsw: float, dlat: float, dlon: float, nlat: int, nlon: int, ikind: int}
58
     */
59 8
    private function getHeader(): array
60
    {
61 8
        $this->gridFile->fseek(0);
62 8
        $rawData = $this->gridFile->fread(52);
63
        /** @var array{xlatsw: float, xlonsw: float, dlat: float, dlon: float, nlat: int, nlon: int, ikind: int} $data */
64 8
        $data = $this->unpack('Gstartbuffer/Exlatsw/Exlonsw/Edlat/Edlon/Nnlat/Nnlon/Nikind/Gendbuffer/', $rawData);
65
66 8
        return $data;
67
    }
68
69
    /**
70
     * @return float[]
71
     */
72 8
    public function getValues(float $x, float $y): array
73
    {
74 8
        if ($x < 0) {
75 8
            $x += 360; // NADCON5 uses 0 = 360 = Greenwich
76
        }
77
78 8
        return $this->interpolate($x, $y);
0 ignored issues
show
introduced by
The expression return $this->interpolate($x, $y) returns an array which contains values of type double which are incompatible with the return type PHPCoord\UnitOfMeasure\A...OfMeasure\Length\Length mandated by PHPCoord\CoordinateOperation\Grid::getValues().
Loading history...
79
    }
80
}
81