4

I am using python 2.7 and have gdal installed. I want to read an entire .jpg image at once and so I used statement

import gdal
from gdalconst import *
driver = gdal.GetDriverByName('JPEG')
driver.Register()
dataset = gdal.Open('1.jpg', GA_ReadOnly)
cols = dataset.RasterXSize
rows = dataset.RasterYSize
bands = dataset.RasterCount
geotransform = dataset.GetGeoTransform()
originX = geotransform[0]
originY = geotransform[3]
pixelWidth = geotransform[1]
pixelHeight = geotransform[5]
band = dataset.GetRasterBand(1)
data = band.ReadAsArray(0, 0, cols, rows)

but I am getting an import error for gdal_array.

ImportError                               Traceback (most recent call last)
<ipython-input-7-6a16047a395b> in <module>()
----> 1 data = band.ReadAsArray(0, 0, cols, rows)

C:\Users\DELL\Anaconda\lib\site-packages\osgeo\gdal.pyc in ReadAsArray(self, xoff,        yoff, win_xsize, win_ysize, buf_xsize, buf_ysize, buf_obj)
1174     def ReadAsArray(self, xoff=0, yoff=0, win_xsize=None, win_ysize=None,
1175                     buf_xsize=None, buf_ysize=None, buf_obj=None):
-> 1176         import gdalnumeric
1177 
1178         return gdalnumeric.BandReadAsArray( self, xoff, yoff,

C:\Users\DELL\Anaconda\lib\site-packages\osgeo\gdalnumeric.py in <module>()
----> 1 from gdal_array import *
2 from numpy import *

C:\Users\DELL\Anaconda\lib\site-packages\osgeo\gdal_array.py in <module>()
 24                 fp.close()
 25             return _mod
 ---> 26     _gdal_array = swig_import_helper()
 27     del swig_import_helper
 28 else:

 C:\Users\DELL\Anaconda\lib\site-packages\osgeo\gdal_array.py in swig_import_helper()
 16             fp, pathname, description = imp.find_module('_gdal_array', [dirname(__file__)])
 17         except ImportError:
 ---> 18             import _gdal_array
 19             return _gdal_array
 20         if fp is not None:

 ImportError: No module named _gdal_array

Please tell me what could possible be going wrong. I am new to using GDAL.

  • I meet the same problem when using python 3.4 I am puzzled that when I changed to X86(32bit) version ,then it could run. expect someone to solve the probelm in x64 version. – chaobin zhang Dec 6 '16 at 11:09
5

Make sure you have numpy installed before you attempt to install the GDAL Python bindings; without numpy, it appears the _gdal_array native code will not be installed.

If you ended up in this situation, removing gdal, installing numpy and then reinstalling gdal might help:

    pip uninstall gdal

    pip install numpy

    pip install GDAL==$(gdal-config --version) --global-option=build_ext --global-option="-I/usr/include/gdal"

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

1

I had the same problem and solved it by building gdal from scratch, after I had Python 2.7 with numpy installed.

1

I ran across the same problem and it appears that many others have too when using the Anaconda environment. The accepted answer here did not work for me.

However, I solved it by working my way throught the alternative instalation methods listed in the Anaconda GDAL instalation documentation for the conda-forge package distributions. The last option (conda install -c conda-forge/label/cf201901 gdal) finally gave me a working environment with gdal_array. It also updated/reinstalled a ton of other packages too so be patient as it gathers information. It took a few minutes.

I don't think you need to try all of them like I did - I guess you can jump to the last one. I also suspect that the label may change over time - so I would check the documentation in the link before just cutting and pasting the command above if you look at this post a few months or years after I write this!

0

I am using gdal within a conda virtual environment. I had the same error, and I was able to resolve it by uninstalling and re-installing some of gdal's key dependencies.

To determine its dependencies: conda info gdal

More specifically, I had to conda uninstall the following:

  • gdal
  • numpy
  • libgdal
  • geos

Quick note on geos: it was not listed as a dependency upon querying with conda info. However, when attempting to reinstall gdal, I had found that there were conflicting libraries associated with geos, so I uninstalled it for thoroughness.

Then, a quick conda install gdal will re-install gdal and its dependencies. When you re-install gdal again with the above line, double-check that you are installing gdal version > 2.0

These specific commands will be different depending on your python distribution / package manager. But hopefully, following a similar procedure of uninstalling/reinstalling will enable you to get a working version.

-1

I just solved this by placing the right files extracted from RPM. Here is where I get my 64bit RPM http://www.filewatcher.com/d/Mandriva/2010.1/x86_64/Sciences/Geosciences/gdal-python-1.7.2-1mdv2010.1.x86_64.rpm.220418.html

Using the rpm extraction command provided by http://www.cyberciti.biz/tips/how-to-extract-an-rpm-package-without-installing-it.html

I was able to find the lost _gdal_array.* files in the path below usr/lib64/python2.6/site-packages/GDAL-1.7.2-py2.6-linux-x86_64.egg/osgeo/

Then simply copy _gdal_array.* to python2.7/site-packages/osgeo/ and problem solved. Hope this helps anyone with similar problems.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.