Completed
Push — master ( 7f5809...3e2944 )
by Andreas
07:09
created

Raster::ST_SummaryStats()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 2
dl 0
loc 18
ccs 0
cts 10
cp 0
crap 6
rs 9.9666
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 $tableName;
19
20
    protected $rasterColumn;
21
22
    protected $srid = 4326;
23
24
    protected $dimensions;
25
26
    protected $colorMap;
27
28
    public function searchableFields()
29
    {
30
        return [
31
            'Band' => 'Band',
32
        ];
33
    }
34
35
    public function getTableName()
36
    {
37
        return $this->tableName;
38
    }
39
40
    public function getRasterColumn()
41
    {
42
        return $this->rasterColumn;
43
    }
44
45
    public function getSrid()
46
    {
47
        return $this->srid;
48
    }
49
50
    public function getDimensions()
51
    {
52
        return $this->dimensions;
53
    }
54
55
    public function getColorMap()
56
    {
57
        return $this->colorMap;
58
    }
59
60
    public function ST_SRID()
61
    {
62
        $sql = sprintf('
63
            SELECT
64
                ST_SRID(%2$s) srid
65
            FROM %1$s
66
            LIMIT 1',
67
            $this->tableName,
68
            $this->rasterColumn
69
        );
70
71
        return DB::query($sql)->value();
72
    }
73
74
    public function ST_SummaryStats($geo = null, $band = 1)
75
    {
76
        $sql = sprintf('
77
            SELECT
78
                (ST_SummaryStats(%2$s, %3$d)).*
79
            FROM %1$s',
80
            $this->tableName,
81
            $this->rasterColumn,
82
            $band
83
        );
84
85
        $sql .= $geo ? sprintf('
86
            WHERE ST_Intersects(%1$s, ST_GeomFromText(\'%2$s\', %3$d))',
87
            $this->rasterColumn,
88
            ...GIS::split_ewkt(GIS::array_to_ewkt($geo))
89
        ) : '';
90
91
        return DB::query($sql)->first();
92
    }
93
94
    public function ST_Value($geo, $band = 1)
95
    {
96
        $split = GIS::split_ewkt(GIS::array_to_ewkt($geo));
97
98
        $sql = sprintf('
99
            SELECT
100
                ST_Value(
101
                    %2$s,
102
                    %3$d,
103
                    ST_GeomFromText(
104
                        \'%4$s\',
105
                        %5$d
106
                    )
107
                )
108
            FROM %1$s
109
            WHERE
110
                ST_Intersects(
111
                    %2$s,
112
                    ST_GeomFromText(
113
                        \'%4$s\',
114
                        %5$d
115
                    )
116
                )
117
            ',
118
            $this->tableName,
119
            $this->rasterColumn,
120
            $band,
121
            $split[0],$split[1]
122
        );
123
124
        return DB::query($sql)->value();
125
    }
126
}
127