Completed
Branch feature/SRTM1ArcSecond (5f684d)
by Hannes
04:35
created

Reader   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 1
cbo 2
dl 0
loc 103
ccs 0
cts 55
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A addProvider() 0 4 1
A numberOfProviders() 0 4 1
A hasProviders() 0 4 1
A hasDataFor() 0 10 3
A getElevations() 0 12 3
A getBoundsFor() 0 21 3
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;
13
14
use Runalyze\DEM\Exception\RuntimeException;
15
use Runalyze\DEM\Provider\ProviderInterface;
16
17
class Reader implements ReaderInterface
18
{
19
    /** @var ProviderInterface[] */
20
    protected $Provider = [];
21
22
    /** @var bool */
23
    protected $InvalidFlag = false;
24
25
    /**
26
     * @param ProviderInterface|null $provider
27
     */
28
    public function __construct(ProviderInterface $provider = null)
29
    {
30
        if (null !== $provider) {
31
            $this->addProvider($provider);
32
        }
33
    }
34
35
    /**
36
     * @param ProviderInterface $provider
37
     */
38
    public function addProvider(ProviderInterface $provider)
39
    {
40
        $this->Provider[] = $provider;
41
    }
42
43
    /**
44
     * @return int
45
     */
46
    public function numberOfProviders()
47
    {
48
        return count($this->Provider);
49
    }
50
51
    /**
52
     * @return bool
53
     */
54
    public function hasProviders()
55
    {
56
        return !empty($this->Provider);
57
    }
58
59
    /**
60
     * @param  array $latitudeLongitudes array(array($lat, $lng), ...)
61
     * @return bool
62
     */
63
    public function hasDataFor(array $latitudeLongitudes)
64
    {
65
        foreach ($this->Provider as $provider) {
66
            if ($provider->hasDataFor($latitudeLongitudes)) {
67
                return true;
68
            }
69
        }
70
71
        return false;
72
    }
73
74
    /**
75
     * @param  array            $latitudes
76
     * @param  array            $longitudes
77
     * @return array            elevations [m] can be false if nothing retrieved
78
     * @throws RuntimeException
79
     */
80
    public function getElevations(array $latitudes, array $longitudes)
81
    {
82
        $boundaries = $this->getBoundsFor($latitudes, $longitudes);
83
84
        foreach ($this->Provider as $provider) {
85
            if ($provider->hasDataFor($boundaries)) {
86
                return $provider->getElevations($latitudes, $longitudes);
87
            }
88
        }
89
90
        throw new RuntimeException('No provider can handle the given locations.');
91
    }
92
93
    /**
94
     * @param  float[] $latitudes
95
     * @param  float[] $longitudes
96
     * @return array   array(array($lat, $lng), ...)
97
     */
98
    protected function getBoundsFor(array $latitudes, array $longitudes)
99
    {
100
        $filteredLatitudes = array_filter($latitudes);
101
        $filteredLongitudes = array_filter($longitudes);
102
103
        if (empty($filteredLatitudes) || empty($filteredLongitudes)) {
104
            return [];
105
        }
106
107
        $minLatitude = min($filteredLatitudes);
108
        $maxLatitude = max($filteredLatitudes);
109
        $minLongitude = min($filteredLongitudes);
110
        $maxLongitude = max($filteredLongitudes);
111
112
        return [
113
            [$minLatitude, $minLongitude],
114
            [$minLatitude, $maxLongitude],
115
            [$maxLatitude, $minLongitude],
116
            [$maxLatitude, $maxLongitude],
117
        ];
118
    }
119
}
120