2016-11-29 38 views
0

我正在處理一個我沒有編寫的NDVI腳本,我試圖讓它運行。它遇到的問題是舊的類型錯誤:無法將字節對象隱式轉換爲str。直到昨天,我從來沒有見過struct包,經過相當多的研究,我一般都瞭解這個概念,但不完全具體。GTiff文件的打包和拆包結構

我正在處理tiff文件並嘗試創建NDVI圖像。這是代碼示例。如果有人能幫我理解這一點,並幫助我解決問題,我將不勝感激!

## The function which loops through the input image and 
## calculates the output NDVI value to be outputted. 
def calcNDVI(self, filePath, outFilePath): 

    # Open the inputted dataset 
    dataset = gdal.Open(filePath, gdal.GA_ReadOnly) 

    # Check the dataset successfully opened 
    if dataset is None: 
     print("The dataset could not be opened") 
     sys.exit(-1) 

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

    # Get hold of the RED and NIR image bands from the image 
    # Note that the image bands are hard coded to handle 
    # landsat 8 TIFFs that produce separate images per band 
    # which are mosaiced together where 
    # Red = band 1 and NIR = band 2 
    red_band = dataset.GetRasterBand(1) 
    nir_band = dataset.GetRasterBand(2) 

    # Retrieve the number of lines within the image 
    numLines = red_band.YSize 

    # Loop through each line in turn: 
    for line in range(numLines): 
     # Define variable for output line 
     outputLine = '' 

     # Read in data for the current line from the 
     # image band representing the red wavelength 
     red_scanline = red_band.ReadRaster(0, line, red_band.XSize, 1, \ 
       red_band.XSize, 1, gdal.GDT_Float32) 
     # Unpack the line of data to be read as floating point data 
     red_tuple = struct.unpack('f' * red_band.XSize, red_scanline) 

     # Read in data for the current line from the 
     # image band representing the NIR wavelength 
     nir_scanline = nir_band.ReadRaster(0, line, nir_band.XSize, 1, \ 
       nir_band.XSize, 1, gdal.GDT_Float32) 
     # Unpack the line of data to be read as floating point data 
     nir_tuple = struct.unpack('f' * nir_band.XSize, nir_scanline) 

     # Loop through the columns within the image 
     for i in range(len(red_tuple)): 
      # Calculate the NDVI for the current pixel 
      ndvi_lower = (nir_tuple[i] + red_tuple[i]) 
      ndvi_upper = (nir_tuple[i] - red_tuple[i]) 
      ndvi = 0 
      # Be careful of zero divide 
      if ndvi_lower == 0: 
       ndvi = 0 
      else: 
       ndvi = ndvi_upper/ndvi_lower 

      # Add the current pixel to the output line 
      outputLine = outputLine + struct.pack('f', ndvi) 

      # Write the completed line to the output image 
      outDataset.GetRasterBand(1).WriteRaster(0, line, red_band.XSize, 1, \ 
      outputLine, buf_xsize=red_band.XSize, buf_ysize=1, buf_type = gdal.GDT_Float32) 

      # Delete the output line following write 
      del outputLine 

奔着當輸出:

outputLine = outputLine + struct.pack('f', ndvi) 

TypeError: Can't convert 'bytes' object to str implicitly 

同樣,任何幫助是非常感謝!另外,在這個網站和其他關於這個特定結構錯誤的地方有幾個問題,但沒有專門處理圖像的問題。

謝謝!

回答

0

好吧我想出了答案,如果任何人有類似的問題,這保持了!這是造成錯誤的輸出線:

outputLine = outputLine + struct.pack('f', ndvi) 

可以固定一個非常簡單的解決方案:

outputLine = outputLine + str(struct.pack('f', ndvi)) 

Python不能轉換成字節隱含對象,但你可以明確地做到這一點。