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 | /* |
||
10 | gdaldem hillshade -of PNG public/assets/wellington-lidar-1m-dem-2013.4326.tif hillshade.png |
||
11 | */ |
||
12 | |||
13 | class Raster |
||
14 | { |
||
15 | use Configurable; |
||
16 | |||
17 | use Injectable; |
||
18 | |||
19 | private static $tile_renderer = 'raster_renderer'; |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
20 | |||
21 | protected $filename; |
||
22 | protected $info; |
||
23 | |||
24 | 3 | public function __construct($filename = null) |
|
25 | { |
||
26 | 3 | $this->filename = $filename; |
|
27 | 3 | } |
|
28 | |||
29 | 3 | public function getFilename() |
|
30 | { |
||
31 | 3 | return $this->filename ?: $this->config()->full_path; |
|
32 | } |
||
33 | |||
34 | 2 | public function getSrid() |
|
35 | { |
||
36 | 2 | if (empty($this->info['srid'])) { |
|
37 | |||
38 | 2 | $cmd = sprintf(' |
|
39 | gdalsrsinfo -o wkt %1$s', |
||
40 | 2 | $this->getFilename() |
|
41 | ); |
||
42 | |||
43 | 2 | $output = `$cmd`; |
|
44 | |||
45 | 2 | if (preg_match('/\WAUTHORITY\["EPSG","([^"]+)"\]\]$/', $output, $matches)) { |
|
46 | 2 | $this->info['srid'] = $matches[1]; |
|
47 | } |
||
48 | } |
||
49 | |||
50 | 2 | return $this->info['srid']; |
|
51 | } |
||
52 | |||
53 | 1 | public function getLocationInfo($geo = null, $band = null) |
|
54 | { |
||
55 | 1 | $cmd = sprintf(' |
|
56 | gdallocationinfo -wgs84 %1$s %2$s %3$s', |
||
57 | 1 | $band ? sprintf('-b %d', $band) : '', |
|
58 | 1 | $this->getFilename(), |
|
59 | 1 | $geo ? sprintf('%f %f', ...GIS::create($geo)->reproject(4326)->coordinates) : '' |
|
60 | ); |
||
61 | |||
62 | 1 | $output = `$cmd`; |
|
63 | |||
64 | 1 | if (preg_match_all('/\sBand\s*(\d+):\s*Value:\s*([\d\.\-]+)/', $output, $matches)) { |
|
65 | 1 | $bands = array_combine($matches[1], $matches[2]); |
|
66 | 1 | array_walk($bands, function(&$item) { |
|
67 | 1 | $item = (int)$item; |
|
68 | 1 | }); |
|
69 | 1 | return $bands; |
|
70 | } |
||
71 | } |
||
72 | |||
73 | 1 | public function translateRaster($topLeftGeo, $bottomRightGeo, $width, $height, $destFileName = '/dev/stdout') |
|
74 | { |
||
75 | 1 | $topLeftGeo = GIS::create($topLeftGeo)->coordinates; |
|
76 | 1 | $bottomRightGeo = GIS::create($bottomRightGeo)->coordinates; |
|
77 | |||
78 | 1 | $cmd = sprintf(' |
|
79 | gdal_translate -of PNG -q -projwin %1$f, %2$f, %3$f, %4$f -outsize %5$d %6$d %7$s %8$s', |
||
80 | 1 | $topLeftGeo[0], $topLeftGeo[1], |
|
81 | 1 | $bottomRightGeo[0], $bottomRightGeo[1], |
|
82 | 1 | $width, $height, |
|
83 | 1 | $this->getFilename(), |
|
84 | 1 | $destFileName |
|
85 | ); |
||
86 | |||
87 | 1 | return `$cmd`; |
|
88 | } |
||
89 | |||
90 | 1 | public function searchableFields() |
|
91 | { |
||
92 | return [ |
||
93 | 1 | 'Band' => 'Band', |
|
94 | ]; |
||
95 | } |
||
96 | } |
||
97 |