Page 1 of 2 (30 posts)

  • talks about »
  • python


Last update:
Sat Nov 1 05:55:09 2014

A Django site.

QGIS Planet

Oh God my plugins! My precious QGIS plugins


The API had to change. We don't like breaking plugins. It makes us sad. We did it now to save pain in the future. You will like the new version better. Trust me.

What happened to my cheese?

When updating to QGIS 2.0 you might have noticed two things. (Apart from all the new awesome stuff!)

  1. All your settings have been set back to defaults
  2. Some of your plugins are gone, or missing in the installer.

Resetting of settings is caused by QGIS now storing its 2.0 settings in a different folder then we used for 1.8. In 1.8, all your plugins, etc, were stored in the ./qgis folder in your home directory, in 2.0 these are now stored in ./qgis2. The reason will become evident later. All user settings, the UI layout, database connections, etc, are now stored in a QGIS location. In windows this in the registry key HKEY_CURRENT_USER\Software\QGIS\QGIS2. So this explains why your settings are missing when you first load QGIS 2.0.

Why did we have to move the settings location?

Good question.

2.0 is very different from 1.8. There has been a lot of work to make this the best version we have ever made, new features, more bug fixes, a bigger dev team, and a even bigger community. Being the next major release we had to make some calls to remove some of the old code and features that were weighing us down. Features such as the old labelling engine, old symbol engine, the old vector API. Carrying old code and old out dated features into the future can sometimes really hurt a project and they have to be cut loose. Because of the massive amount of changes in 2.0 people needed to be able to run 2.0 and 1.8 on the same machine without causing any issues. If they both store settings in the same location this would have had bad results.

Why move the settings. Part 2

Moving the settings was also a result of having non backwards compatible plugins between 1.x and 2.x. If we kept both plugins in the same folder it just wouldn't work. You would install a 1.8 version of a plugin, I would update my plugin to 2.0, you then install the same plugin in 2.0, and now you 1.8 version is broken. Fun!. To avoid this we moved all QGIS 2.0 stuff into ./qgis2.

Why did my plugins break anyway. Why not just leave them be.

In 1.x we were using SIP v1. This meant the framework we used, PyQt, felt more like C++ then it did Python. If you are a Python developer then this isn't a very fun thing to deal with. In SIP v1 you need to tell PyQt, and our QGIS API, what to convert the type to. feature['mycolumn'].toInt() that is pretty gross. In V2 you can just do feature['mycolumn'] and SIP will auto convert the type for you. This makes our API feel more like Python and less like C++. There are other changes when using SIP V2 but you get the idea. Unfortunately SIP v1 and v2 do not work together so we couldn't make the code backwards compatible. This was also a forced change for us. Once we switch to Python 3 at some stage in the future V2 would be the default and we have to change then. The bright side of this change is most of the time you are removing code. Consider it a good time to go though your code, give it a bit of a polish, and remove anything that is no longer needed.

There was another major API change that needed to happen. Vector API update. In order to allow for multithreading in the future, and we all know everyone is looking forward to that, we needed to change how code can ask for features from a layer. The old method would never work in a multithreaded structure and had to go.

What can I do if I need a plugin?

Having a plugin missing from the plugin installer when you really need it can be a bit of a pain. Plugin authors are working hard to update there plugins. I approve about two a day into the plugin repository. While most plugins might be updated at some stage. There are some things that you can do if you need a plugin update to work with 2.0.

  1. Email the author of the plugin to see where they are at with the update

  2. Email the author and offer your help to update the plugin. Remember a lot of plugins are written by volunteers who just need the plugin to get their work done and wanted to share it with everyone.

  3. If the author has no intention of updating the plugin, or can't be contacted. You are free to update you local copy and offer it back to the community as the updated copy. If you are going to upload it back to the plugin repository please try to contact the author and seek permission first. I will be watching for copies of plugins to make sure we don't end up with 10 versions of the same plugin. The GPL allows for you to modify and share your updated version but it's nice to keep the original author in the loop. If the author no longer wants to maintain the plugin and you are able to then contact me and I will make you the owner of the plugin. Overall be nice not evil, we are all friends here.

  4. If you don't have, or know someone with, the skills to update the plugin. You can contact a developer to help update the plugin for you. Companies like mine, or Faunalia, or a whole range of other open source devs, can normally be contracted to update a plugin if needed.

Moving forward

