Completed
Push — master ( 7a5cb9...59abba )
by Hannes
02:54
created

SRTM4Provider::loadTileReferencesFor()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 4

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 21
ccs 15
cts 15
cp 1
rs 9.0534
cc 4
eloc 13
nc 8
nop 2
crap 4
1
<?php
2
3
/*
4
 * This file is part of the Runalyze DEM Reader.
5
 *
6
 * (c) RUNALYZE <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Runalyze\DEM\Provider\GeoTIFF;
13
14
use Runalyze\DEM\Exception\InvalidArgumentException;
15
16
class SRTM4Provider extends AbstractGeoTIFFProvider
17
{
18
    /** @var int */
19
    const MAX_LATITUDE = 60;
20
21
    /** @var int */
22
    const MAX_LONGITUDE = 180;
23
24
    /** @var float */
25
    const DEGREES_PER_TILE = 5.0;
26
27
    /** @var float */
28
    protected $CurrentTileHorizontalReference;
29
30
    /** @var float */
31
    protected $CurrentTileVerticalReference;
32
33
    /** @var float top left latitude */
34
    protected $CurrentTileLatitude;
35
36
    /** @var float top left longitude */
37
    protected $CurrentTileLongitude;
38
39
    /** @var string */
40
    protected $FilenameFormat = 'srtm_%02d_%02d.tif';
41
42
    /** @var GeoTIFFReader */
43
    protected $ResourceReader;
44
45 13
    public function initResourceReader()
46
    {
47 13
        $this->ResourceReader = new GeoTIFFReader();
48 13
    }
49
50
    /**
51
     * @param $format
52
     */
53 1
    public function setFilenameFormat($format)
54
    {
55 1
        $this->FilenameFormat = $format;
56 1
    }
57
58
    /**
59
     * @param  float                    $latitude
60
     * @param  float                    $longitude
61
     * @return string
62
     * @throws InvalidArgumentException
63
     */
64 11
    protected function getFilenameFor($latitude, $longitude)
65
    {
66 11
        $this->loadTileReferencesFor($latitude, $longitude);
67
68 11
        return sprintf($this->FilenameFormat, $this->CurrentTileHorizontalReference, $this->CurrentTileVerticalReference);
69
    }
70
71
    /**
72
     * @param float $latitude
73
     * @param float $longitude
74
     */
75 11
    protected function loadTileReferencesFor($latitude, $longitude)
76
    {
77 11
        if (fmod($latitude, static::DEGREES_PER_TILE) === 0.0) {
78 1
            $this->CurrentTileVerticalReference = (static::MAX_LATITUDE - $latitude) / static::DEGREES_PER_TILE + 1;
79 1
        } else {
80 11
            $this->CurrentTileVerticalReference = ceil((static::MAX_LATITUDE - $latitude) / static::DEGREES_PER_TILE);
81
        }
82
83 11
        if (fmod($longitude, static::DEGREES_PER_TILE) === 0.0) {
84 1
            $this->CurrentTileHorizontalReference = (static::MAX_LONGITUDE + $longitude) / static::DEGREES_PER_TILE + 1;
85 1
        } else {
86 11
            $this->CurrentTileHorizontalReference = ceil((static::MAX_LONGITUDE + $longitude) / static::DEGREES_PER_TILE);
87
        }
88
89 11
        $this->CurrentTileLatitude = static::MAX_LATITUDE - (($this->CurrentTileVerticalReference - 1) * static::DEGREES_PER_TILE);
90 11
        $this->CurrentTileLongitude = (($this->CurrentTileHorizontalReference - 1) * static::DEGREES_PER_TILE) - static::MAX_LONGITUDE;
91
92 11
        if ($latitude < 0) {
93 3
            $this->CurrentTileLatitude = -$this->CurrentTileLatitude;
0 ignored issues
show
Documentation Bug introduced by
The property $CurrentTileLatitude was declared of type double, but -$this->CurrentTileLatitude is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
94 3
        }
95 11
    }
96
97
    /**
98
     * @param  float   $latitude
99
     * @param  float   $longitude
100
     * @return float[] array(row, col)
101
     */
102 7
    protected function getExactRowAndColFor($latitude, $longitude)
103
    {
104 7
        return $this->ResourceReader->getExactRowAndColFor(
105 7
            abs($this->CurrentTileLatitude - abs($latitude)) / static::DEGREES_PER_TILE,
106 7
            abs($this->CurrentTileLongitude - $longitude) / static::DEGREES_PER_TILE
107 7
        );
108
    }
109
}
110