2012-10-25 78 views
3

我想寫一個Python代碼,將通過調整這個代碼導入LANDSAT衛星圖像轉換成草GIS:http://grass.osgeo.org/wiki/LANDSAT的Python:腳本導入多個陸地衛星圖像,以草GIS

LANDSAT瓦片被下載的文件夾,每個包含7個tiff圖像(波段1-7)。因此,我有一個包含幾個子目錄的目錄(每個LANDSAT磁貼一個)。

我目前的代碼如下:

#!/usr/bin/python 

import os 
import sys 
import glob 
import grass.script as grass 

def import_tifs(dirpath): 

    for dirpath, dirname, filenames in os.walk(dirpath): 
     for dirname in dirpath: 

     dirname = os.path.join(dirpath,dirname) 

      for file in os.listdir(dirname): 
       if os.path.splitext(file)[-1] != '.TIF': 
        continue 
       ffile = os.path.join(dirname, file) 
       name = os.path.splitext(file)[0].split(dirname)[-1] 

       grass.message('Importing %s -> %[email protected]%s...' % (file, name, dirpath)) 

       grass.run_command('r.in.gdal', 
            flags = 'o', 
            input = ffile, 
            output = name, 
            quiet = True, 
            overwrite = True) 

def main():         
    if len(sys.argv) == 1: 
     for directory in filter(os.path.isdir, os.listdir(os.getcwd())): 
      import_tifs(directory) 
    else: 
     import_tifs(sys.argv[1]) 

if __name__ == "__main__": 
    main() 

,我發現了以下錯誤:

Traceback (most recent call last): 
    File "C:/Users/Simon/Documents/import_landsat2.py", line 
40, in <module> 
    main() 
    File "C:/Users/Simon/Documents/import_landsat2.py", line 
37, in main 
    import_tifs(sys.argv[1]) 
    File "C:/Users/Simon/Documents/import_landsat2.py", line 
17, in import_tifs 
    for file in os.listdir(dirname): 
WindowsError: [Error 3] The system cannot find the path 
specified: 'dirpath\\C/*.*' 

誰能解釋發生什麼事,我需要做些什麼來解決它,或建議替代?謝謝。

回答

2

我相信你的主要問題是dirnameos.walk()返回一個列表(而不是一個字符串),所以你的後續字符串(即dirname = os.path.join(dirpath,dirname))有點畸形。這裏有一個可能的選擇 - 爲了測試這個,我使用了sys.argv[1]這個目錄的完整路徑,但是你可以使它更加動態以適應你的情況。此外,避免使用變量名稱,例如file,因爲它們是Python關鍵字。我無法測試你的grass.*函數,但希望這將是一個足夠清晰的例子,以便你可以調整你的需要。 os.walk()本地處理大量的標準解析,這樣可以去掉一些目錄操作功能:

def import_tifs(dirpath): 
    for dirpath, dirname, filenames in os.walk(dirpath): 
    # Iterate through the files in the current dir returned by walk() 
    for tif_file in filenames: 
     # If the suffix is '.TIF', process 
     if tif_file.upper().endswith('.tif'): 
     # This will contain the full path to your file 
     full_path = os.path.join(dirpath, tif_file) 

     # tif_file will already contain the name, so you can call from here 
     grass.message('Importing %s -> %[email protected]%s...' % (full_path, tif_file, dirpath)) 

     grass.run_command('r.in.gdal', 
          flags = 'o', 
          input = full_path, 
          output = tif_file, 
          quiet = True, 
          overwrite = True) 
+0

大 - 感謝您抽出寶貴回覆的時間。我使用了自己和@jonhkr重寫的代碼,並讓我的代碼正常工作,並希望在同一時間更高效。 –

+0

@ user1774932太棒了!祝你一切順利。 – RocketDonkey

2

我只是改寫你的代碼列出所有的目錄樹,並找到一個文件擴展名,在這種情況下「 .TIF」,

#!/usr/bin/python 

import os 
import sys 


def import_tifs(dirpath): 

    for dirpath, dirname, filenames in os.walk(dirpath): 
     for filename in filenames: 
      name, extension = os.path.splitext(filename) 
      if extension.lower() == ".tif": 
       filepath = os.path.join(dirpath, filename) 

       print(filepath, name, dirpath) 


def main(): 
    if len(sys.argv) == 1: 
      import_tifs(os.getcwd()) 
    else: 
     import_tifs(sys.argv[1]) 


if __name__ == "__main__": 
    main() 

請檢查如果這是你在找什麼...