We like the new API. It makes the Python side of QGIS much cleaner. There is still more work to do, it's never ending, but this is a good step. We don't like breaking plugins, but breaking a few now is better then breaking heaps as the popularity of QGIS continues to grow.

GRASS GIS 6.4.3RC4 released

Fourth (and last) release candidate of GRASS GIS 6.4.3 with improvements and stability fixes
A fourth release candidate of GRASS GIS 6.4.3 is now available.

Source code download:

Binaries download:

To get the GRASS GIS 6.4.3RC4 source code directly from SVN:
 svn checkout

Key improvements of this release include some new functionality (assistance for topologically unclean vector data), fixes in the vector network modules, fixes for the wxPython based portable graphical interface (attribute table management, wxNVIZ, and Cartographic Composer), fixes in the location wizard for Datum transform selection and support for PROJ.4 version 4.8.0, improvements for selecting the Python version to be used, enhanced portability for MS-Windows (native support, fixes in case of missing system DLLs), and more translations (esp. Romanian).

See also our detailed announcement:

First time users should explore the first steps tutorial after installation.

Release candidate management at

Please join us in testing this release candidate for the final release.

Consider to donate pizza or beer for the upcoming GRASS GIS Community Sprint in Prague:
Thanks to all contributors!

Python support even closer

anybody has a hint?


flattr this!

Getting closer to taming the snake

very geeky but I have to post this:

D/Qt (27512): src/python/qgspythonutilsimpl.cpp: 188: (runString) COMAND OK: import sys
D/Qt (27512): src/python/qgspythonutilsimpl.cpp: 188: (runString) COMAND OK: import os
D/Qt (27512): src/python/qgspythonutilsimpl.cpp: 188: (runString) COMAND OK: sys.path = ["/data/data/org.qgis.qgis/files/share/python","/data/data/org.qgis.qgis/files//python","/data/data/org.qgis.qgis/files//python" + "/plugins","/data/data/org.qgis.qgis/files/share/python/plugins"] + sys.path
D/Qt (27512): src/python/qgspythonutilsimpl.cpp: 91: (initPython) newpaths: "/data/data/org.qgis.qgis/files/share/python","/data/data/org.qgis.qgis/files//python","/data/data/org.qgis.qgis/files//python" + "/plugins","/data/data/org.qgis.qgis/files/share/python/plugins"
D/Qt (27512): src/python/qgspythonutilsimpl.cpp: 188: (runString) COMAND OK: from sip import wrapinstance, unwrapinstance
D/Qt (27512): src/core/qgsmessagelog.cpp: 45: (logMessage) 2013-05-21T01:57:20 [0] Python support ENABLED :-)


flattr this!

Python support in qgis is getting there

Never been so close,  but it took the heck out of me… now lets see if after 4 days of continuous fiddling around I manage to tame the snake

flattr this!

Installing Python setuptools into OSGeo4W Python

The easiest way install Python libraries is to use easy_install and pip.  easy_install and pip are package managers for Python. From the easy_install page:

Easy Install is a python module (easy_install) bundled with setuptools that lets you automatically download, build, install, and manage Python packages.

and from the pip page:

pip is a tool for installing and managing Python packages, such as those found in the Python Package Index. It’s a replacement for easy_install.

To get easy_install you need to install Python setuptools and you are good to go. Sounds easy!  However the setuptools installer assumes that you have the normal standalone Python installed which writes it’s install location to the registry, and when you run the installer it will say that it can’t find Python on the system.  What the!?

If you have installed QGIS, or any other tool from the OSGeo4W install, you will see that OSGeo4W bundles its own version of Python in: C:\OSGeo4W\apps\python27. This is the Python that is used when calling python in the OSGeo4W shell.  It seems someone on the OSGeo wiki has made a bootstrapped installer for setuptools that will install setuptools and easy_install into the  C:\OSGeo4W\apps\python27 folder for you.

Steps to take

  • Download
  • Run python in your OSGeo4W shell
  • Done!

To install a package with easy_install just use:

easy_install {packagename}

I wanted to have bottle and flask installed:

easy_install bottle

which gives you something like:

Searching for bottle
Best match: bottle 0.11.4
Processing bottle-0.11.4.tar.gz
Running bottle-0.11.4\ -q bdist_egg –dist-dir c:\users\woo\appdata\local\temp\easy_install-5b4qq6\bottle-0.11.4\egg-dist-tmp-q2yd68
zip_safe flag not set; analyzing archive contents…
bottle: module references __file__
bottle: module references __path__
Adding bottle 0.11.4 to easy-install.pth file
Installing script to C:\OSGeo4W\apps\Python27\Scripts
Installed c:\osgeo4w\apps\python27\lib\site-packages\bottle-0.11.4-py2.7.egg
Processing dependencies for bottle
Finished processing dependencies for bottle

