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'; |
|
|
|
|
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){ |
|
|
|
|
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
|
|
|
|