Passed
Push — master ( 2c19f0...d4552d )
by Doug
46:35
created

GUGiKHeightGrid   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 73
ccs 39
cts 39
cp 1
rs 10
wmc 8

3 Methods

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