Me gustaría saber cómo exportar una imagen GeoTiff con n bandas en Python.
Tengo una función pero sólo funciona con una banda rasterizada.
def save_raster ( output_name, dataset, raster_data, driver ,NaN_Value,arr_projection=None):
if arr_projection is None:
arr_projection=[]
else:
if str(type(arr_projection)) == "<class 'osgeo.gdal.Dataset'>":
srs=arr_projection.GetProjectionRef ()
else:
srs = arr_projection
"""
A function to save a 1-band raster using GDAL to the file indicated
by ``output_name``. It requires a GDAL-accesible dataset to collect
the projection and geotransform.
"""
# Open the reference dataset
g = ( dataset )
# Get the Geotransform vector
if raster_data is False:
raster_data=g.ReadAsArray()
if type(raster_data) == tuple:
raster_data = np.array(raster_data[0])
if str(type(g)) == "<class 'osgeo.gdal.Dataset'>":
geo_transform = g.GetGeoTransform ()
x_size = g.RasterXSize # Raster xsize
y_size = g.RasterYSize # Raster ysize
srs = g.GetProjectionRef () # Projection
elif str(type(g)) == "<class 'affine.Affine'>":
geo_transform = (g[2],g[0],g[1],g[5],g[3],g[4])
RastArr = raster_data
x_size = int(RastArr.shape[1])
y_size = int(RastArr.shape[0])
#PROCESS RASTERIO NUMPY
else:
geo_transform = (g[1][2],g[1][0],g[1][1],g[1][5],g[1][3],g[1][4])
RastArr = np.array(g[0])
x_size = int(RastArr.shape[2])
y_size = int(RastArr.shape[1])
if raster_data.ndim > 2:
raster_data=raster_data[0]
NaN_rast = NaN_Value
# raster_data[raster_data == NaN_rast] = 'NaN'
raster_data[raster_data == NaN_rast] = np.NaN
# Need a driver object. By default, we use GeoTIFF
driver = gdal.GetDriverByName ( driver )
dataset_out = driver.Create ( output_name, x_size, y_size, 1, \
gdal.GDT_Float32 )
dataset_out.SetGeoTransform ( geo_transform )
dataset_out.SetProjection ( srs )
dataset_out.GetRasterBand ( 1 ).WriteArray ( \
raster_data.astype(np.float32) )
¿Algún consejo?