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 SplFixedArray; |
13
|
|
|
|
14
|
|
|
use function explode; |
15
|
|
|
use function max; |
16
|
|
|
use function min; |
17
|
|
|
use function round; |
18
|
|
|
use function str_replace; |
19
|
|
|
use function trim; |
20
|
|
|
use function assert; |
21
|
|
|
|
22
|
|
|
use const PHP_FLOAT_MAX; |
23
|
|
|
use const PHP_FLOAT_MIN; |
24
|
|
|
|
25
|
|
|
class GUGiKHeightGrid extends GeographicGeoidHeightGrid |
26
|
|
|
{ |
27
|
|
|
use BilinearInterpolation; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var SplFixedArray<float> |
31
|
|
|
*/ |
32
|
|
|
private SplFixedArray $data; |
33
|
|
|
|
34
|
|
|
public function __construct(string $filename) |
35
|
|
|
{ |
36
|
|
|
$this->storageOrder = self::STORAGE_ORDER_INCREASING_LONGITUDE_INCREASING_LATIITUDE; |
37
|
|
|
$this->gridFile = new GridFile($filename); |
38
|
|
|
$this->columnGridInterval = 0.01; // always |
39
|
|
|
$this->rowGridInterval = 0.01; // always |
40
|
|
|
|
41
|
|
|
// these files have variable length headers, and then are not rectangular!! |
42
|
|
|
do { |
43
|
|
|
$headerLine = str_replace(self::BOM, '', $this->gridFile->fgets()); |
44
|
|
|
} while ($headerLine && $headerLine[0] === '#'); |
45
|
|
|
|
46
|
|
|
$firstDataRowIndex = $this->gridFile->key() + 1; |
47
|
|
|
|
48
|
|
|
$startX = PHP_FLOAT_MAX; |
49
|
|
|
$endX = PHP_FLOAT_MIN; |
50
|
|
|
$startY = PHP_FLOAT_MAX; |
51
|
|
|
$endY = PHP_FLOAT_MIN; |
52
|
|
|
while ($row = $this->gridFile->fgets()) { |
53
|
|
|
$data = explode("\t", trim($row)); |
54
|
|
|
$startX = min($startX, (float) $data[1]); |
55
|
|
|
$endX = max($endX, (float) $data[1]); |
56
|
|
|
$startY = min($startY, (float) $data[0]); |
57
|
|
|
$endY = max($endY, (float) $data[0]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->startX = $startX; |
61
|
|
|
$this->startY = $startY; |
62
|
|
|
$this->numberOfColumns = (int) (string) (($endX - $startX) / $this->columnGridInterval) + 1; |
63
|
|
|
$this->numberOfRows = (int) (string) (($endY - $startY) / $this->rowGridInterval) + 1; |
64
|
|
|
|
65
|
|
|
// init with 0 |
66
|
|
|
$this->data = new SplFixedArray($this->numberOfColumns * $this->numberOfRows); |
67
|
|
|
for ($i = 0, $numValues = $this->numberOfColumns * $this->numberOfRows; $i < $numValues; ++$i) { |
68
|
|
|
$this->data[$i] = 0.0; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// fill in with actual values |
72
|
|
|
$this->gridFile->seek($firstDataRowIndex); |
73
|
|
|
while ($row = $this->gridFile->fgets()) { |
74
|
|
|
$rowData = explode("\t", trim($row)); |
75
|
|
|
|
76
|
|
|
$index = (int) round((((float) $rowData[0] - $this->startY) / $this->rowGridInterval) * $this->numberOfColumns + ((float) $rowData[1] - $this->startX) / $this->columnGridInterval); |
77
|
|
|
$this->data[$index] = (float) $rowData[2]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Metre[] |
83
|
|
|
*/ |
84
|
|
|
public function getValues(float $x, float $y): array |
85
|
|
|
{ |
86
|
|
|
$shift = $this->interpolate($x, $y)[0]; |
87
|
|
|
|
88
|
|
|
return [new Metre($shift)]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
protected function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues |
92
|
|
|
{ |
93
|
|
|
$recordId = $latitudeIndex * $this->numberOfColumns + $longitudeIndex; |
94
|
|
|
|
95
|
|
|
assert($this->data[$recordId] !== null); |
96
|
|
|
$record = $this->data[$recordId]; |
97
|
|
|
|
98
|
|
|
$longitude = $longitudeIndex * $this->columnGridInterval + $this->startX; |
99
|
|
|
$latitude = $latitudeIndex * $this->rowGridInterval + $this->startY; |
100
|
|
|
|
101
|
|
|
return new GridValues($longitude, $latitude, [$record]); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|