Answers:
您需要gdal.band.WriteArray方法。GDAL API教程中有一个示例(如下所示):
format = "GTiff"
driver = gdal.GetDriverByName( format )
dst_ds = driver.Create( dst_filename, 512, 512, 1, gdal.GDT_Byte )
dst_ds.SetGeoTransform( [ 444720, 30, 0, 3751320, 0, -30 ] )
srs = osr.SpatialReference()
srs.SetUTM( 11, 1 )
srs.SetWellKnownGeogCS( 'NAD27' )
dst_ds.SetProjection( srs.ExportToWkt() )
raster = numpy.zeros( (512, 512), dtype=numpy.uint8 )
dst_ds.GetRasterBand(1).WriteArray( raster )
# Once we're done, close properly the dataset
dst_ds = None
要生成随机数据,请查看numpy.random模块。
这是一个更完整的工作示例:
from osgeo import gdal, osr
import numpy
dst_filename = '/tmp/test.tif'
#output to special GDAL "in memory" (/vsimem) path just for testing
#dst_filename = '/vsimem/test.tif'
#Raster size
nrows=1024
ncols=512
nbands=7
#min & max random values of the output raster
zmin=0
zmax=12345
## See http://gdal.org/python/osgeo.gdal_array-module.html#codes
## for mapping between gdal and numpy data types
gdal_datatype = gdal.GDT_UInt16
np_datatype = numpy.uint16
driver = gdal.GetDriverByName( "GTiff" )
dst_ds = driver.Create( dst_filename, ncols, nrows, nbands, gdal_datatype )
## These are only required if you wish to georeference (http://en.wikipedia.org/wiki/Georeference)
## your output geotiff, you need to know what values to input, don't just use the ones below
#Coordinates of the upper left corner of the image
#in same units as spatial reference
#xmin=147.2
#ymax=-34.54
#Cellsize in same units as spatial reference
#cellsize=0.01
#dst_ds.SetGeoTransform( [ xmin, cellsize, 0, ymax, 0, -cellsize ] )
#srs = osr.SpatialReference()
#srs.SetWellKnownGeogCS("WGS84")
#dst_ds.SetProjection( srs.ExportToWkt() )
raster = numpy.random.randint(zmin,zmax, (nbands, nrows, ncols)).astype(np_datatype )
for band in range(nbands):
dst_ds.GetRasterBand(band+1).WriteArray( raster[band, :, :] )
# Once we're done, close properly the dataset
dst_ds = None
我知道这不是您要的,但是如果您想要的只是多光谱或高光谱样本数据-Opticks项目的此测试数据可能会起作用。或者,您可以直接从Earth Explorer获取LANDSAT数据。
该站点的示例代码将2D numpy数组转换为单波段geoTIFF,将多波段geoTIFF转换为3D numpy数组。
编辑:
进一步的研究发现了一页示例代码,其中包含“缺少示例”,3D numpy数组->多频带geoTIFF。