Install pip


Most of the time any Python packages that are needed by your OSGeo4W tools are bundled in the installer and can be downloaded using the OSGeo4W installer, however there have been cases when I wanted to install a non OSGeo4W package into my setup by using easy_install or pip. Like bottle and flask in the example above.

Filed under: Open Source Tagged: FOSSGIS, osgeo, python, qgis, Quantum GIS

Remote debugging QGIS plugins using PyCharm

Wow it’s been a long time since I posted anything here….but I’m back with another (hopefully) useful howto. Over the last six months or so I have made a rather dramatic shift away from using VIM as my primary development environment for everything to using IDE’s for my python development work. I spent the first... Read more »

Remote debugging QGIS plugins using PyCharm

Wow it's been a long time since I posted anything here....but I'm back with another (hopefully) useful howto. Over the last six months or so I have made a rather dramatic shift away from using VIM as my primary development environment for everything to using IDE's for my python development work. I spent the first 4 or so of those months using Eclipse PyDev which is excellent but has certain issues, particularly from a QGIS context. Most of the issues relate to my not being able to get it to reliably recognise the QGIS API after adding the QGIS python directories to the package search path and the lack of decent refactoring tools. In my java programming days of yore, I used to love the refactoring tools that Eclipse provided, but unfortunately these are not carried through to PyDev.

Lately I have been using PyCharm which is a dedicated python IDE also written in Java (ironic). I'm not going to attempt to review all of PyCharm here (Guido van Rossum already did that but bear in mind the review is two years old and the software has surely advanced a lot since then). I will say there are two things that I find not as pleasant to use in PyCharm (compared to PyDev) - its PyLint and PEP8 checkers. In the projects I work on we follow PEP8 very strictly and not having a proper integrated tool for this is a real PITA. There are work arounds however so it is not a lost cause, but it would be nice if the developers took this a little more seriously:

On the other hand, I simply do not see the value of highlighting PEP 8 violations in the editor, especially in the middle of code modifications. We'd rather spend our time implementing inspections that report actual problems with the code, and not formatting nit-picks. - Dmitry Jemerov

One other big downside to PyCharm is that it is not open source software which goes against the grain somewhat. They do however provide free licensing to those using it for bona fide open source development work, so don't let the price tag deter you if you are planning to use if for a FOSS project.

Read on to see how I use PyCharm for QGIS development...

PyCharm for QGIS remote debugging

Why use PyCharm? Well it happens to be a really nice platform for QGIS python / python plugin development. For this article I am going to focus on the steps needed to remote debug python scripts / plugins running inside QGIS. The process is conceptually the same as remote debugging using Eclipse/PyDev - which I have blogged about previously. I am going to start with the assumption that you have your PyCharm set up and your plugin basics in place and now you are at the point where you wish to debug your software while it is running in QGIS. Let me prefix this by saying that I very seldom need to use this technique since our code is heavily tested (by means of a python test suite) so in most cases I can just debug a particular test directly without needing to remotely attach to a python process in QGIS. So the first thing you need to do is set up a python debug server (provided as part of PyCharm). To do this,  choose edit configurations from the task list:

Create a new run configuration

Next click on the little '+' icon and choose python remote debug:


Set the following options:

  • Local host name: localhost
  • Port: 53100

Note that you can use any high port that you like (assuming it is unused).

You will notice that on the dialog it gives you some handy hints as to what needs to be inserted into your code in order to enable the trace point.

Creating the debug server run configuration

The next thing you need to do is add a couple of lines to the module that you wish to debug (this is also described in the above dialog). First, in your imports add this:

from pydev import pydevd

And then in the place where you wish execution to halt, add this line:


You can also try using 'localhost' instead of your IP address.

The last thing you need to have in place before you can test is pydevd needs to be in your PYTHONPATH in the context of the running plugin. In my case I simply extracted the pydevd egg supplied in the root of the PyCharm installation into my plugin directory:

cp ~/apps/pycharm-2.5.2/pycharm-debug.egg .
unzip pycharm-debug.egg

There are a number of other ways you could do this, for example by changing your code to add the pydev directory into sys.path.

