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

OSGM15IrelandGrid::__construct()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 20
nc 4
nop 1
dl 0
loc 25
ccs 21
cts 21
cp 1
crap 4
rs 9.6
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 PHPCoord\UnitOfMeasure\Length\Metre;
12
use SplFileObject;
13
use SplFixedArray;
14
15
use function explode;
16
use function preg_replace;
17
use function preg_split;
18
use function trim;
19
20
class OSGM15IrelandGrid extends GeographicGeoidHeightGrid
21
{
22
    use BilinearInterpolation;
23
24
    private SplFixedArray $textData;
25
26 1
    public function __construct($filename)
27
    {
28 1
        $this->gridFile = new SplFileObject($filename);
29 1
        $this->storageOrder = self::STORAGE_ORDER_INCREASING_LONGITUDE_DECREASING_LATIITUDE;
30
31 1
        $header = preg_split('/\s+/', trim($this->gridFile->fgets()));
32
33 1
        $this->startX = (float) $header[2];
34 1
        $this->startY = (float) $header[0];
35 1
        $this->endX = (float) $header[3];
36 1
        $this->endY = (float) $header[1];
37 1
        $this->columnGridInterval = (float) $header[5];
38 1
        $this->rowGridInterval = (float) $header[4];
39 1
        $this->numberOfColumns = (int) (string) (($this->endX - $this->startX) / $this->columnGridInterval) + 1;
40 1
        $this->numberOfRows = (int) (string) (($this->endY - $this->startY) / $this->rowGridInterval) + 1;
41
42 1
        $this->textData = new SplFixedArray($this->numberOfColumns * $this->numberOfRows);
43 1
        $index = 0;
44 1
        while (!$this->gridFile->eof()) {
45 1
            $rawData = trim($this->gridFile->fgets());
46 1
            if ($rawData) {
47 1
                $values = explode(' ', trim(preg_replace('/\s+/', ' ', $rawData)));
48 1
                foreach ($values as $value) {
49 1
                    $this->textData[$index] = (float) $value;
50 1
                    ++$index;
51
                }
52
            }
53
        }
54
    }
55
56
    /**
57
     * @return Metre[]
58
     */
59 1
    public function getValues($x, $y): array
60
    {
61 1
        $shift = $this->interpolate($x, $y)[0];
62
63 1
        return [new Metre($shift)];
64
    }
65
66 1
    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...
67
    {
68 1
        $recordId = ($this->numberOfRows - $latitudeIndex - 1) * $this->numberOfColumns + $longitudeIndex;
69 1
        $longitude = $longitudeIndex * $this->columnGridInterval + $this->startX;
70 1
        $latitude = $latitudeIndex * $this->rowGridInterval + $this->startY;
71
72 1
        return new GridValues(
73 1
            $longitude,
74 1
            $latitude,
75 1
            [$this->textData[$recordId]]
76 1
        );
77
    }
78
}
79