Pyinstaller-从os.system调用GDAL(gdal_translate) [英] Pyinstaller - Calling GDAL from os.system (gdal_translate)

查看:474
本文介绍了Pyinstaller-从os.system调用GDAL(gdal_translate)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候同学们。在Windows 7上运行32位Python2.7。

Greetings learned fellows. Running 32bit Python2.7 on Windows 7.

我对在pyinstaller构建中包含GDAL可执行文件有疑问。我正在进行系统调用以运行FWTools版本中的两个GDAL函数。这些函数位于Windows C:\Program Files(x86)\FWTools2.4.7\bin 上的PATH变量中,因此可以在Python27环境中正常运行。但是,此路径不会延续到pyinstaller构建中。

I'm have a question regarding including GDAL executables in a pyinstaller build. I am making a system call to run two GDAL functions from the FWTools release. These functions are in the PATH variable on windows C:\Program Files (x86)\FWTools2.4.7\bin and so it runs fine from the Python27 environment. However, this path is not carried over to the pyinstaller build.

有问题的代码正在调用GDAL函数,将图像重新转换为不同的地理空间坐标。

The code in question is calling a GDAL function to re-translate an image onto different geospatial co-ordinates.

os.system("gdal_translate -of GTiff -a_ullr  694440.7939 6403967.2406  696438.7261 6404791.6774 -a_srs EPSG:28355 site.tif siteGR.tif")

os.system("gdalinfo siteGR.tif")

运行时运行良好,直到碰到上述行,然后返回以下错误:

The runtime works well until it hits the above lines and then returns the following error:

'gdal_translate' is not recognized as an internal or external command,
operable program or batch file.

'gdalinfo' is not recognized as an internal or external command,
operable program or batch file.

我试图将gdal_translate.exe和gdalinfo.exe都作为二进制文件包含在生成文件夹中就像您为.dll所做的那样,但是由于脚本执行完后它失败了,所以我认为它不是指它们。

I have tried to include both gdal_translate.exe and gdalinfo.exe in the build folder as binaries, much like you would do for a .dll, but since it is failing after the script has got going, I don't think it is referring to them.

下面的规格文件。我可以就如何获取pyinstaller构建来使用一些建议,以识别以python脚本从系统运行的可执行文件。

I have included the spec file below. I could use some advice on how to get the pyinstaller build to recognize executables run from the system in a python script.

规范:

# -*- mode: python -*-

a = Analysis(['gis_helper.py'],
             pathex=['C:\\Users\\Hp\\PycharmProjects\\GISdev'],
             hiddenimports=['scipy.linalg.cython_blas', 'scipy.linalg.cython_lapack', 'scipy.special._ufuncs_cxx', 'ctypes.util', 'pandas.util', 'distutils.util', 'shapely', '_socket', '_proj', 'multiprocessing', '_multiprocessing', 'multiprocessing.process', 'multiprocessing.util'],
             hookspath=['C:\\Python27\\Lib\\site-packages\\PyInstaller\\hooks'], 
             runtime_hooks=None)


a.binaries1=['geos_c.dll', 'geos_c.dll', 'BINARY'],
a.binaries2=['python27.dll', 'python27.dll', 'BINARY'],
a.binaries3=['_socket.pyd', '_socket.pyd', 'BINARY'],
a.binaries4=['win32api.pyd', 'win32api.pyd', 'BINARY'],
a.binaries5=['pywintypes27.dll', 'pywintypes27.dll', 'BINARY'],
a.binaries6=['pythoncom27.dll', 'pythoncom27.dll', 'BINARY'],
a.binaries7=['_imaging.pyd', '_imaging.pyd', 'BINARY'],
a.binaries8=['_fblas.pyd', '_fblas.pyd', 'BINARY'],
a.binaries9=['gdal_translate.exe', 'gdal_translate.exe', 'BINARY'],
a.binaries10=['gdalinfo.exe', 'gdalinfo.exe', 'BINARY'],

import mpl_toolkits.basemap
import os

src_basedata = os.path.join(mpl_toolkits.basemap.__path__[0], "data")
tgt_basedata = os.path.join('mpl_toolkits', 'basemap', 'data')

pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          [('v',None,'OPTION')],
          a.binaries,  # This needs to be included
          a.binaries1,
          a.binaries2,
          a.binaries3,
          a.binaries4,
          a.binaries5,
          a.binaries6,
          a.binaries7,
          a.binaries8,
          a.binaries9,
          a.binaries10,
          a.zipfiles,
          a.datas + Tree(src_basedata, prefix=tgt_basedata),
          name='gis_helper.exe',
          debug=True,
          strip=None,
          upx=True,
          console=True )


推荐答案

在GDAL 2.1中,您可以使用<$的'librified'版本python绑定中的c $ c> gdal_translate ,它可能更健壮并防止您不得不弄乱路径:

From GDAL 2.1 you can use the 'librified' version of gdal_translate in the python bindings, which may be more robust and prevent you from having to muck about with the path:

import gdal
gdal.Translate(<options>)

正在使用它的一些示例可以在gdal存储库的测试套件中找到:
https://svn.osgeo.org/gdal/trunk/autotest/utilities/test_gdal_translate_lib.py

Some examples of it being used can be found in the test suite of the gdal repository: https://svn.osgeo.org/gdal/trunk/autotest/utilities/test_gdal_translate_lib.py

您可以 pip install gdal ,我相信它是2.1版本(在Linux上)。对于Windows,最简单的安装方法是使用 conda

You can pip install gdal and I believe it is the 2.1 release (on linux). For windows, the easiest way to install is with conda.

这应该避免您不得不在pyinstaller中过多地包含二进制文件/路径,因为它可以像对待任何python模块一样对待

This should prevent you from having to mess around including binaries/paths in pyinstaller too much as it can be treated like any python module

这篇关于Pyinstaller-从os.system调用GDAL(gdal_translate)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