Ok now you are all set. One thing to remember is that the settrace line is just the initial breakpoint - you can set additional breakpoints in your code using normal PyCharm debugging techniques. Now launch your PyCharm debug server configuration by clicking the little run icon next to it (highlighted in red below):

Start the debug server

After this you will see some output like this in the PyCharm run panel:

Starting debug server at port 53100
Waiting for connection...

Next fire up your copy of QGIS and open the plugin that will trigger your settrace. When the trace point is hit, PyCharm will enter debug mode and highlight the trace line in blue like this:

PyCharm debugging a remote process

Now you can step through your code, inspect variables and generally have a productive time understanding your code. I'll hopefully post a follow up article on how to set up PyCharm for QGIS development in the future! Happy hacking!

Don’t forget to migrate your QGIS plugins!

From QGIS 1.8 and onwards the Plugin Installer plugin will no longer include the option to add the 3rd party repositories.  This was by design and intended to move people over to using the official plugin repository at so we can provide a richer experience and keep everything in one place.

If you have plugins that are still not on the official repository then I would strongly recommend that you migrate them as a lot of new 1.8 users will be missing out on your great work.

Filed under: Open Source, qgis Tagged: FOSSGIS, gis, Open Source, python, qgis, Quantum GIS

Generating chainage (distance) nodes in QGIS

Something that I need to do now and then is generate points along a line at supplied distance.  I had never really looked into doing it in QGIS until this question poped up on  This is a quick blog post because I thought it was a pretty handy little thing to do.

In the development version there is a new method on QgsGeometry called interpolate. This method takes a distance and returns a point along a line at that distance. Perfect! We can then just wrap this in a loop and generate a point increasing the distance as we move along

