Quantum GIS (or QGIS) is an easy to use, open-source, cross platform Geographic Information System with powerful capabilities, and roots going back to GRASS. One of the cool things about QGIS is the ability to extend its functionality using Python to build plug-ins which are easily copied to, and run on, different operating systems and platforms.
This post assumes that you are familiar with GIS systems, in general, and wish to develop applications for QGIS, specifically on Ubuntu (or other GNU/Linux versions). First, I’ll be covering issues with different versions of Ubuntu and QGIS, then hiccups with setting up the development environment (Eclipse, PyDev, …), and finally, establishing a work flow for efficient (and fun) development. A good introduction to QGIS is Learning QGIS 2.0 by Packt Publishing. The online documentation on the main website is also good, but some portions need to be updated.
As you may (or may not) know, development on QGIS is very active, and this has lead to some instabilities in the software, and a lag in documentation. To mitigate these issues, we’re going to install and run QGIS version 2.2. This release is the most stable (and fastest) of the “2.0” releases, and has documentation that is mostly up-to-date.
I was running Ubuntu 13.04 when I first tried to set-up a QGIS development environment. The standard Ubuntu distribution for QGIS was a pre-2.0 version, which has an older Python API. Based on user recommendations (and lack of version 2.4 documentation), I tried to compile (from source code) and install version 2.2. This failed due to library dependency conflicts. A little research showed that Ubuntu 14.04 uses the required libraries, so I updated my development machine. The standard Ubuntu distribution under Ubuntu 14.04 is QGIS version 2.0.1, but version 2.2 compiled and installed well (with many bug and performance fixes). Instructions for building QGIS are here. I installed the build dependencies listed for “trusty”, but downloaded the source code “tarball” from the main website instead of checking it out from github.
The 2.2 release source code can be found by going to the download page, then click on the “ALL RELEASES” tab near the top of the page, then click on the “Older releases of QGIS are available here” link, then download qgis-2.2.0.tar.bz2. Un-compress the source tree, and section 3.7 in the INSTALL file details the build steps. After installing, just type “qgis” to fire it up (you may have to log-out and log back in again).
I decided to try and use the Eclipse IDE with the PyDev plugin. Once Eclipse is installed (sudo apt-get install eclipse), follow the directions on the PyDev page to install PyDev as an eclipse plug-in. All that’s well and good, and the QGIS documentation tells how to configure QGIS and PyDev using the Remote Debug plug-in (in QGIS). BUT, what I found out (the hard way) is that PyDev has changed their debugging protocol, and it’s not compatible any longer with the QGIS Remote Debug plug-in. It may look as though it’s working, at first, but things go horribly wrong quickly… I spent a day trying to fix the QGIS plugin, but finally gave up (having actual work to get done).
You can still write code in Eclipse, but no breakpoint debugging, etc, is possible within QGIS (sigh). I just fell back to vim. Things actually start to get better from here The main reference document for creating plug-ins is the PyQGIS Developer Cookbook. I recommend spending some time getting acquainted with it. The first thing you’ll want to-do is install the Plugin Builder plug-in. Do this by starting QGIS, selecting the “Plugins” pull down at the top menu panel, select “Manage and Install Plugins”, then scroll down and select & install Plugin Builder, and Plugin Reloader. Both of these will then appear under the “Plugins” pull down. This site covers how to use the Plugin Builder.
Plug-ins are created (and installed) in your home directory under the .qgis2/python/plugins directory. I do NOT recommend doing development in this area. Copy your plug-in code to another directory (like an eclipse workspace), then symbolically link that directory to the $HOME/.qgis2/python/plugins/(plugin-name) directory. I “uninstalled” my plug-in once, only to find out that this operation deleted the plugin directory and its contents (oops). A symbolic link will keep this from happening (version control recommended).
If you’re not sure where to start, try installing some plug-ins that look like they may have similar functionality (or at least partial) to what you’d like to-do. Then examine the code, to see how it’s done. This page lists available plug-ins. Once you start editing your plug-in code, it can be reloaded into QGIS with a single keystroke (F6) after you’ve configured the Plugin Reloader plug-in. Your plug-in should appear as an icon (initially a “plug”) on one of the menu bars. This allows you to edit, reload, and go, with just a couple of key/mouse strokes, without having to restart QGIS.
Graphical (UI) design is a snap with the QT UI designer tool. Just run the program “designer” (or designer-qt4) with your plug-in .ui file as the first argument. Drag & drop your widgets, then wire-up the functionality in the “plug-in”.py file. The class reference page for QGIS is here, and for the QT widgets here.
Just to reiterate, some portions of the PyQGIS Developer Cookbook haven’t been fully updated to version 2.2 (still pre 2.0). If you get stuck, look at other plug-in code to see how things are being done.
That should get you up and running, and if you have questions, just leave them in the comments section, and I’ll get back to you.
To enable the Spatialite database in the Database Manager, be sure to:
sudo apt-get install python-pyspatialite
To enable MrSID image file support (LizardTech proprietary format), the gdal package has to be recompiled with the proprietary binaries provided by LizardTech, once you’ve signed up as a developer. As such, it’s a rather involved process, and beyond the scope of this blog post.
On another note, the Multispec package (windows only) from Purdue doesn’t run under Wine 1.7 (sigh).