尽管几年前我学过Python(2.7),但我现在才开始再次使用它。 我正在使用Python 3.4.4,并尝试使用GDAL包将栅格读取为数组。 我按照此处列出的步骤进行操作:https://sandbox.idre.ucla.edu/sandbox/tutorials/installing-gdal-for-windows,但对于Python 3.4,我使用了来自这里的GDAL二进制文件:http://www.gisinternals.com/release.php。
在测试基本功能时,我尝试读取一个tiff文件,如下所示。
在测试基本功能时,我尝试读取一个tiff文件,如下所示。
import gdal as gdal
import numpy as np
import ogr
import osr
import os
import sys
e=('error has occurred')
# this allows GDAL to throw Python Exceptions
gdal.UseExceptions()
# open dataset
test = ('LE70130312004049EDC01_sr_adjacent_cloud_qa.tif')
print("file exists")
# getting metadata
gtif = gdal.Open(test)
print (gtif.GetMetadata())
print("metadata printed")
try:
src_ds = gdal.Open(test)
print("gdal.open success")
except (RuntimeError):
print ('Unable to open INPUT.tif')
print(e)
try:
srcband = src_ds.GetRasterBand(1)
print("get raster band 1 success")
except (RuntimeError):
# for example, try GetRasterBand(10)
print ('Band ( %i ) not found') % band_num
print (e)
try:
rasArray=np.array(src_ds.ReadAsArray())
print("read as array")
except (RuntimeError):
print (e)
当我运行最后一个代码块时,使用"rasArray=np.array(src_ds.ReadAsArray())"命令,会返回以下错误代码:
Traceback (most recent call last):
File "C:\Python34\lib\site-packages\osgeo\gdal_array.py", line 16, in swig_import_helper
fp, pathname, description = imp.find_module('_gdal_array', [dirname(__file__)])
File "C:\Python34\lib\imp.py", line 297, in find_module
raise ImportError(_ERR_MSG.format(name), name=name)
ImportError: No module named '_gdal_array'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\dem12002\Desktop\test.py", line 38, in <module>
rasArray=np.array(src_ds.ReadAsArray())
File "C:\Python34\lib\site-packages\osgeo\gdal.py", line 1829, in ReadAsArray
from . import gdalnumeric
File "C:\Python34\lib\site-packages\osgeo\gdalnumeric.py", line 1, in <module>
from osgeo.gdal_array import *
File "C:\Python34\lib\site-packages\osgeo\gdal_array.py", line 26, in <module>
_gdal_array = swig_import_helper()
File "C:\Python34\lib\site-packages\osgeo\gdal_array.py", line 18, in swig_import_helper
import _gdal_array
ImportError: No module named '_gdal_array'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\dem12002\Desktop\test.py", line 40, in <module>
except (RuntimeError,e):
NameError: name 'e' is not defined
这个问题是由于我安装GDAL或Python的方式引起的吗? 我应该切换到Python 2.7吗? 我看到很多关于“没有模块命名”的问题,但是没有太多涉及像GDAL这样的问题。
更新: 我只需重新安装我下载的msi中的GDAL,它就自动修复了。谁知道这么简单。谢谢!
_gdal_array.dll
文件,你可以尝试将它重命名为_gdal_array.pyd
。很抱歉我无法告诉你更多的信息,因为我不熟悉这个模块。我很久以前在 Linux 上使用过 Swig。 - Gribouillis