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\Angle\ArcSecond; |
12
|
|
|
|
13
|
|
|
use function assert; |
14
|
|
|
use function round; |
15
|
|
|
use function usort; |
16
|
|
|
|
17
|
|
|
class NTv2Grid extends GeographicGrid |
18
|
|
|
{ |
19
|
|
|
private const RECORD_SIZE = 16; |
20
|
|
|
private const FLAG_WITHIN_LIMITS = 1; |
21
|
|
|
private const FLAG_ON_UPPER_LATITUDE = 2; |
22
|
|
|
private const FLAG_ON_UPPER_LONGITUDE = 3; |
23
|
|
|
private const FLAG_ON_UPPER_LATITUDE_AND_LONGITUDE = 4; |
24
|
|
|
|
25
|
|
|
private string $integerFormatChar = 'V'; |
26
|
|
|
private string $doubleFormatChar = 'e'; |
27
|
|
|
private string $floatFormatChar = 'g'; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array<string, array{offsetStart: int, S_LAT: float, N_LAT: float, E_LONG: float, W_LONG: float, LONG_INC: float, LAT_INC: float}> |
31
|
|
|
*/ |
32
|
|
|
private array $subFileMetaData = []; |
33
|
|
|
|
34
|
5 |
|
public function __construct(string $filename) |
35
|
|
|
{ |
36
|
5 |
|
$this->gridFile = new GridFile($filename); |
37
|
5 |
|
$this->storageOrder = self::STORAGE_ORDER_INCREASING_LATITUDE_INCREASING_LONGITUDE; |
38
|
|
|
|
39
|
5 |
|
$this->readHeader(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return ArcSecond[] |
44
|
|
|
*/ |
45
|
5 |
|
public function getValues(float $x, float $y): array |
46
|
|
|
{ |
47
|
|
|
// NTv2 is longitude positive *west* |
48
|
5 |
|
$x *= -1; |
49
|
|
|
|
50
|
|
|
// NTv2 is in seconds, not degrees |
51
|
5 |
|
$x *= 3600; |
52
|
5 |
|
$y *= 3600; |
53
|
|
|
|
54
|
5 |
|
$gridToUse = $this->determineBestGrid($x, $y); |
55
|
|
|
|
56
|
5 |
|
return $gridToUse->getValues($x, $y); |
57
|
|
|
} |
58
|
|
|
|
59
|
5 |
|
private function readHeader(): void |
60
|
|
|
{ |
61
|
5 |
|
$this->gridFile->fseek(0); |
62
|
5 |
|
$rawData = $this->gridFile->fread(11 * self::RECORD_SIZE); |
63
|
5 |
|
if ($this->unpack('VNUM_OREC', $rawData, 8)['NUM_OREC'] !== 11) { |
64
|
|
|
$this->integerFormatChar = 'N'; |
65
|
|
|
$this->doubleFormatChar = 'E'; |
66
|
|
|
$this->floatFormatChar = 'G'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** @var array{NUM_OREC: int, NUM_SREC: int, NUM_FILE: int, GS_TYPE: string} $data */ |
70
|
5 |
|
$data = $this->unpack("A8/{$this->integerFormatChar}NUM_OREC/x4/A8/{$this->integerFormatChar}NUM_SREC/x4/A8/{$this->integerFormatChar}NUM_FILE/x4/A8/A8GS_TYPE/A8/A8VERSION/A8/A8SYSTEM_F/A8/A8SYSTEM_T/A8/{$this->doubleFormatChar}MAJOR_F/A8/{$this->doubleFormatChar}MINOR_F/A8/{$this->doubleFormatChar}MAJOR_T/A8/{$this->doubleFormatChar}MINOR_T", $rawData); |
71
|
|
|
|
72
|
5 |
|
assert($data['GS_TYPE'] === 'SECONDS'); |
73
|
|
|
|
74
|
5 |
|
$subFileStart = 11 * self::RECORD_SIZE; |
75
|
5 |
|
for ($i = 0; $i < $data['NUM_FILE']; ++$i) { |
76
|
5 |
|
$this->gridFile->fseek($subFileStart); |
77
|
5 |
|
$subFileRawData = $this->gridFile->fread(11 * self::RECORD_SIZE); |
78
|
|
|
/** @var array{SUB_NAME: string, S_LAT: float, N_LAT: float, E_LONG: float, W_LONG: float, LONG_INC: float, LAT_INC: float, GS_COUNT: int} $subFileData */ |
79
|
5 |
|
$subFileData = $this->unpack("A8/A8SUB_NAME/A8/A8PARENT/A8/A8CREATED/A8/A8UPDATED/A8/{$this->doubleFormatChar}S_LAT/A8/{$this->doubleFormatChar}N_LAT/A8/{$this->doubleFormatChar}E_LONG/A8/{$this->doubleFormatChar}W_LONG/A8/{$this->doubleFormatChar}LAT_INC/A8/{$this->doubleFormatChar}LONG_INC/A8/{$this->integerFormatChar}GS_COUNT/x4", $subFileRawData); |
80
|
5 |
|
$subFileData['offsetStart'] = $subFileStart; |
81
|
|
|
|
82
|
|
|
// apply rounding to eliminate fp issues when being deserialized |
83
|
5 |
|
$subFileData['S_LAT'] = round($subFileData['S_LAT'], 5); |
84
|
5 |
|
$subFileData['N_LAT'] = round($subFileData['N_LAT'], 5); |
85
|
5 |
|
$subFileData['E_LONG'] = round($subFileData['E_LONG'], 5); |
86
|
5 |
|
$subFileData['W_LONG'] = round($subFileData['W_LONG'], 5); |
87
|
5 |
|
$this->subFileMetaData[$subFileData['SUB_NAME']] = $subFileData; |
88
|
|
|
|
89
|
5 |
|
$subFileStart += 11 * self::RECORD_SIZE + $subFileData['GS_COUNT'] * self::RECORD_SIZE; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
5 |
|
private function determineBestGrid(float $longitude, float $latitude): NTv2SubGrid |
94
|
|
|
{ |
95
|
5 |
|
$possibleGrids = []; |
96
|
5 |
|
foreach ($this->subFileMetaData as $subFileMetaDatum) { |
97
|
5 |
|
if ($latitude === $subFileMetaDatum['N_LAT'] && $longitude === $subFileMetaDatum['W_LONG']) { |
98
|
|
|
$possibleGrids[] = [self::FLAG_ON_UPPER_LATITUDE_AND_LONGITUDE, $subFileMetaDatum]; |
99
|
5 |
|
} elseif ($longitude === $subFileMetaDatum['W_LONG']) { |
100
|
|
|
$possibleGrids[] = [self::FLAG_ON_UPPER_LONGITUDE, $subFileMetaDatum]; |
101
|
5 |
|
} elseif ($latitude === $subFileMetaDatum['N_LAT']) { |
102
|
|
|
$possibleGrids[] = [self::FLAG_ON_UPPER_LATITUDE, $subFileMetaDatum]; |
103
|
5 |
|
} elseif ($latitude >= $subFileMetaDatum['S_LAT'] && $latitude <= $subFileMetaDatum['N_LAT'] && $longitude >= $subFileMetaDatum['E_LONG'] && $longitude <= $subFileMetaDatum['W_LONG']) { |
104
|
5 |
|
$possibleGrids[] = [self::FLAG_WITHIN_LIMITS, $subFileMetaDatum]; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
5 |
|
usort($possibleGrids, static fn ($a, $b) => $a[0] <=> $b[0] ?: $a[1]['LAT_INC'] <=> $b[1]['LAT_INC'] ?: $a[1]['LONG_INC'] <=> $b[1]['LONG_INC']); |
109
|
|
|
|
110
|
5 |
|
$gridToUse = $possibleGrids[0][1]; |
111
|
|
|
|
112
|
5 |
|
return new NTv2SubGrid( |
113
|
5 |
|
$this->gridFile->getPathname(), |
114
|
5 |
|
$gridToUse['offsetStart'], |
115
|
5 |
|
$gridToUse['S_LAT'], |
116
|
5 |
|
$gridToUse['N_LAT'], |
117
|
5 |
|
$gridToUse['E_LONG'], |
118
|
5 |
|
$gridToUse['W_LONG'], |
119
|
5 |
|
$gridToUse['LAT_INC'], |
120
|
5 |
|
$gridToUse['LONG_INC'], |
121
|
5 |
|
$this->floatFormatChar |
122
|
5 |
|
); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|