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 array_unique; |
16
|
|
|
use function end; |
17
|
|
|
use function explode; |
18
|
|
|
use function round; |
19
|
|
|
use function sort; |
20
|
|
|
use function trim; |
21
|
|
|
|
22
|
|
|
class BEVHeightGrid extends GeographicGeoidHeightGrid |
23
|
|
|
{ |
24
|
|
|
use BilinearInterpolation; |
25
|
|
|
|
26
|
|
|
private SplFixedArray $data; |
27
|
|
|
|
28
|
3 |
|
public function __construct($filename) |
29
|
|
|
{ |
30
|
3 |
|
$this->storageOrder = self::STORAGE_ORDER_INCREASING_LONGITUDE_INCREASING_LATIITUDE; |
31
|
3 |
|
$this->gridFile = new SplFileObject($filename); |
32
|
|
|
|
33
|
|
|
// these are not rectangular!! |
34
|
3 |
|
$xs = []; |
35
|
3 |
|
$ys = []; |
36
|
3 |
|
$this->gridFile->seek(1); |
37
|
3 |
|
while ($row = $this->gridFile->fgets()) { |
38
|
3 |
|
$data = explode(';', trim($row)); |
39
|
3 |
|
$xs[] = (float) $data[1]; |
40
|
3 |
|
$ys[] = (float) $data[0]; |
41
|
|
|
} |
42
|
3 |
|
$xs = array_unique($xs); |
43
|
3 |
|
$ys = array_unique($ys); |
44
|
3 |
|
sort($xs); |
45
|
3 |
|
sort($ys); |
46
|
|
|
|
47
|
3 |
|
$this->startX = $xs[0]; |
48
|
3 |
|
$this->startY = $ys[0]; |
49
|
3 |
|
$this->endX = end($xs); |
50
|
3 |
|
$this->endY = end($ys); |
51
|
3 |
|
$this->columnGridInterval = round($xs[1] - $xs[0], 7); |
52
|
3 |
|
$this->rowGridInterval = round($ys[1] - $ys[0], 7); |
53
|
3 |
|
$this->numberOfColumns = (int) (string) (($this->endX - $this->startX) / $this->columnGridInterval) + 1; |
54
|
3 |
|
$this->numberOfRows = (int) (string) (($this->endY - $this->startY) / $this->rowGridInterval) + 1; |
55
|
|
|
|
56
|
|
|
// init with 0 |
57
|
3 |
|
$this->data = new SplFixedArray($this->numberOfColumns * $this->numberOfRows); |
58
|
3 |
|
for ($i = 0, $numValues = $this->numberOfColumns * $this->numberOfRows; $i < $numValues; ++$i) { |
59
|
3 |
|
$this->data[$i] = 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// fill in with actual values |
63
|
3 |
|
$this->gridFile->seek(1); |
64
|
3 |
|
while ($row = $this->gridFile->fgets()) { |
65
|
3 |
|
$rowData = explode(';', trim($row)); |
66
|
|
|
|
67
|
3 |
|
$index = round((($rowData[0] - $this->startY) / $this->rowGridInterval) * $this->numberOfColumns + ($rowData[1] - $this->startX) / $this->columnGridInterval); |
68
|
3 |
|
$this->data[$index] = (float) $rowData[2]; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return Metre[] |
74
|
|
|
*/ |
75
|
3 |
|
public function getValues($x, $y): array |
76
|
|
|
{ |
77
|
3 |
|
$shift = $this->interpolate($x, $y)[0]; |
78
|
|
|
|
79
|
3 |
|
return [new Metre($shift)]; |
80
|
|
|
} |
81
|
|
|
|
82
|
3 |
|
protected function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues |
83
|
|
|
{ |
84
|
3 |
|
$recordId = $latitudeIndex * $this->numberOfColumns + $longitudeIndex; |
85
|
|
|
|
86
|
3 |
|
$record = $this->data[$recordId]; |
87
|
|
|
|
88
|
3 |
|
$longitude = $longitudeIndex * $this->columnGridInterval + $this->startX; |
89
|
3 |
|
$latitude = $latitudeIndex * $this->rowGridInterval + $this->startY; |
90
|
|
|
|
91
|
3 |
|
return new GridValues($longitude, $latitude, [$record]); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|