Passed
Push — master ( 0a16ff...1038d1 )
by Doug
28:57
created

GUGiKHeightGrid::__construct()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 30
CRAP Score 6.0012

Importance

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