My first Python Qgis Plugin


I needed to make an html image map once for a small project. Because I was using arcview 3.1 at that time, I found a nice repository 1 with all kind of scripts, and also one (Avenue) script to do exactly what I wanted: take the current map view, generate an image of it, and write an html-file with an image and map-area tags in it 2.

Nowadays I'm using Qgis 3 ;-) . With QGis you can view your spatial data from Esri shapefile, miff files, Postgis/Postgresql databases and even WMS (WebMappingSources) from around the internet. One very nice feature is that from version 9.0 of Qgis on there will be an Python interface available. Not only to manupulate the map via a console, but also to make small custom map applications using Python and Qgis. {see link}. One nice feature of the 9.0 version is the ability to build your own plugins IN PYTHON.

So to make a long story short: I wrote my first QGis plugin, thereby not only polishing up my (rather thin) Python knowledge, but als learned how this Qt4 widget stuff is working, and what resource files are. I will try to write my ramblings down so everybody with a little spatial and python knowledge can start hacking plugins!

I do not pretend to know the best way to write a plugin, but hope this plugin is usefull to somebody, or makes somebody enthousiast enough to start off him/herself



You will need Qgis > version 0.9, and the python bindings installed. Download: see above for different versions.


This is how the dialog looks like:

And this example shows what it generates: for a given polygon or multipolygon it will:

  • write an screendump of current mapwindow as [name].png
  • write an html file with some basic javascript and html for an image map:
    <area shape="polygon" href="CROATIA" onClick="mapOnClick('CROATIA')" onMouseOver="mapOnMouseOver('ZAGREB')" 
  • the attribute values href, onClick and onMouseOver are optional in area tags
  • instead of writing ALL polygons to area tags, it's also possible to make a selection and just use this selection polygons to generate the area-tags.

    (Python) plugins

    The starting point for your own plugin will be the Qgis wiki: Then you will need to know something about how to build a Qt4 gui, the public Python interface for QGis-plugins, and the interface of PyQt

    Tips voor beginners (like me ;-) )

    qt designer

    If you never build or used qt dialogs, here is the way to go:

  • Using an '.ui'-file from another plugin, drag and drop you dialog together in Qt-Designer:

  • This will give you an '.ui' file
  • This .ui file is then compiled using pyuic4 to a python source file containing the gui class definition
  • You can then define your own class (inheriting from the gui class file above AND QDialog and add the event/signal handlers to it
  • if you named some widget for exampe 'chkBoxHref', there is an easy way to handle clicks from it by adding an method to the class with a certain name convention: on_chkBoxHref_stateChanged(self) etc
  • I had some troubles to handle the events from the 'standard button bar', but... it's easy: see

    The files:

  • make file (see in there for details about calling the pyuic4 and pyrcc4 programs)
  • (init file to let qgis init the plugin 'as plugin')
  • imagemap.png (image left in the plugin dialog)
  • imagemapicon.xpm (button image in button bar)
  • imagemapplugin.qrc (xml file containing the resource to use)
  • (compiled qrc file with pyrcc4)
  • imagemapplugingui.ui (qt4 ui xml file containing the dialog/gui definition)
  • (compiled ui file with pyuic4; contains class definition of: 'Ui_ImageMapPluginGui' (because main qt-object is name ImageMapPluginGui)

    Essential documentation

    There are some really good starting points in the blog and wiki: Tim Sutton's introduction in writing python apps using the Python Qgis interface 8 and a wiki page to show how to start with a simple Qgis python plugin 9

    You will really need the pyqt-api documentation 5 and off course the qgis documentation 6

    Links and resources

  • (Free) arcview Avenue script for inspiration and idea's
  • The original arcview image map extension
  • The Qgis site
  • Wiki page about developing Qgis Python plugins
  • PyQt documenation/api
  • QGis iface api and
  • MUST READ: Tim Sutton's introduction in writing python apps using the Python Qgis interface
  • MUST READ: Wiki page to start with a simple Qgis python plugin
  • Developers corner page in Wiki

    Tips, comments, questions: richard at-sign (Richard Duivenvoorde)