|
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
|
|
|
use SplFixedArray; |
|
14
|
|
|
|
|
15
|
|
|
use function explode; |
|
16
|
|
|
use function preg_match; |
|
17
|
|
|
use function preg_replace; |
|
18
|
|
|
use function strlen; |
|
19
|
|
|
use function trim; |
|
20
|
|
|
|
|
21
|
|
|
class IGNESHeightGrid extends GeographicGeoidHeightGrid |
|
22
|
|
|
{ |
|
23
|
|
|
use BilinearInterpolation; |
|
24
|
|
|
|
|
25
|
|
|
private const ITERATION_CONVERGENCE = 0.0001; |
|
26
|
|
|
|
|
27
|
|
|
private bool $coordinatesIncludedInData; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
private int $valuesPerCoordinate; |
|
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
private bool $precisionIncluded; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
private SplFixedArray $data; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct($filename) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->gridFile = new SplFileObject($filename); |
|
38
|
|
|
$this->init(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @return Metre[] |
|
43
|
|
|
*/ |
|
44
|
|
|
public function getValues(float $x, float $y): array |
|
45
|
|
|
{ |
|
46
|
|
|
$shift = $this->interpolate($x, $y)[0]; |
|
47
|
|
|
|
|
48
|
|
|
return [new Metre($shift)]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
protected function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues |
|
52
|
|
|
{ |
|
53
|
|
|
$recordId = ($this->numberOfRows - $latitudeIndex - 1) * $this->numberOfColumns + $longitudeIndex; |
|
54
|
|
|
|
|
55
|
|
|
$record = $this->data[$recordId]; |
|
56
|
|
|
|
|
57
|
|
|
$longitude = $longitudeIndex * $this->columnGridInterval + $this->startX; |
|
58
|
|
|
$latitude = $latitudeIndex * $this->rowGridInterval + $this->startY; |
|
59
|
|
|
|
|
60
|
|
|
return new GridValues($longitude, $latitude, $record); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function init(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$headerRegexp = '^\s+(-?[\d.]+)\s+(-?[\d.]+)\s+(-?[\d.]+)\s+(-?[\d.]+)\s+([\d.]+)\s+([\d.]+)'; |
|
66
|
|
|
$header = $this->gridFile->fgets(); |
|
67
|
|
|
|
|
68
|
|
|
preg_match('/' . $headerRegexp . '/', $header, $headerParts); |
|
69
|
|
|
|
|
70
|
|
|
$this->columnGridInterval = $headerParts[4] / 60; |
|
71
|
|
|
$this->rowGridInterval = $headerParts[3] / 60; |
|
72
|
|
|
$this->numberOfColumns = (int) $headerParts[6]; |
|
73
|
|
|
$this->numberOfRows = (int) $headerParts[5]; |
|
74
|
|
|
$this->startX = (float) $headerParts[2]; |
|
75
|
|
|
if ($this->startX > 180) { |
|
76
|
|
|
$this->startX -= 360; |
|
77
|
|
|
} |
|
78
|
|
|
$this->endX = $this->startX + ($this->numberOfColumns - 1) * $this->columnGridInterval; |
|
79
|
|
|
$this->endY = (float) $headerParts[1]; |
|
80
|
|
|
$this->startY = $this->endY - ($this->numberOfRows - 1) * $this->rowGridInterval; |
|
81
|
|
|
|
|
82
|
|
|
// these files are not 1 record per line so direct file access is not possible. Read into memory instead :/ |
|
83
|
|
|
|
|
84
|
|
|
$this->data = new SplFixedArray($this->numberOfColumns * $this->numberOfRows); |
|
85
|
|
|
|
|
86
|
|
|
$rawData = $this->gridFile->fread($this->gridFile->getSize() - strlen($header)); |
|
87
|
|
|
$values = explode(' ', trim(preg_replace('/\s+/', ' ', $rawData))); |
|
88
|
|
|
|
|
89
|
|
|
$cursor = 0; |
|
90
|
|
|
for ($i = 0, $numValues = $this->numberOfColumns * $this->numberOfRows; $i < $numValues; ++$i) { |
|
91
|
|
|
$rowData = [(float) $values[$cursor]]; |
|
92
|
|
|
++$cursor; |
|
93
|
|
|
|
|
94
|
|
|
$this->data[$i] = $rowData; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|