Test Failed
Push — master ( 7b011d...e1b672 )
by Andreas
05:38
created

Raster::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 1
cp 0
crap 2
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 Smindel\GIS\GIS;
8
9
class Raster
10
{
11
    use Configurable;
12
13
    use Injectable;
14
15
    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...
16
17
    protected $filename;
18
    protected $info;
19
20
    public function __construct($filename = null)
21
    {
22
        $this->filename = $filename;
23
    }
24
25
    public function getFilename()
26
    {
27
        return $this->filename ?: $this->config()->full_path;
28
    }
29
30
    public function getSrid()
31
    {
32
        if (empty($this->info['srid'])) {
33
34
            $cmd = sprintf('
35
                gdalsrsinfo -o wkt %1$s',
36
                $this->getFilename()
37
            );
38
39
            $output = `$cmd`;
40
41
            if (preg_match('/\WAUTHORITY\["EPSG","([^"]+)"\]\]$/', $output, $matches)) {
42
                $this->info['srid'] = $matches[1];
43
            }
44
        }
45
46
        return $this->info['srid'];
47
    }
48
49
    public function getLocationInfo($geo = null, $band = null)
50
    {
51
        $cmd = sprintf('
52
            gdallocationinfo -wgs84 %1$s %2$s %3$s',
53
            $band ? sprintf('-b %d', $band) : '',
54
            $this->getFilename(),
55
            $geo ? sprintf('%f %f', ...GIS::reproject(GIS::to_array($geo), 4326)['coordinates']) : ''
56
        );
57
58
        $output = `$cmd`;
59
60
        if (preg_match_all('/\sBand\s*(\d+):\s*Value:\s*([\d\.\-]+)/', $output, $matches)) {
61
            $bands = array_combine($matches[1], $matches[2]);
62
            array_walk($bands, function(&$item){
0 ignored issues
show
Bug introduced by
It seems like $bands can also be of type false; however, parameter $array of array_walk() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
            array_walk(/** @scrutinizer ignore-type */ $bands, function(&$item){
Loading history...
63
                $item = (int)$item;
64
            });
65
            return $bands;
66
        }
67
    }
68
69
    public function translateRaster($topLeftGeo, $bottomRightGeo, $width, $height, $destFileName = '/dev/stdout')
70
    {
71
        $topLeftGeo = GIS::to_array($topLeftGeo);
72
        $bottomRightGeo = GIS::to_array($bottomRightGeo);
73
74
        $cmd = sprintf('
75
            gdal_translate -of PNG -q -projwin %1$f, %2$f, %3$f, %4$f -outsize %5$d %6$d %7$s %8$s',
76
            $topLeftGeo['coordinates'][0], $topLeftGeo['coordinates'][1],
77
            $bottomRightGeo['coordinates'][0], $bottomRightGeo['coordinates'][1],
78
            $width, $height,
79
            $this->getFilename(),
80
            $destFileName
81
        );
82
83
        return `$cmd`;
84
    }
85
86
    public function searchableFields()
87
    {
88
        return [
89
            'Band' => 'Band',
90
        ];
91
    }
92
}
93