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

BEVHeightGrid   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 41 4
A getRecord() 0 10 1
A getValues() 0 5 1
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