Passed
Push — master ( 485b67...7b011d )
by Andreas
05:28
created

Raster::getValue()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 2
nop 2
dl 0
loc 12
ccs 0
cts 7
cp 0
crap 20
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Smindel\GIS\Model;
4
5
use SilverStripe\Core\Config\Configurable;
6
use SilverStripe\Core\Injector\Injectable;
7
use SilverStripe\ORM\DB;
8
use Smindel\GIS\GIS;
9
10
class Raster
11
{
12
    use Configurable;
13
14
    use Injectable;
15
16
    private static $tile_renderer = 'raster_renderer';
0 ignored issues
show
introduced by
The private property $tile_renderer is not used, and could be removed.
Loading history...
17
18
    protected $info;
19
20
    public function searchableFields()
21
    {
22
        return [
23
            'Band' => 'Band',
24
        ];
25
    }
26
27
    public function getFullPath()
28
    {
29
        return static::config()->full_path;
30
    }
31
32
    public function getSrid()
33
    {
34
        if (empty($this->info['srid'])) {
35
36
            $cmd = sprintf('
37
                gdalsrsinfo -o wkt %1$s',
38
                $this->getFullPath()
39
            );
40
41
            $output = `$cmd`;
42
43
            if (preg_match('/\WAUTHORITY\["EPSG","([^"]+)"\]\]$/', $output, $matches)) {
44
                $this->info['srid'] = $matches[1];
45
            }
46
        }
47
48
        return $this->info['srid'];
49
    }
50
51
    public function getValue($geo = null, $band = null)
52
    {
53
        $cmd = sprintf('
54
            gdallocationinfo -wgs84 %1$s %2$s %3$s',
55
            $band ? sprintf('-b %d', $band) : '',
56
            $this->getFullPath(),
57
            $geo ? sprintf('%f %f', ...GIS::reproject(GIS::to_array($geo), 4326)['coordinates']) : ''
58
        );
59
60
        $output = `$cmd`;
61
62
        if (preg_match_all('/\sBand\s*(\d+):\s*Value:\s*([\d\.\-]+)/', $output, $matches)) return array_combine($matches[1], $matches[2]);
63
    }
64
}
65