2011年12月27日 星期二

Configuring PyScripter for Quantum GIS (qgis) on Windows7

Recently, I am studying QGIS if it is easy for customization and whether it can be used as a testing platform for my research studies. In fact, I has been using OpenJump for over 1 year and it works great especially for its capability adding plugin for new functionality. However, after reading some blogs and comments from the Internet, it seems many people are using QGIS for development and many comprehensive and updated tutorials and resources (in fact, tutorials found are more or less the same) can be found when compared with OpenJump. This triggered me to have a in-depth study in QGIS. QGIS is developed by C++. I do think for serious GIS development, C++ is the only solution. Besides C++, QGIS also supports PYTHON for building plugin. As revealed from the Internet, PYTHON is comparatively easier than C++ due to its readable syntax. Frankly, I think JAVA is better. I think the event and action model, i.e. Connect, SIGNAL and SLOT, is not easy to follow.

The reason I write this blog is to record the way I setup PyScripter for QGIS as it has taken me part of my Christmas holiday to figure out how to do it. To do this, you should know the following:
1. QGIS is using QT4 for UI design;
2. QGIS has its own library for Python called PyQGIS;

The goal is to setup PyScripter so that it can provide auto-complete function for programming PyQGIS. Without auto-complete, you can still program it but it would be much easier to figure out typo and syntax errors in the program with auto-complete.

The following is the setup procedure (cover both QGIS and PyScripter):
1. Download and install QGIS and OSGeo4W from here. (Then, you will have a OSGeo4W directory, QGIS and all the required libraries.)
2. Download and install PyScripter from here.
3. Read this to setup batch file for startup PyScripter. The batch file I am using is as follows (Last statement is the key):
@echo off
SET OSGEO4W_ROOT=C:\OSGeo4W
call "%OSGEO4W_ROOT%"\bin\o4w_env.bat
call "%OSGEO4W_ROOT%"\bin\gdal16.bat
@echo offapps\qgis\python\qgis
path %PATH%;"C:\Program Files\Quantum GIS Wroclaw\bin";%OSGEO4W_ROOT%\apps\qgis\python\qgis;
Start "PyScripter" /B "C:\pyscripter\pyscripter.exe" --python25 --pythondllpath=C:\OSGeo4W\bin

4. Edit the python_init.py of the PyScripter and make sure the following statements are there.
from PyQt4 import QtCore, QtGui
from qgis.core import *
from qgis.gui import *

5. In the menu, select Tools>Options>IDE Options. Add PyQt4, qgis in the special package.

6. Add the following paths in the system PATH and a new system variable "PYTHONPATH" in the Windows environment. Without the following PATH, PyScripter cannot find the qgis libraries, e.g. qgis_core.dll
System Path: %PATH%;C:\OSGeo4W\apps\qgis\bin;C:\OSGeo4W\bin;
PYTHONPATH: C:\OSGeo4W\apps\qgis\python;C:\OSGeo4W\apps\Python25;

After all, the PyScripter should start without any error. However, it seems it still cannot retrieve some functions e.g. QMessageBox.