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); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|