Raster band¶
Number of bands¶
from girs.rast.raster import RasterReader
from girs.rast.proc import composite
rasters = [
'D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif',
'D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.11.tif']
r0 = RasterReader(rasters[0])
r1 = composite(*rasters)
print r0.get_band_count()
print r1.get_band_count()
Output:
1
2
Band data type¶
get_band_data_type() works in the same way as get_nodata()
from girs.rast.raster import RasterReader
from girs.rast.proc import composite
rasters = [
'D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif',
'D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.11.tif']
r0 = RasterReader(rasters[0])
r1 = composite(*rasters)
print r0.get_band_data_type()
print r0.get_band_data_type(1)
print r0.get_band_data_type([1])
print r1.get_band_data_type()
print r1.get_band_data_type(1)
print r1.get_band_data_type([1])
print r1.get_band_data_type(2)
print r1.get_band_data_type([2])
print r1.get_band_data_type([1, 2])
print r1.get_band_data_type([2, 1])
Output:
6
6
[6]
6
6
[6]
6
[6]
[6, 6]
[6, 6]
Get band¶
get_band() returns a gdal band. If the dataset goes out of scope, the system will crash. It is recommendable to not use get_band(). Instead, retrieve from the band what you want directly using girs.
The following will crash the system, because dataset goes out of scope after returning the function my_wrong_get_band():
def my_wrong_get_band():
from osgeo import gdal
dataset = gdal.Open('D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif', gdal.GA_ReadOnly)
return dataset.GetRasterBand(1)
band = my_wrong_get_band()
array = band.ReadAsArray() # crashes because dataset is deallocated (out of scope)
The following also crashes:
from osgeo import gdal
fn = 'D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif'
array = gdal.Open(fn, gdal.GA_ReadOnly).GetRasterBand(1).ReadAsArray()
Also this crashes:
band = RasterReader('D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif').get_band()
array = band.ReadAsArray()
The following works: it does not return band, but what we want from the band, namely in this case the array.
from girs.rast.raster import RasterReader
array = RasterReader('D:/tmp/girs/chirps_basin/chirps-v2.0.2016.01.10.tif').get_array()
Note
Avoid using get_band