After being fresh out of Udacity CS101 – Computer Science course I wanted to put my new Python skills to use. QGIS supports plugins programmed in Python so I thought I would give that a go. What I realised is that there is a missing link between an introduction to programming and how to apply it! After several hours I was able to complete a simple plugin and get it working, with a GUI – all in Windows 8.
Here are the steps I followed:
1. Install QGIS 1.8 using OSGeo4W. Select ‘Express Desktop Install’. I installed all packages even though we won’t be using them all. It is worth having.
2. Download and Install Monkey Studio IDE.
Optional: Download QGIS APIs for auto-complete. You can add these to Monkey Studio by going to Edit > Settings. In ‘Source APIs’ select Python as language and add all the .api files. I sources these files here but stored them on my server in case the drop box goes missing.
3. The first Plugin I made was called Zoom to Point. I used instructions from a book excerpt available here. Chapter 15 – GIS Scripting, starting at page 270.
I was able to follow the instructions with the following additional tips:
- Windows 8 QGIS plugin directory is located at
- QT Designer is built into Monkey Studio. When you need to edit ui_zoomtopoint.ui launch Monkey Studio, then open this file to edit it.
- Run the command line commands in the OSGeo4W console which should be on your desktop from Step 1. Navigate to your plugin directory e.g.
pyrcc4 -o resources.py resources.qrc
pyuic4 -o ui_zoomtopoint.py ui_zoomtopoint.ui
- On page 284-285 there is code which you will need to copy over to your zoomtopoint.py file. For me lines 41 and 42 produced an error. I was able to fix this by replacing them with:
41 x = dlg.ui.xCoord.toPlainText()42 y = dlg.ui.yCoord.toPlainText()
- In the file zoomtopoint.py (around line 27) I changed ‘import resources_rc’ to ‘import resources’. Otherwise I get an error in QGIS.
- Every time you make a change to your plugin you need to restart QGIS. There is a plugin called Plugin Reloader that speeds this up allowing you to reload plugin with restarting QGIS.
- Be very careful manually copying code. Most of my errors came from incorrectly copying code!
I hope that saves some others a few hours starting out. I have just scratched the surface and am looking forward to building some handy QGIS plugins as time allows.
Some more tips as I go:
- For some reason if I create my Class name (ie when creating plugin template in Plugin Builder) in all lower case it does not build properly with pyuic4. If you have this same problem try using CamelCase and see if that makes a difference – it did for me.