Monday 9 January 2017

python - In gdal how to save 3 dimensional array as stacked raster image?


I have constructed a 3-dimensional numpy array by stacking multiple 2-dimentional rasters.


>>> print arr_list_stack.shape
(30L, 370L, 400L)

As the arr_list_stack.shape shows that this 3-dimensional numpy array has 30 bands, by using gdal I want to save this array as raster image which has all the 30 bands.



I have a method which can save 2-dimentional arrays


def CreateGeoTiff(Name, Array, driver, NDV, 
xsize, ysize, GeoT, Projection, DataType):
Array[np.isnan(Array)] = NDV
DataSet = driver.Create(NewFileName, xsize, ysize, 1, DataType)
DataSet.SetGeoTransform(GeoT)
DataSet.SetProjection( Projection.ExportToWkt() )
DataSet.GetRasterBand(1).WriteArray( Array )
DataSet.GetRasterBand(1).SetNoDataValue(NDV)
return NewFileName


but CreateGeoTiff() fails to save 3-dimentional arrays and returns the error


  File "C:\Users\Documents\Quadium\SPI\Code\testing_stack_arr.py", line 39, in CreateGeoTiff
DataSet.GetRasterBand(1).WriteArray( Array )
File "C:\PROGRA~1\QGISWI~1\apps\Python27\lib\site-packages\osgeo\gdal.py", line 1235, in WriteArray
callback_data = callback_data )
File "C:\PROGRA~1\QGISWI~1\apps\Python27\lib\site-packages\osgeo\gdal_array.py", line 359, in BandWriteArray
raise ValueError("expected array of dim 2")

What changes should I make in CreateGeoTiff() method that will enable me to save 3-dimentional array.





No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...