Despite learning python (2.7) several years ago, I am just starting to use it again. I am using python 3.4.4 and attempting to use the GDAL package to read a raster as an array. I followed the steps outlined here: https://sandbox.idre.ucla.edu/sandbox/tutorials/installing-gdal-for-windows except for Python 3.4 and used the GDAL binaries from here: http://www.gisinternals.com/release.php
While testing basic functionality, I attempted to read a tiff file as shown
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)
And when I run the last block with "rasArray=np.array(src_ds.ReadAsArray())" I get the following error code returned to me:
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
Is this issue with how I installed GDAL or Python? Should I switch to Python 2.7? I've seen many questions about "no module named", however not many involving GDAL issue like so.
Update: I simply went to reinstall GDAL from the msi I had downloaded and it automatically repaired it for me. Who would've know it was that easy. Thanks!
I came accross this problem too,here is how I solved it :
pip3 uninstall gdal
pip3 install numpy
pip3 install gdal
I am using MAC OS BTW
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With