Passed
Push — master ( 0baa7a...f13449 )
by Doug
50:51
created

OSGM15IrelandGrid   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 61
ccs 0
cts 33
cp 0
rs 10
c 1
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 25 4
A getValues() 0 5 1
A getRecord() 0 12 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 Composer\Pcre\Preg;
12
use PHPCoord\UnitOfMeasure\Length\Metre;
13
use SplFixedArray;
14
15
use function explode;
16
use function trim;
17
use function assert;
18
19
class OSGM15IrelandGrid extends GeographicGeoidHeightGrid
20
{
21
    use BilinearInterpolation;
22
23
    /**
24
     * @var SplFixedArray<float>
25
     */
26
    private SplFixedArray $textData;
27
28
    public function __construct(string $filename)
29
    {
30
        $this->gridFile = new GridFile($filename);
31
        $this->storageOrder = self::STORAGE_ORDER_INCREASING_LONGITUDE_DECREASING_LATIITUDE;
32
33
        $header = Preg::split('/\s+/', trim($this->gridFile->fgets()));
34
35
        $this->startX = (float) $header[2];
36
        $this->startY = (float) $header[0];
37
        $this->endX = (float) $header[3];
38
        $this->endY = (float) $header[1];
39
        $this->columnGridInterval = (float) $header[5];
40
        $this->rowGridInterval = (float) $header[4];
41
        $this->numberOfColumns = (int) (string) (($this->endX - $this->startX) / $this->columnGridInterval) + 1;
42
        $this->numberOfRows = (int) (string) (($this->endY - $this->startY) / $this->rowGridInterval) + 1;
43
44
        $this->textData = new SplFixedArray($this->numberOfColumns * $this->numberOfRows);
45
        $index = 0;
46
        while (!$this->gridFile->eof()) {
47
            $rawData = trim($this->gridFile->fgets());
48
            if ($rawData) {
49
                $values = explode(' ', trim(Preg::replace('/\s+/', ' ', $rawData)));
50
                foreach ($values as $value) {
51
                    $this->textData[$index] = (float) $value;
52
                    ++$index;
53
                }
54
            }
55
        }
56
    }
57
58
    /**
59
     * @return Metre[]
60
     */
61
    public function getValues(float $x, float $y): array
62
    {
63
        $shift = $this->interpolate($x, $y)[0];
64
65
        return [new Metre($shift)];
66
    }
67
68
    private function getRecord(int $longitudeIndex, int $latitudeIndex): GridValues
0 ignored issues
show
Unused Code introduced by
The method getRecord() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
69
    {
70
        $recordId = ($this->numberOfRows - $latitudeIndex - 1) * $this->numberOfColumns + $longitudeIndex;
71
        $longitude = $longitudeIndex * $this->columnGridInterval + $this->startX;
72
        $latitude = $latitudeIndex * $this->rowGridInterval + $this->startY;
73
74
        assert($this->textData[$recordId] !== null);
75
76
        return new GridValues(
77
            $longitude,
78
            $latitude,
79
            [$this->textData[$recordId]]
80
        );
81
    }
82
}
83