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
|
|
|
class SRTM4Provider extends AbstractGeoTIFFProvider |
15
|
|
|
{ |
16
|
|
|
/** @var int */ |
17
|
|
|
const MAX_LATITUDE = 60; |
18
|
|
|
|
19
|
|
|
/** @var int */ |
20
|
|
|
const MAX_LONGITUDE = 180; |
21
|
|
|
|
22
|
|
|
/** @var float */ |
23
|
|
|
const DEGREES_PER_TILE = 5.0; |
24
|
|
|
|
25
|
|
|
/** @var float */ |
26
|
|
|
protected $CurrentTileHorizontalReference; |
27
|
|
|
|
28
|
|
|
/** @var float */ |
29
|
|
|
protected $CurrentTileVerticalReference; |
30
|
|
|
|
31
|
|
|
/** @var float top left latitude */ |
32
|
|
|
protected $CurrentTileLatitude; |
33
|
|
|
|
34
|
|
|
/** @var float top left longitude */ |
35
|
|
|
protected $CurrentTileLongitude; |
36
|
|
|
|
37
|
|
|
/** @var string */ |
38
|
|
|
protected $FilenameFormat = 'srtm_%02d_%02d.tif'; |
39
|
|
|
|
40
|
8 |
|
public function initResourceReader() |
41
|
|
|
{ |
42
|
8 |
|
$this->ResourceReader = new GeoTIFFReader(); |
43
|
8 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $format |
47
|
|
|
*/ |
48
|
|
|
public function setFilenameFormat($format) |
49
|
|
|
{ |
50
|
|
|
$this->FilenameFormat = $format; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param float $latitude |
55
|
|
|
* @param float $longitude |
56
|
|
|
* @return string |
57
|
|
|
* @throws \InvalidArgumentException |
58
|
|
|
*/ |
59
|
7 |
|
protected function getFilenameFor($latitude, $longitude) |
60
|
|
|
{ |
61
|
7 |
|
$this->loadTileReferencesFor($latitude, $longitude); |
62
|
|
|
|
63
|
7 |
|
return sprintf($this->FilenameFormat, $this->CurrentTileHorizontalReference, $this->CurrentTileVerticalReference); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param float $latitude |
68
|
|
|
* @param float $longitude |
69
|
|
|
*/ |
70
|
7 |
|
protected function loadTileReferencesFor($latitude, $longitude) |
71
|
|
|
{ |
72
|
7 |
|
if (fmod($latitude, static::DEGREES_PER_TILE) === 0) { |
73
|
|
|
$this->CurrentTileVerticalReference = (static::MAX_LATITUDE - $latitude) / static::DEGREES_PER_TILE + 1; |
74
|
|
|
} else { |
75
|
7 |
|
$this->CurrentTileVerticalReference = ceil((static::MAX_LATITUDE - $latitude) / static::DEGREES_PER_TILE); |
76
|
|
|
} |
77
|
|
|
|
78
|
7 |
|
if (fmod($longitude, static::DEGREES_PER_TILE) === 0) { |
79
|
|
|
$this->CurrentTileHorizontalReference = (static::MAX_LONGITUDE + $longitude) / static::DEGREES_PER_TILE + 1; |
80
|
|
|
} else { |
81
|
7 |
|
$this->CurrentTileHorizontalReference = ceil((static::MAX_LONGITUDE + $longitude) / static::DEGREES_PER_TILE); |
82
|
|
|
} |
83
|
|
|
|
84
|
7 |
|
$this->CurrentTileLatitude = static::MAX_LATITUDE - (($this->CurrentTileVerticalReference - 1) * static::DEGREES_PER_TILE); |
85
|
7 |
|
$this->CurrentTileLongitude = (($this->CurrentTileHorizontalReference - 1) * static::DEGREES_PER_TILE) - static::MAX_LONGITUDE; |
86
|
|
|
|
87
|
7 |
|
if ($latitude < 0) { |
88
|
3 |
|
$this->CurrentTileLatitude = -$this->CurrentTileLatitude; |
|
|
|
|
89
|
3 |
|
} |
90
|
7 |
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param float $latitude |
94
|
|
|
* @param float $longitude |
95
|
|
|
* @return float[] array(row, col) |
96
|
|
|
*/ |
97
|
6 |
|
protected function getExactRowAndColFor($latitude, $longitude) |
98
|
|
|
{ |
99
|
6 |
|
return $this->ResourceReader->getExactRowAndColFor( |
|
|
|
|
100
|
6 |
|
abs($this->CurrentTileLatitude - abs($latitude)) / static::DEGREES_PER_TILE, |
101
|
6 |
|
abs($this->CurrentTileLongitude - $longitude) / static::DEGREES_PER_TILE |
102
|
6 |
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
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.