你好我是Gdal的新手,我和我的代码挣扎了。在我的代码中,一切似乎都很顺利,最后的输出带是空的。当我指定255时,无数据值设置为256,所以我真的不知道什么是错的。谢谢任何帮助将不胜感激!!!
这是我的代码
from osgeo import gdal
from osgeo import gdalconst
from osgeo import osr
from osgeo import ogr
import numpy
#graticule
src_ds = gdal.Open("E:\\NFI_photo_plot\\photoplotdownloadAllCanada\\provincial_merge\\Aggregate\\graticule1.tif")
band = src_ds.GetRasterBand(1)
band.SetNoDataValue(0)
graticule = band.ReadAsArray()
print('graticule done')
band="none"
#Biomass
dataset1 = gdal.Open("E:\\NFI_photo_plot\\photoplotdownloadAllCanada\provincial_merge\\Aggregate\\Biomass_NFI.tif")
band1 = dataset1.GetRasterBand(1)
band1.SetNoDataValue(-1)
Biomass = band1.ReadAsArray()
maskbiomass = numpy.greater(Biomass, -1).astype(int)
print("biomass done")
Biomass="none"
band1="none"
dataset1="none"
#Baseline
dataset2 = gdal.Open("E:\\NFI_photo_plot\\Baseline\\TOTBM_250.tif")
band2 = dataset2.GetRasterBand(1)
band2.SetNoDataValue(0)
baseline = band2.ReadAsArray()
maskbaseline = numpy.greater(baseline, 0).astype(int)
print('baseline done')
baseline="none"
band2="none"
dataset2="none"
#sommation
biosource=(graticule+maskbiomass+maskbaseline)
biosource1=numpy.uint8(biosource)
biosource="none"
#Écriture
dst_file="E:\\NFI_photo_plot\\photoplotdownloadAllCanada\\provincial_merge\\Aggregate\\Biosource.tif"
dst_driver = gdal.GetDriverByName('GTiff')
dst_ds = dst_driver.Create(dst_file, src_ds.RasterXSize,
src_ds.RasterYSize, 1, gdal.GDT_Byte)
#projection
dst_ds.SetProjection( src_ds.GetProjection() )
dst_ds.SetGeoTransform( src_ds.GetGeoTransform() )
outband=dst_ds.GetRasterBand(1)
outband.WriteArray(biosource1,0,0)
outband.SetNoDataValue(255)
biosource="none"
graticule="none"
答案 0 :(得分:1)
一些指示:
="none"
,则需要= None
来关闭/清理对象,否则您将对象设置为一个字符数组:n
o
n
e
,这不是您打算做的。band1.SetNoDataValue(-1)
,而其他NoData值为0?此数据源是已签名还是未签名?如果是无符号,那么-1不存在。gdal.Open
打开具有访问权限选项的栅格时,默认为gdal.GA_ReadOnly
,这意味着您之后的SetNoDataValue
来电无效。如果要修改数据集,则需要使用gdal.GA_Update
作为gdal.Open
的第二个参数。driver.CreateCopy
;有关详细信息,请参阅tutorial。