我想寫一個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/*.*'
誰能解釋發生什麼事,我需要做些什麼來解決它,或建議替代?謝謝。
大 - 感謝您抽出寶貴回覆的時間。我使用了自己和@jonhkr重寫的代碼,並讓我的代碼正常工作,並希望在同一時間更高效。 –
@ user1774932太棒了!祝你一切順利。 – RocketDonkey