[GIS] Installing GDAL in Python on Windows 7

gdalinstallationpython

I'm having problems installing the gdal library for python. I use a Windows 7 and have WinPython 2.7.9.2 which runs Python 2.7.9. I'd like to use gdal + Python for raster manipulation.

I've read that PIP is the preferred way for installing packages on windows and that its packed with Python 2.7.9. I've edited my PATH environment variable to include pip and python:

;c:\~\WinPython-32bit-2.7.9.2\python-2.7.9\Scripts\;c:\~WinPython-32bit-2.7.9.2\python-2.7.9\

so I can now run both python and pip directly from command prompt.

The error appears when I type

c:\>pip install gdal

which gives:

C:\~\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Include\xlocale(342) : warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
extensions/gdal_wrap.cpp(2855) : fatal error C1083: Cannot open include file: 'cpl_port.h': No such file or directory
error: command 'C:\\~\\AppData\\Local\\Programs\\Common\\Microsoft\\Visual C++ for Python\\9.0\\VC\\Bin\\cl.exe' failed with exit status 2

So apparently something called 'cpl_port.h' is missing. This is also described in this question where it's suggested that header files cannot be found. The solution provided in that thread is however LINUX based and incompatible with my Windows.

I'm not quite sure how to proceed from here so should I forego PIP and try another route?

I've verified that PIP works by installing some other packages, so the problem seems to be with gdal.

Best Answer

GDAL is actually a set of toos written in C. As such it needs to be compiled prior to installation, and Windows doesn't come with a C compiler. As such your easiest bet is to download and install pre-compiled binaries of the library. The easiest place to get binaries is from Christoph Gholke who offers a package of the complete GDAL library as well as the Python bindings.

Finally, if you're looking to play with modifying rasters in Python I'd strongly recommend installing rasterio (binaries available from Gholke as well) which is a much nicer wrapper around GDAL and might be a lot easier to use.