2017-01-14 7 views
0

gdalで新しくなった。私はNDVIを計算しようとしています。しかし、私はいくつかの問題があるので、いくつかの簡単なテストを試みますが、 "dataset.GetRasterBand(1).WriteRaster(...)"が機能しません、この結果空の画像WriteRasterが機能しない

私のコードは:

#A function to create the output image 
def createOutputImage(self, outFilename, rb_ds): 
    # Define the image driver to be used 
    # This defines the output file format TIF 
    driver = gdal.GetDriverByName("Gtiff") 

    # Check that this driver can create a new file. 
    metadata = driver.GetMetadata() 
    if metadata.has_key(gdal.DCAP_CREATE) and metadata[gdal.DCAP_CREATE] == 'YES': 
     print 'Driver GTiff supports Create() method.' 
    else: 
     print 'Driver GTIFF does not support Create()' 
     sys.exit(-1) 

    # Get the spatial information from the input file 
    geoTransform = rb_ds.GetGeoTransform() 
    geoProjection = rb_ds.GetProjection() 
    # Create an output file of the same size as the inputted 
    # image, but with only 1 output image band. 
    newDataset = driver.Create(outFilename, rb_ds.RasterXSize, \ 
           rb_ds.RasterYSize, 1, gdal.GDT_Float32) 


    # Define the spatial information for the new image. 

    newDataset.SetGeoTransform(geoTransform) 
    newDataset.SetProjection(geoProjection) 

    return newDataset 


# The function which loops through the input image and 
# calculates the output NDVI value to be outputted.  
def calcNDVI(self, red_band, NIR_band, outFilePath): 
    # Open the red band dataset 
    red_band_dataset = gdal.Open(red_band, gdal.GA_ReadOnly) 
    nir_band_dataset = gdal.Open(NIR_band, gdal.GA_ReadOnly) 

    # Check the dataset was successfully opened 
    if (red_band_dataset is None) or (nir_band_dataset is None): 
     print "The datasets could not openned" 
     sys.exit(-1) 

    # Create the output dataset 
    outDataset = self.createOutputImage(outFilePath, red_band_dataset) 
    # Check the datasets was successfully created. 
    if outDataset is None: 
     print 'Could not create output image' 
     sys.exit(-1) 

    # Load de two band 
    red = red_band_dataset.GetRasterBand(1) 

    for line in range(red.YSize): 
     # read a line 
     red_scanline = red.ReadRaster(0, line, red.XSize, 1, red.XSize, 1, gdal.GDT_Float32) 

     outDataset.GetRasterBand(1).WriteRaster(0,line,red.XSize, 1, red_scanline, buf_xsize=red.XSize,buf_ysize=1, buf_type=gdal.GDT_Float32) 


     del outputLine 

    print "NDVI calculate correct" 

ちょうど私がoutputDatasetに赤(画像)から行の行をコピーしていますが、私は、このリソース使用しています

空のイメージがあります:http://learningzone.rspsoc.org.uk/index.php/Learning-Materials/Python-Scripting/9.4-Calculate-NDVI-using-GDAL

感謝を

答えて

0

del outputLineにしようとすると、このプログラムがエラーをスローするように出力を提供しないと(「NDVI計算が正しいか」という印字をしますか?)、outputLineが宣言されていません。このクラッシュにより、作成された1行の画像が作成されますが、それ以外の場合は空白になります。

+0

こんにちは、申し訳ありませんが、完全なコードではありません。私はそれを単純な質問をするために減らしました。コードプリントNDVIはエラーなしで正確に計算されますが、ブランクのimg –

関連する問題