Building a plugin for QGIS

Quantum GIS is an open source GIS desktop application.  Python is the scripting language for QGIS.  Below are instructions on how to build a python plugin for QGIS in order to extent functionality.

 

Necessary installations

A. Quantum GIS - you can download the latest version of Quantum GIS from the following website.  http://www.qgis.org/en/site/

B. Qt Creator - Qt is a software development framework that is used to develop applications that run on Windows, Mac, Linux as well as various mobile operating systems. QGIS itself is written using the Qt framework. For plugin development, we will use an application called Qt Creator to design the interface for our plugin.

C. OSGeo4W network installer and choose Express Desktop Install. Install the package QGIS. After installation, you will be able to access the pyrcc4 tool via the OSGeo4W Shell.

 

Additional Plugins

A. Plugin Builder plugin - In QGIS go to Plugins > Manage and Install Plugins.  Search for Plugin Builder and install.  

This plugin will build the framework for your new plugin to make getting started so much easier.

B. Plugin Reloader plugin - In QGIS go to Plugins > Manager and Install Plugins.  Search for Plugin Reloader and install.

This plugin will allow you to reload the plugin during development and testing.

 

Building your first plugin

A. Working with QGIS Plugin Builder

  • In QGIS go to Plugins > Plugin Builder

This will open a dialog box.  Enter the following information

Class name - DeleteFeature

Plugin Name - Delete Feature

Description - This plugin will be delete the selected feature

Module Name - delete_feature

Author Name - <your name>

Email - <your email>

 

  •  Click Next

 

The next window asks for more information about your plugin.  This information will be viewed when the user is loading the plugin from QGIS.

  • In this box type in the following.

This plugin will delete the selected features from one layer.

  • Click Next.
  • Leave all the defaults on this screen and click Next.
  • Click Next again until you get to the screen asking for publication information.
  • Enter your Bug Tracking and Repository information here and click Next.

Finally it is asking for a location to store your plugin.  QGIS plugins are stored in the following directory on Windows C:\Users\<user name>\.qgis2\python\plugin.  

  • Navigate to plugin directory and click Select Folder.

 

B. Build plugin resource file

  • Go to Start > Programs > OSGeo4W > OSGeo4W Shell.

In ths shell navigate to where your plugin is installed. 


  • Next type in pyrcc4 -o resources.py resources.qrc.  

This command will compile the .qrc file to .py which you will import into your plugin python code.

 

  • Close QGIS and reopen QGIS.
  • In QGIS go to Plugins > Manager and Install Plugins and turn on your plugin Create Plot.

You will see a new tool on your interface with the following icon.   

  • Click on this icon.

Notice that a dialog box opens with an OK and Cancel button but nothing else.  You will now enter information to populate this dialog box. Close the window we will now write some code for the dialog box.

C. Design Dialog Box

  • Open "C:\Users\boehnert\.qgis2\python\plugins\DeleteFeature\delete_feature_dialog_base.ui" in QT Designer.
  • Drag a ComboBox to the interface and set the Object Name to be layerListCombo

 

  •  Now drag out a label and change the Text for label to read - Select a layer from the list that contains the feature to be deleted.
  • Click the option for a textWrap.
  • Click Save.
  • Back in QGIS Reload the Plugin Delete Feature.
  • Click the icon again.

You will now see the combobox and the label that you just added.  


D. Add code behind the dialog box

  • In a text editor open the file "C:\Users\boehnert\.qgis2\python\plugins\DeleteFeature\delete_feature.py"
  • Go to the end of the code and the following code to the run method

 


#this code will populate the combo box with all

#vector layer from the Table of Content

self.dlg.layerListCombo.clear()

layers = self.iface.legendInterface().layers()
layer_list = []
for layer in layers:
   layerType = layer.type()
   if layerType == QgsMapLayer.VectorLayer:
      layer_list.append(layer.name())
self.dlg.layerListCombo.addItems(layer_list)


Also add the following import statement at the top of the script.


from qgis.core import *

  • Save your python script.
  • Reload the plugin in QGIS.  
  • Make sure you have data in your Table of Content.
  • Test your plugin.

 

E. Add code to delete the selected feature once OK is clicked.

We still need to add code to run when you click OK.  Back in your text editor add the following code to the run method in the if result: statement

# Do something useful here - delete the line containing pass and 

# substitute with your code.
# delete selected feature from layer chosen by user

selectedLayerIndex = self.dlg.layerlistcombo.currentIndex()
selectedLayer = layers[selectedLayerIndex]
selFeatures = selectedLayer.selectedFeatures()
ids = [f.id() for f in selFeatures]
selectedLayer.startEditing()
for fid in ids:
   selectedLayer.deleteFeature( fid )
   selectedLayer.commitChanges()


mc = self.iface.mapCanvas()
mc.refresh()



  • Save your python script
  • Reload the plugin in QGIS

This script will delete a feature permanently from a layer.  You may want to work on test dataset.

  • Select a feature to be deleted.
  • Run your new plugin tool.
  • Select the layer name that contains this feature to be deleted.
  • Click OK.

Your tool will delete all features that are selected in the layer you chose.