Thursday, February 3, 2011

Configure GDAL/OGR Python debug environment in Pydev on Windows

Recently I am involved in a project that requires some Geospatial development in Python. So I decide to take this chance to get familiar with using GDAL/OGR in Python because it is the most popular open source GIS library plus it has a python binding.

The first thing you need is obviously a Python development environment for GDAL/OGR. I know IDLE or even a plain text editor plus “print …” will do the job just fine, but I still prefer a decent IDE. Why not? Especially when it is free. So I choose the Eclipse based Python IDE Pydev. The only problem is that the process of configuring it with GDAL/OGR on Windows isn’t that obvious, or at least to me it is not. So in this post I will go through it step by step.

1. Install Python

Installing Python is quite straight forward, just go to http://www.python.org/, pick up the installer for your version and OS, and install it. I would recommend Python 2.6 or 2.7 because there are pre-compiled GDAL/OGR python binding for both versions.

2. Install GDAL/OGR Python binding

First of all, this website is really helpful for you to get pre-compiled GDAL/OGR Python binding. It has different stable released version and the version latest trunk, and the best part is that it also has the installer version and non-installer (zipped version) versions for both 32bit and 64bit.

Use installer version

Find the .exe or .msi installer file, which usually comes with name GDAL-1.x.0-win32-py2.x.msi () e.g. here is the download link to the one for GDAL 1.8.0 32bit for python 2.7 (GDAL-1.8.0.win32-py2.7.msi). So basically just follow the installation wizards to finish the installation. Under the hood, my understanding is that the installer just simply copy a bunch of files and folder into \Lib\site-packages folder of your Python install location.

Note: the installer version requires you to have an appropriate version of Python installed and registered in registry first. The normal installation of Python takes care of the registry, but if not (e.g. you want to use the Python that comes with ArcGIS Server), then you will have to manually install a standard version of Python at a different location, install the GDAL Python binding, then copy those GDAL related files and folders back to the site-packages folder of that unregistered Python.

Use non-installer version

Instead of using installer version, you can choose to use a non-installer version which avoid adding stuff upon your vanilla Python install and registry. So from the same website just download those zip files with name e.g. release-1500-x64-gdal-mapserver.zip (don’t worry it comes with  a copy of UNM MapServer), and unzip it on your local disk. The “information” link gives you very detailed information about the folder structure and versions of packages included.

After unzipping, I highly recommend you to take a look at the SDKShell.bat, which usually contains code similar to below:

:setenv2
SET PATH=%CD%\bin;%CD%\bin;%CD%\bin\gdal\python\osgeo;%CD%\bin\proj\apps;%CD%\bin\gdal\apps;%CD%\bin\ms\apps;%CD%\bin\gdal\csharp;%CD%\bin\ms\csharp;%CD%\bin\curl;%PATH%
SET GDAL_DATA=%CD%\bin\gdal-data
SET GDAL_DRIVER_PATH=%CD%\bin\gdal\plugins
SET PYTHONPATH=%CD%\bin\gdal\python\osgeo
SET PROJ_LIB=%CD%\bin\proj\SHARE

This basically tells you what environment variables and paths must be included in PATH system environment variable, which we need when later configuring Pydev.

3. Install Pydev Python IDE

Install Pydev IDE is also easy and straightforward. Either download it from sourceforge, or update it through Eclipse by adding http://pydev.org/updates in software update sites.

After installation, just launch it.

4. Configure GDAL/OGR Python debug environment in Pydev

This is the most trick part. Depend on whether you’re using the installer version or the non-installer version, the steps are different.

First of all, you need to specify the Python interpreter in Pydev. So just go to Windows->Preference. and navigate to “Pydev”

image

Click “New”, type in a name and specify the path to python.exe of your Python installation, and finally click ok.

If you install GDAL Python binding with installer, then you should be good to go.

If you install GDAL Python binding with non-installer zip file, then you will have to configure a few more things. Still in the “Python Interpreters” configure dialog box, at the bottom part, select “libraries” tab and click “New Folder” on the right, navigate and select the <GDAL_Python_Install>\bin\gdal\python, finally click ok.

image

Assume you unzip the GDAL Python package at E:\programs\gdal\1.8.0, then you should add E:\programs\gdal\1.8.0\bin\gdal\python to the list of libraries.

Next go to “Environment” table, and there you need to add a few environment variables to reflect the settings in SDKShell.bat (mentioned earlier) except for “PYTHONPATH”.

image

Assume again you unzip the GDAL Python package at E:\programs\gdal\1.8.0, then you should add following

Path=<your_original_path_variable>;E:\programs\gdal\1.8.0\bin;E:\programs\gdal\1.8.0\bin\gdal\python\osgeo;E:\programs\gdal\1.8.0\bin\proj\apps;E:\programs\gdal\1.8.0\bin\gdal\apps;E:\programs\gdal\1.8.0\bin\curl;
GDAL_DATA=E:\programs\gdal\1.8.0\bin\gdal-data
GDAL_DRIVER_PATH=E:\programs\gdal\1.8.0\bin\gdal\plugins
PROJ_LIB=E:\programs\gdal\1.8.0\bin\proj\SHARE

Note: these environment setting only applies within your Pydev IDE when you choose to use this particular Python interpreter, and it doesn’t affect your existing system environment settings.

Finally save the changes and you are good to go. To verify it, just launch a Python console window in Pydev, and execute following:

from osgeo import gdal

from osgeo import ogr

from osgeo import osr

They should just execute fine without any error message.

1 comment: