Friday, March 21, 2014

Creating QGIS Plugins in Windows: Getting Started

I have started thinking about creating a plugin or two for QGIS, but I ran into a few hurdles along the way... I am not a programmer (and maybe you are not either!), but I know a few basic skills, so keep that in mind. I am working on creating QGIS plugins on a Windows 7 PC.  Many programmers likely use a Linux distribution.

First, there are several tutorials on the web but I would start with this one.  To start, in QGIS navigate to the "Plugins" tab and download the Plugin Builder, if you don't have it installed already. The plugin builder creates a template with important information for your plugin as well as the needed files.  Click on any of the screenshots to magnify them.

QGIS Plugin Builder 2.0.3
The text in green and the link to the tutorial explain what each of the fields mean and does for your plugin. A folder with the necessary files will be created.  Remember the file path.

The next step is to compile the plugin files.  This was the first hurdle.  Eventually, I found a post with a solution.  Double-click the OSGeo4W icon on your desktop.

  1. At the command prompt, point to the folder where your plugin is located.  Example:  cd: C:/Users/yourusername/.qgis2/python/plugins/pluginname (in this example CalculateArea is the name of the plugin).  You will need to substitute the fields highlighted in red.
Point to the directory where you plugin is located.
  1. Next, type: pyuic4 -o ui_pluginname.py ui_pluginname.ui and press Enter
  2. Lastly, pyrcc4 -o resources.py resources.qrc and press Enter
Compiling the Resource and UI files

Then follow the steps for deploying and testing.  After installing the plugin from the plugin tab, you should be able to see it in the tab along with any other plugins you have installed.  Click it and the basic plugin window will appear!  You should be able to install your plugin in QGIS and see its name.
The basic plugin window with the name of your plugin
Lastly, time to start writing some python. You can download a viewer/editor like gedit.  Open up the file named pluginname.py and you will see a lot of the basic skeleton coding already written.

Viewing and editing python code from the QGIS Plugin Builder 
A few parting thoughts:  Be sure to search the QGIS repositories to see if someone has already created a plugin for what you are thinking of doing.  Borrow code from other plugins, which have been validated, to help speed the process along.

No comments:

Post a Comment