Passed
Push — master ( 344e29...cbaf3e )
by Doug
38:02
created

GTXGrid::getHeader()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0593

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 16
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 20
ccs 13
cts 16
cp 0.8125
crap 3.0593
rs 9.7333
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\Length\Metre;
12
use SplFileObject;
13
14
use function in_array;
15
use function substr;
16
use function unpack;
17
18
/**
19
 * @see https://vdatum.noaa.gov/docs/gtx_info.html for documentation
20
 */
21
class GTXGrid extends GeographicGeoidHeightGrid
22
{
23
    use BilinearInterpolation;
24
25
    private int $headerLength;
26
    private string $shiftDataType;
27
28 10
    public function __construct($filename)
29
    {
30 10
        $this->gridFile = new SplFileObject($filename);
31 10
        $this->storageOrder = self::STORAGE_ORDER_INCREASING_LONGITUDE_INCREASING_LATIITUDE;
32
33 10
        $header = $this->getHeader();
34 10
        $this->startX = $header['xlonsw'];
35 10
        $this->startY = $header['xlatsw'];
36 10
        $this->numberOfColumns = $header['nlon'];
37 10
        $this->numberOfRows = $header['nlat'];
38 10
        $this->columnGridInterval = $header['dlon'];
39 10
        $this->rowGridInterval = $header['dlat'];
40
41 10
        if ($this->startX > 180) { // normalise if necessary
42 2
            $this->startX -= 360;
43
        }
44
    }
45
46
    /**
47
     * @return Metre[]
48
     */
49 10
    public function getValues($x, $y): array
50
    {
51 10
        if ($x < $this->startX) { // normalise if necessary
52
            $x += 360;
53
        }
54
55 10
        $shift = $this->interpolate($x, $y)[0];
56
57
        // These are in millimeters for some reason... :/
58 10
        if (in_array($this->gridFile->getBasename(), ['vertconc.gtx', 'vertcone.gtx', 'vertconw.gtx'], true)) {
59 1
            $shift /= 1000;
60
        }
61
62 10
        return [new Metre($shift)];
63
    }
64
65 10
    private function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues
0 ignored issues
show
Unused Code introduced by
The method getRecord() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
66
    {
67 10
        $recordId = match ($this->storageOrder) {
68 10
            self::STORAGE_ORDER_INCREASING_LATITUDE_INCREASING_LONGITUDE => $longitudeIndex * $this->numberOfRows + $latitudeIndex,
69 10
            self::STORAGE_ORDER_INCREASING_LONGITUDE_DECREASING_LATIITUDE => ($this->numberOfRows - $latitudeIndex - 1) * $this->numberOfColumns + $longitudeIndex,
70 10
            self::STORAGE_ORDER_INCREASING_LONGITUDE_INCREASING_LATIITUDE => $latitudeIndex * $this->numberOfColumns + $longitudeIndex,
71 10
        };
72
73 10
        $offset = $this->headerLength + $recordId * 4;
74 10
        $this->gridFile->fseek($offset);
75 10
        $rawRow = $this->gridFile->fread(4);
76 10
        $data = unpack("{$this->shiftDataType}shift", $rawRow);
77
78 10
        return new GridValues(
79 10
            $longitudeIndex * $this->columnGridInterval + $this->startX,
80 10
            $latitudeIndex * $this->rowGridInterval + $this->startY,
81 10
            [$data['shift']]
82 10
        );
83
    }
84
85 10
    private function getHeader(): array
86
    {
87 10
        $this->gridFile->fseek(0);
88 10
        $rawHeader = $this->gridFile->fread(44);
89 10
        $ikind = substr($rawHeader, 40, 4);
90 10
        if (unpack('Nikind', $ikind)['ikind'] === 1) { // big endian
91
            $this->headerLength = 44;
92
            $this->shiftDataType = 'G';
93
            $data = unpack('Exlatsw/Exlonsw/Edlat/Edlon/Nnlat/Nnlon', $rawHeader);
94 10
        } elseif (unpack('Vikind', $ikind)['ikind'] === 1) { // little endian
95 1
            $this->headerLength = 44;
96 1
            $this->shiftDataType = 'g';
97 1
            $data = unpack('exlatsw/exlonsw/edlat/edlon/Vnlat/Vnlon', $rawHeader);
98
        } else { // not all files (e.g. NZ) have this endian check column, assume big endian
99 9
            $this->headerLength = 40;
100 9
            $this->shiftDataType = 'G';
101 9
            $data = unpack('Exlatsw/Exlonsw/Edlat/Edlon/Nnlat/Nnlon', $rawHeader);
102
        }
103
104 10
        return $data;
105
    }
106
}
107