QGIS Server Python Plugins

Alessandro Pasotti (a.pasotti@itopen.it)

QGIS Server

QGIS Server is an open source WMS 1.3 and WFS 1.0.0 and WCS implementation whith advanced cartographic features for thematic mapping.

Starting from version 1.8 QGIS Server has Python plugins support.

Python plugin Goals

Not goals

Possible applications

Standard flow without plugins

images/qgis-server-stdflow.png

Observer hooks

Server plugins register one or more QgsServerFilters that "listen to signals". Plugin filters receive the request/response objects and they can manipulate them with the following methods:

Observer flowchart

images/qgis-server-pluginflow.png

Observer details

requestReady

This is called when the request is ready: incoming URL and query string have been parsed.

This is the point where you can manipulate the input and perform actions like:

sendResponse

This is called whenever output is sent to FCGI stdout (and from there, to the client).

SendResponse is the best place for direct manipulation of core service’s output and while responseComplete is typically also an option, sendResponse is the only viable option in case of streaming services.

responseComplete

This is called once when the response is ready to be sent to the client.

responseComplete is the ideal place to provide new services implementation (WPS or custom services) and to perform direct manipulation of the output coming from core services (for example to add a watermark upon a WMS image).

Plugin example (metadata)

A server plugin must set server metadata

metadata.txt:

server=True

Plugin example (__init__)

A server plugin initialisation is similar to a desktop plugin, in __init__.py

def serverClassFactory(serverIface):
    from SimpleServer import SimpleServer
    return SimpleServer(serverIface)

Plugin example (SimpleServer)

# Import server and core
from qgis.server import *
from qgis.core import *

class SimpleServer:
    def __init__(self, serverIface):
        serverIface.registerFilter( \
            SimpleHelloFilter(serverIface)
            , 100 )

Plugin example (SimpleHelloFilter)

class SimpleHelloFilter(QgsServerFilter):
    def requestReady(self):
        QgsMessageLog.logMessage("requestReady")

    def sendResponse(self):
        QgsMessageLog.logMessage("sendResponse")

    def responseComplete(self):
        QgsMessageLog.logMessage("responseComplete")

Plugin example (responseComplete)

def responseComplete(self):
    request = self.serverInterface().requestHandler()
    params = request.parameterMap()
    if params.get('SERVICE', '').upper() == 'SIMPLE':
        request.clearHeaders()
        request.setHeader('Content-type', 'text/plain')
        request.clearBody()
        request.appendBody('HelloServer!')

QgsServerInterface

This is the server equivalent of desktop's iface

To GUI or not to GUI?

A Server plugin can have (not must) a desktop interface to configure itself

One-click install and deployment

Example(s)

An example plugin with a few example filters is available:

https://github.com/elpaso/qgis-helloserver

Example query string:

http://localhost/cgi-bin/qgis_mapserv.fcgi?SERVICE=HELLO&REQUEST=SayHello

Restrictions

If the plugin has a Desktop interface it cannot usually access to user's QSettings, this means that plugins options have to be stored somewhere else in order to be accessible by the server side.

Thanks

Thanks to

  • Martin Dobias
  • Marco Hugentobler