from qgis.core import (QgsFeature, QgsGeometry,
                       QgsVectorLayer, QgsMapLayerRegistry,
from PyQt4.QtCore import QVariant
from qgis.utils import iface

def createPointsAt(distance, geom):
    length = geom.length()
    currentdistance = distance
    feats = []

    while currentdistance < length:
        # Get a point along the line at the current distance
        point = geom.interpolate(currentdistance)
        # Create a new QgsFeature and assign it the new geometry
        fet = QgsFeature()
        fet.setAttributeMap( { 0 : currentdistance } )
        # Increase the distance
        currentdistance = currentdistance + distance

    return feats

def pointsAlongLine(distance):
    # Create a new memory layer and add a distance attribute
    vl = QgsVectorLayer("Point", "distance nodes", "memory")
    pr = vl.dataProvider()
    pr.addAttributes( [ QgsField("distance", QVariant.Int) ] )
    layer = iface.mapCanvas().currentLayer()
    # Loop though all the selected features
    for feature in layer.selectedFeatures():
        geom = feature.geometry()
        features = createPointsAt(distance, geom)


The above code might look a bit scary at first if you have never done any Python/pyqgis but hopefully the comments will ease the pain a little. The main bit is the createPointsAt function.

Cool! If we want to use this we can just stick it in a file in the .qgis/python folder (lets call it and then run import pointtools in the python console.

So lets have a go. First select some objects then run the following in the Python Console

import pointtools

That will generate a point every 40 meters along then selected lines

Distance nodes along line in qgis-dev

To generate nodes along different lines, select the new features, then call pointtools.pointsAlongLine(40) in the Python console.

Simple as that!

(Who knows, someone (maybe me) might even add this as a core object function in QGIS in the future)

Filed under: Open Source, qgis Tagged: language python, Open Source, osgeo, pyqgis, python, qgis, qgis-tips, Quantum GIS, tips

Kursprogramm Herbst 2012

Sourcepole bietet Grundlagen- und Aufbau-Kurse für den Betrieb von Geodaten-Infrastrukturen auf der Basis von PostgreSQL/PostGIS und Quantum GIS an. Detaillierte Informationen zu den Kursen, die im Herbst 2012 stattfinden, entnehmen Sie bitte dem Kursprogramm. Die Anmeldung ist ab sofort online möglich. Wir freuen uns darauf Sie in Zürich begüssen zu können.

A new sphinx theme

During the Zurich QGIS hackfest we had some extended discussions about migrating our documentation away from LaTeX to sphinx because the latter offers a more approachable syntax for casual documentation writers and has good support for internationalisation via gettext. This week I am going to our first 2012 QGIS hackfest (to be held in Lyon,... Read more »

A new sphinx theme

During the Zurich QGIS hackfest we had some extended discussions about migrating our documentation away from LaTeX to sphinx because the latter offers a more approachable syntax for casual documentation writers and has good support for internationalisation via gettext. This week I am going to our first 2012 QGIS hackfest (to be held in Lyon, France) and we will be spending some time and effort to move forward with the migration to sphinx. In order to get familiar with sphinx, I have slowly been moving my own documentation work from txt2tags (which I love for its simplicity, but it is not great for complex documents that need interdocument references) to sphinx (which is a little more complex but has many more bells and whistles). Sphinx provides a number of themes 'out of the box'  for generated html content, but I don't really like any of them much and I want my stuff to look a little more unique. So I have dipped my toes a little into the waters of sphinx theme creation. This weekend I decided to make my efforts public. There are still a number of layout issues I want to address (mainly in the TOC sidebar), and I want to add some more explicit markup for various sphinx constructs, but what I have is already usable and hopefully interesting for others to try out. Here is a little screenshot of what the theme looks like:

Linfiniti Sphinx Theme (click for larger image)

You can download the theme from its github project page (  - the accompanying README should give you enough info to get started with it. I am hoping that we can use a variant of this theme for the QGIS project documentation too. I look forward to any patches to make the theme better any of you html/css gurus out there may have to offer!

A new QGIS plugin: Python Script Runner

Gary Sherman has just published a new Python plugin for QGIS that I think people will find very handy, I know I will.  The plugin allows you to run Python scripts inside QGIS for tasks that don’t really require, or warrant, a whole plugin.

Go check out Gray’s post about the new plugin at

The new plugin can be installed via the Plugin Installer using the “runner” or “script”.  The Plugin Installer is another one of my favorite plugins for QGIS, being able to push out a new plugin and know that everyone can get it is a good feeling :)

Filed under: Open Source, qgis Tagged: FOSSGIS, gis, Open Source, osgeo, plugin, python, qgis, Quantum GIS

Script Runner: A Plugin to Run Python Scripts in QGIS

Following up on my last post, Running Scripts in the Python Console, I created a plugin to simplify running scripts:

The Script Runner plugin allows you to add your scripts to a list so they are readily available. You can then run them to automate QGIS tasks and have full access to the PyQGIS API. In addition, you can view information about the classes, methods, and functions in your module as well as browse the source:

In order for your script to work with ScriptRunner it has to implement a single function as an entry point. Here is some additional information from the Help tab of the plugin:


In order for Script Runner to execute your script you must define a run_script function that accepts a single argument. This is the standard entry point used by Script Runner. A reference to the qgis.utils.iface object will be passed to your run_script function. You don’t have to use the iface object in your script but your run_script function must accept it as an argument.

Here is an example of a simple run_script function:

    def run_script(iface):
        ldr = Loader(iface)

In this example, the run_script creates an instance (ldr) of a class named Loader that is defined in the same source file. It then calls a method in the Loader class named load_shapefiles to do something useful—in this case, load all the shapefiles in a specified directory.

Alternatively, you could choose not to use classes and just do everything within the run_script function, including having it call functions in the same script or others you might import. The important thing is to be sure you have defined a run_script function. If not, Script Runner won’t load your script.

Working with Scripts

To run a script, you must add it to Script Runner using the Add Script tool on the toolbar. This will add it to a list in the left panel. This list of scripts is persisted between uses of QGIS. You can remove a script using the Remove Script tool. This just removes it from the list; it does nothing to the script file on disk.

Once you have a script loaded, you can click the Script Info tool to populate the Info and Source tabs in the panel on the right. The Info tab contains the docstring from your module and then a list of the classes, methods, and functions found in the script. Having a proper docstring at the head of your script will help you determine the puprose of script.

You can view the source of the script on the Source tab. This allows you to quickly confirm that you are using the right script and it does what you think it will.

Installing the Plugin

To install the plugin:

  1. Open the Python plugin installer: Plugins->Fetch Python Plugins
  2. Check to see if you have the new Official repository in your list of plugins by clicking on Repositories tab. The URL is
  3. If you have it, skip to step 5. If the new repository isn’t in the list, add it by clicking the Add button. Give it a name and insert the URL
  4. Click on the Plugins tab
  5. Enter scriptrunner in the Filter box
  6. Select the ScriptRunner plugin and click Install

ScriptRunner adds an entry to the Plugins menu as well as a tool on the Plugins toolbar: . Click it and you are off and running.

QGIS: Running Scripts in the Python Console

The QGIS Python console is great for doing one-off tasks or experimenting with the API. Sometimes you might want to automate a task using a script, and do it without writing a full blown plugin. Currently QGIS does not have a way to load an arbitrary Python script and run it1. Until it does, this post illustrates a way you can create a script and run it from the console.

There are a couple of requirements to run a script in the console:

  1. The script must be in your PYTHONPATH
  2. Just like a QGIS plugin, the script needs a reference to qgis.utils.iface

Setting up the Environment

By default, the Python path includes the .qgis/python directory. The location depends on your platform:

  • Windows: in your home directory under .qgis\python. For example, C:\Documents and Settings\gsherman\.qgis\python
  • Linux and OS X: $HOME/.qgis/python

To see what is in your PYTHONPATH you can do the following in QGIS Python console:

import sys

While you could use the .qgis\python directory for your custom scripts, a better way is to create a directory specifically for that purpose and add that directory to the PYTHONPATH environment variable. On Windows you can do this using the Environment Variables page in your system properties:

On Linux or OS X, you can add it to your .bash_profile, .profile, or other login script in your home directory:

export PYTHONPATH=$PYTHONPATH:/home/gsherman/qgis_scripts

Writing the Script

With the environment set, we can create scripts to automate QGIS tasks and run them from the console. For this example, we will use a simple script to load all shapefiles in a specified directory. There are a couple of ways to do this:

  1. Write a simple script with a function that accepts qgis.utils.iface as an argument, along with a path to the shapefiles
  2. Create a Python class that uses an __init__ method to store a reference to the iface object and then add methods to do the work

We will use the latter approach because it is more flexible and allows us to initialize once and then call methods without having to pass the iface object each time.

The script looks like this:

#!/usr/bin/env Python
"""Load all shapefiles in a given directory.
  This script ( runs from the QGIS Python console.
  From the console, use:
    from loader import Loader
    ldr = Loader(qgis.utils.iface)
from glob import glob
from os import path
class Loader:
    def __init__(self, iface):
        """Initialize using the qgis.utils.iface 
        object passed from the console.
        self.iface = iface
    def load_shapefiles(self, shp_path):
        """Load all shapefiles found in shp_path"""
        print "Loading shapes from %s" % path.join(shp_path, "*.shp")
        shps = glob(path.join(shp_path, "*.shp"))
        for shp in shps:
            (shpdir, shpfile) = path.split(shp)
            self.iface.addVectorLayer(shp, shpfile, 'ogr' )

Running the Script

To open the console use the Plugins->Python Console menu item.

The comment at the head of the script explains how to use it.

First we import the Loader class from the script file (named This script resides in the qgis_scripts directory that is our PYTHONPATH.

from loader import Loader

We then create an instance of Loader, passing it the reference to the iface object:

ldr = Loader(qgis.utils.iface)

This creates the Loader object and calls the __init__ method to initialize things.

Once we have an instance of Loader we can load all the shapefiles in a directory by calling the load_shapefiles method, passing it the full path to the directory containing the shapefiles:


The load_shapefiles method uses the path to get a list of all the shapefiles and then adds them to QGIS using addVectorLayer.

Here is the result, rendered in the random colors and order that the shapefiles were loaded:

Some Notes

  • When testing a script in the console you may need to reload it as you make changes. This can be done using reload and the name of the module. In our example, reload(loader) does the trick.
  • You can add more methods to your class to do additional tasks
  • You can create a “driver” script that accepts the iface object and then initializes additional classes to do more complex tasks

1. I have plans on the drawing board to implement this feature.

QGIS Plugin of the Week: OpenLayers

This week we look at the OpenLayers plugin for QGIS. This plugin allows you to add a number of image services to your map canvas:

  • Google
    • Physical
    • Streets
    • Hybrid
    • Satellite
  • OpenStreetMap
  • Yahoo
    • Street
    • Hybrid
    • Satellite
  • Bing
    • Road
    • Aerial
    • Aerial with labels

Installing the Plugin

The OpenLayers plugin is installed like all other Python plugins. From the the Plugins menu in QGIS, choose Fetch Python Plugins. This brings up the plugin installer. To find the plugin, enter openlayers in the Filter box, then select OpenLayers Plugin from the list. Once it’s highlighted, click the Install plugin button. This will download the plugin from the repository, install it, and load it into QGIS.

Using the Plugin

The OpenLayers Plugin uses your view extent to fetch the data from the service you choose. For this reason you should load at least one of your own layers first. Since each of the services are expecting a request in latitude/longitude your layer either has to be geographic or you must enable on the fly projection.

To add one of the services you have two choices; you can pick the service from the Plugins->OpenLayers plugin menu or you can use the OpenLayers Overview. The Overview opens a new panel that allows you to choose a service from a drop-down list. Click the Enable map checkbox to enable the drop-down list and preview the service you want to add. If you are happy with what you see, you can add it to the map by clicking the Add map button.

In the screenshot below we have enabled the Overview panel, added the world boundaries layer1, zoomed to an area of interest, and added the Google terrain (physical) data:

You can add as many services as you want, previewing them using the OpenLayers Overview panel.

1 You can get the world boundaries layer from the Geospatial Desktop sample data set.

QGIS Plugin of the Week: Profile

This week we take a look at a how to plot a terrain profile using the Profile plugin. The plugin can be used with any raster format supported by QGIS. You can can display profiles from up to three rasters at once, allowing you to compare the results. To illustrate, we’ll create a simple profile using a DEM of a 1:63,360 quadrangle in Alaska.

Installing the Plugin

The Profile plugin is installed like all other Python plugins. From the the Plugins menu in QGIS, choose Fetch Python Plugins. This brings up the plugin installer. To find the plugin, enter profile in the Filter box, then select Profile from the list. Once it’s highlighted, click the Install plugin button. This will download the plugin from the repository, install it, and load it into QGIS.1

Using the Plugin

Here we have loaded the DEM as well as a raster (DRG) of the topography for the same quadrangle and zoomed in a bit to an area of interest:

The yellow line has been added to indicate where we will take the profile. While the Profile plugin allows you to interactively select the profile line it doesn’t display the line on the map.

To create a profile, first make sure the raster you want to profile is selected in the layer list, then activate the plugin from the Plugins toolbar by clicking on it. The cursor becomes a cross that you use to create the profile line: click at the start point, move to the end and click again. Once you have created the profile line, the plugin pops up the result:

The profile is taken from the northeast towards the southwest, based on where we clicked first. You can change the exaggeration of the profile by using the slider on the left. The X axis shows the distance along the profile line in map units and the Y axis shows the cell values from the raster—in this case, elevation in meters.

You can save the result as a PDF or SVG using the buttons at the bottom of the dialog.

The Statistics tab displays some information for the raster layer and the profile line:

If we had additional rasters loaded, we could use the Setup tab to add them to the profile analysis. This would display the results using the colors specified for each layer and we could compare them on the Profile tab.

This is just one of several QGIS plugins that deal with profiles. You can check out the rest of them using the Python Plugin installer.

1 The Profile plugin requires the Python module for QWT5. If you don’t have this installed, a warning will be displayed during the installation process.

QGIS Plugin of the Week: Points to Paths

This week we highlight the Points to Paths plugin, a handy way to convert a series of points into line features. This plugin lets you “connect the dots” based on an common attribute and a sequence field. The attribute field determines which points should be grouped together into a line. The sequence field determines the order in which the points will connected. The output from this plugin is a shapefile.

Let’s take a look at some example data. Here we have some fictional wildlife tracking data for two moose. The tracking data is in shapefile format, but you can use any vector format supported by QGIS. The tracking data is symbolized by our two animals: Moose1 and Moose2:

The moose_tracks layer has an animal tracking id field (animal_tid) and a sequence field (id). This is all we need to convert the individual observations into a line feature that represents the animals path.

Installing the Plugin

The Points to Paths plugin is installed like all other Python plugins. From the the Plugins menu in QGIS, choose Fetch Python Plugins. This brings up the plugin installer. To find the plugin, enter points to in the Filter box, then select Points to Paths from the list. Once it’s highlighted, click the Install plugin button. This will download the plugin from the repository, install it, and load it into QGIS.

Using the Plugin

Let’s convert the tracking data to paths. To get started, choose Plugins->Points to Paths from the menu or click on the Points to Paths tool on the Plugin toolbar. This brings up the PointsToPaths dialog box where we specify the paramaters needed to create the paths. Below is the completed dialog for our moose tracks:

The first drop-down box contains a list of the vector layers loaded in QGIS—in our case we have just one: moose_tracks. For the group field drop-down we chose the field that contains the tracking identifier for each animal. This determines which points will be selected and grouped to form an individual line. The point order field drop-down specifies the field that contains the ordering for the observations. In this case, the id field is incremented with each observation and can be used to construct the paths. We don’t have a date/time field in this data, but your observations may be sequenced in this way. The Python date format field allows you to specify a format string so the plugin can determine how to sequence your points based on date/time.

The last thing we need to specify is the output shapefile. You can do this by typing in the full path to a new shapefile or by using the Browse button.

With these options set, clicking the OK button will create the new shapefile containing the paths created from our point observations. Once the shapefile is created, the plugin gives you the option to add the new shapefile directly to QGIS.

The Result

The result of our simple example are shown below:

We symbolized the individual tracks using the Categorized renderer on the Style tab of the vector properties dialog. You can see we now have a track for each animal. The attribute table created by the plugin contains the following fields:

  • group – the name of the animal taken from the field we chose as the group field
  • begin – the value of the first point order field used to create the path
  • end – the value of the last point order field used to create the path

In our data, group contains the animal name, begin the value of the lowest id field for animal, and end contains the greatest id value. In a more typical data set, begin and end would contain the start and end date/time values for the observation. Labeling the observation points with our sequence field or date/time values would allow us to determine the direction of movement.

If you have point data that represent a movement of an object, this plugin is a great way to convert it into a path that can be used for visualization, analysis, or map composition.

QGIS Plugin of the Week: Time Manager

QGIS has a lot of plugins, including over 180 that have been contributed by users. If you aren’t using plugins, you are missing out on a lot that QGIS has to offer. I’m starting what I hope to be a regular feature: Plugin of the Week. This week we’ll take a look at Time Manager.

Time Manger and QGIS Users
Time Manager lets you browse spatial data that has a temporal component. Essentially this includes anything that changes location through time. Examples include:

  • Wildlife tracking
  • Vehicles
  • Storm centers
  • QGIS users

Data Preparation

Expanding on our last post about QGIS Users Around the World, we’ll use Time Manager to watch access to the QGIS Python plugin repository through time. If you refer to the previous post, you’ll see that all the IP addresses contacting the repository were extracted from the web server log and geocoded to get the approximate geographic coordinates. To use Time Manager all we need is the time for each access to the repository.

A important part (for our purpose) of the web server log entry looks like this: - - [23/Oct/2011:21:17:54 +0000] "GET /repo/contributed HTTP/1.1" 200 256

Time Manager supports date/time in the following formats:


As you can see, this doesn’t work with the format in the web server log.

The geocoding process created a file containing IP address, country, city (where available), latitude and longitude. This file is used to create a Python dictionary to look up locations by IP address. Using this file and a bit of Python, the web server log entries were converted into a CSV file containing:

ip,date_time,country,latitude,longitude,2011-10-23 21:13:53,United States,61.2149,-149.258,2011-10-23 21:14:22,United States,61.2149,-149.258,2011-10-23 21:17:54,United States,61.2149,-149.258,2011-10-23 21:18:04,United States,61.2149,-149.258

The CSV file was first converted to a shapefile using the QGIS Delimited Text plugin. Performance with Time Manager was somewhat slow using a shapefile containing 134,171 locations. The shapefile was imported into a SpatiaLite database (you can do this using ogr2ogr or the SpatiaLite GUI).

Using Time Manager

To display the progression of access to the repository (and thus users of QGIS), we first have to have the Time Manager plugin installed.[1] Once installed, we enable it using the Plugin Manger.

As you can see from the screenshot above, Time Manager installs a new panel in QGIS that sits below the map canvas. You can set a number of options by clicking Settings; the most important being the layer to use in the visualization. For the QGIS users, we use the time_manager_req layer that was created from the web server logs. With the location and date/time data in the proper format, you can click the “Play” button to start the display. For each time interval, the plugin selects the appropriate entries and displays a frame for the duration specified in the settings.

You can use the time slider to move around or move the time interval forward or backward using the buttons on each end of the slider.

A really nice feature is the ability to export to video. At present this saves a PNG file and world file for each time interval. You can then use another software package to combine these to create a video animation of the time sequence. Once solution is to use mencoder[1]:

mencoder "mf://*.PNG" -mf fps=10 -o output.avi -ovc lavc -lavcopts vcodec=mpeg4

Putting all this together gives us the following visualization of QGIS user activity from October 23 through December 19, 2011:

QGIS User Activity

You can see the “wave” of activity progress from east to west as the daylight hours come and go.


If you have spatial data with a date or time component, the Time Manager plugin provides a convenient way to visualize the temporal relationships.


  • Page 1 of 2 ( 30 posts )
  • >>
  • python

Back to Top