1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHPCoord. |
4
|
|
|
* |
5
|
|
|
* @author Doug Wright |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace PHPCoord\CoordinateOperation; |
10
|
|
|
|
11
|
|
|
use function array_map; |
12
|
|
|
use function array_slice; |
13
|
|
|
use function assert; |
14
|
|
|
use function count; |
15
|
|
|
use function explode; |
16
|
|
|
use function max; |
17
|
|
|
use function min; |
18
|
|
|
use function preg_match; |
19
|
|
|
use function preg_replace; |
20
|
|
|
|
21
|
|
|
use SplFileObject; |
22
|
|
|
use SplFixedArray; |
23
|
|
|
|
24
|
|
|
use function str_repeat; |
25
|
|
|
use function str_replace; |
26
|
|
|
use function strlen; |
27
|
|
|
use function trim; |
28
|
|
|
|
29
|
|
|
trait IGNFGrid |
30
|
|
|
{ |
31
|
|
|
use BilinearInterpolation; |
32
|
|
|
|
33
|
|
|
private int $valuesPerCoordinate; |
34
|
|
|
|
35
|
|
|
private SplFixedArray $data; |
36
|
|
|
|
37
|
5 |
|
public function __construct($filename) |
38
|
|
|
{ |
39
|
5 |
|
$this->gridFile = new SplFileObject($filename); |
|
|
|
|
40
|
|
|
|
41
|
5 |
|
match ($this->gridFile->getExtension()) { |
42
|
2 |
|
'txt' => $this->initTxt(), |
|
|
|
|
43
|
3 |
|
'mnt', 'tac' => $this->initMntOrTac(), |
|
|
|
|
44
|
|
|
}; |
45
|
|
|
} |
46
|
|
|
|
47
|
5 |
|
protected function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues |
48
|
|
|
{ |
49
|
5 |
|
$recordId = match ($this->storageOrder) { |
50
|
5 |
|
self::STORAGE_ORDER_INCREASING_LATITUDE_INCREASING_LONGITUDE => ($longitudeIndex * $this->numberOfRows + $latitudeIndex), |
|
|
|
|
51
|
1 |
|
self::STORAGE_ORDER_INCREASING_LONGITUDE_DECREASING_LATIITUDE => ($this->numberOfRows - $latitudeIndex - 1) * $this->numberOfColumns + $longitudeIndex, |
|
|
|
|
52
|
|
|
}; |
53
|
|
|
|
54
|
5 |
|
$record = $this->data[$recordId]; |
55
|
|
|
|
56
|
5 |
|
$longitude = $longitudeIndex * $this->columnGridInterval + $this->startX; |
57
|
5 |
|
$latitude = $latitudeIndex * $this->rowGridInterval + $this->startY; |
58
|
|
|
|
59
|
5 |
|
return new GridValues($longitude, $latitude, $record); |
60
|
|
|
} |
61
|
|
|
|
62
|
2 |
|
private function initTxt(): void |
63
|
|
|
{ |
64
|
2 |
|
$this->valuesPerCoordinate = 3; |
65
|
|
|
|
66
|
2 |
|
$header0 = $this->gridFile->fgets(); |
|
|
|
|
67
|
2 |
|
$header1 = $this->gridFile->fgets(); |
68
|
2 |
|
$header2 = $this->gridFile->fgets(); |
69
|
2 |
|
$header3 = $this->gridFile->fgets(); |
|
|
|
|
70
|
|
|
|
71
|
2 |
|
$interpolationMethod = trim(str_replace('GR3D2', '', $header2)); |
72
|
2 |
|
assert($interpolationMethod === 'INTERPOLATION BILINEAIRE'); |
73
|
|
|
|
74
|
2 |
|
$gridDimensions = explode(' ', trim(preg_replace('/ +/', ' ', str_replace('GR3D1', '', $header1)))); |
75
|
2 |
|
$this->startX = (float) $gridDimensions[0]; |
76
|
2 |
|
$this->endX = (float) $gridDimensions[1]; |
77
|
2 |
|
$this->startY = (float) $gridDimensions[2]; |
78
|
2 |
|
$this->endY = (float) $gridDimensions[3]; |
79
|
2 |
|
$this->columnGridInterval = (float) $gridDimensions[4]; |
80
|
2 |
|
$this->rowGridInterval = (float) $gridDimensions[5]; |
81
|
2 |
|
$this->numberOfColumns = (int) (string) (($this->endX - $this->startX) / $this->columnGridInterval) + 1; |
82
|
2 |
|
$this->numberOfRows = (int) (string) (($this->endY - $this->startY) / $this->rowGridInterval) + 1; |
83
|
2 |
|
$this->storageOrder = self::STORAGE_ORDER_INCREASING_LATITUDE_INCREASING_LONGITUDE; |
|
|
|
|
84
|
|
|
|
85
|
2 |
|
$this->data = new SplFixedArray($this->numberOfColumns * $this->numberOfRows); |
86
|
2 |
|
for ($i = 0, $numValues = $this->numberOfColumns * $this->numberOfRows; $i < $numValues; ++$i) { |
87
|
2 |
|
$rowData = explode(' ', trim(preg_replace('/ +/', ' ', $this->gridFile->fgets()))); |
88
|
2 |
|
$wantedData = array_slice($rowData, 3, 3); // ignore weird first fixed value, coordinates, precision and map sheet |
89
|
2 |
|
$wantedData = array_map(static fn (string $value) => (float) $value, $wantedData); |
90
|
2 |
|
$this->data[$i] = $wantedData; |
91
|
|
|
} |
92
|
|
|
|
93
|
2 |
|
$this->gridFile->fgets(); |
94
|
2 |
|
assert($this->gridFile->eof()); |
95
|
|
|
} |
96
|
|
|
|
97
|
3 |
|
private function initMntOrTac(): void |
98
|
|
|
{ |
99
|
3 |
|
$fixedHeaderRegexp = '^(-?[\d.]+) (-?[\d.]+) (-?[\d.]+) (-?[\d.]+) ([\d.]+) ([\d.]+) ([1-4]) ([01]) (\d) ([01]) '; |
100
|
3 |
|
$header = $this->gridFile->fgets(); |
101
|
|
|
|
102
|
3 |
|
preg_match('/' . $fixedHeaderRegexp . '/', $header, $fixedHeaderParts); |
103
|
|
|
|
104
|
3 |
|
$this->startX = min((float) $fixedHeaderParts[1], (float) $fixedHeaderParts[2]); |
105
|
3 |
|
$this->endX = max((float) $fixedHeaderParts[1], (float) $fixedHeaderParts[2]); |
106
|
3 |
|
$this->startY = min((float) $fixedHeaderParts[3], (float) $fixedHeaderParts[4]); |
107
|
3 |
|
$this->endY = max((float) $fixedHeaderParts[3], (float) $fixedHeaderParts[4]); |
108
|
3 |
|
$this->columnGridInterval = (float) $fixedHeaderParts[5]; |
109
|
3 |
|
$this->rowGridInterval = (float) $fixedHeaderParts[6]; |
110
|
3 |
|
$this->numberOfColumns = (int) (string) (($this->endX - $this->startX) / $this->columnGridInterval) + 1; |
111
|
3 |
|
$this->numberOfRows = (int) (string) (($this->endY - $this->startY) / $this->rowGridInterval) + 1; |
112
|
3 |
|
$this->storageOrder = (int) $fixedHeaderParts[7]; |
|
|
|
|
113
|
3 |
|
$coordinatesIncludedInData = (bool) $fixedHeaderParts[8]; |
114
|
3 |
|
$this->valuesPerCoordinate = (int) $fixedHeaderParts[9]; |
115
|
3 |
|
$precisionIncluded = (bool) $fixedHeaderParts[10]; |
116
|
|
|
|
117
|
3 |
|
preg_match('/' . $fixedHeaderRegexp . str_repeat('(-?[\d.]+) ', $this->valuesPerCoordinate) . '(.*)$/', $header, $fullHeaderParts); |
118
|
|
|
|
119
|
3 |
|
$baseAdjustments = array_slice($fullHeaderParts, count($fullHeaderParts) - $this->valuesPerCoordinate - 1, $this->valuesPerCoordinate); |
120
|
3 |
|
foreach ($baseAdjustments as $baseAdjustment) { |
121
|
3 |
|
assert((float) $baseAdjustment === 0.0); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// these files are not always 1 record per line (sometimes blank lines, sometimes multiple records per row) |
125
|
|
|
// so direct file access is not possible. Read into memory instead :/ |
126
|
|
|
|
127
|
3 |
|
$this->data = new SplFixedArray($this->numberOfColumns * $this->numberOfRows); |
128
|
|
|
|
129
|
3 |
|
$rawData = $this->gridFile->fread($this->gridFile->getSize() - strlen($header)); |
130
|
3 |
|
$values = explode(' ', trim(preg_replace('/\s+/', ' ', $rawData))); |
131
|
|
|
|
132
|
3 |
|
$cursor = 0; |
133
|
3 |
|
for ($i = 0, $numValues = $this->numberOfColumns * $this->numberOfRows; $i < $numValues; ++$i) { |
134
|
3 |
|
if ($coordinatesIncludedInData) { |
135
|
2 |
|
$cursor += 2; |
136
|
|
|
} |
137
|
|
|
|
138
|
3 |
|
$rowData = []; |
139
|
3 |
|
for ($j = 0; $j < $this->valuesPerCoordinate; ++$j) { |
140
|
3 |
|
$rowData[] = (float) $values[$cursor]; |
141
|
3 |
|
++$cursor; |
142
|
|
|
} |
143
|
|
|
|
144
|
3 |
|
$this->data[$i] = $rowData; |
145
|
|
|
|
146
|
3 |
|
if ($precisionIncluded) { |
147
|
3 |
|
++$cursor; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
$this->gridFile->fgets(); |
152
|
3 |
|
assert($this->gridFile->eof()); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|