调用gdal_merge进入python脚本


10

我是python的新手,并且对如何在(spyder)python脚本中使用gdal_merge不屑一顾。我使用Windows 10,Python 3.6,并从osgeo4w安装了gdal工具。我意识到还有很多其他文章描述了这个问题,但是没有一个可以帮助我解决这个问题。

当我从cmd调用gdal模块时,它的工作原理很像:

 python "C:\OSGeo4W64\bin\gdal_merge.py" -o merged.tif input1.tif input2.tif

但是,我无法使其在python脚本(spyder)中正常工作。

第一种方法产生一个输出,但是名称不正确:它产生一个“ out.tif”文件,而不是我要求的“ merged.tif”文件:

import sys
sys.path.append('C:\\OSGeo4W64\\bin')
import gdal_merge as gm
gm.main(['-o', 'merged.tif', 'N00W078.tif', 'N00W079.tif'])

第二种方法只是不产生任何输出:

import subprocess
merge_command = ["python", "C:\OSGeo4W64\bin\gdal_merge.py", "-o", "merged.tif", "input1.tif", "input2.tif"]
subprocess.call(merge_command,shell=True)

关于如何解决此问题有什么想法?

Answers:


12

在第一种方法中添加一个空参数(因为gdal_merge.py解析从1而不是0开始的参数):

import sys
sys.path.append('C:\\OSGeo4W64\\bin')
import gdal_merge as gm
gm.main(['', '-o', 'merged.tif', 'N00W078.tif', 'N00W079.tif'])

gdal_merge.py第二种方法中加入之路:

import os, subprocess
gm = os.path.join('C:\\','OSGeo4W64','bin','gdal_merge.py')
merge_command = ["python", gm, "-o", "merged.tif", "input1.tif", "input2.tif"]
subprocess.call(merge_command,shell=True)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.