2016-11-29 7 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はバイトが暗黙的にオブジェクトを変換することはできませんが、明示的にそれを行うことができます。

関連する問題