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.
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:
create request handler
call plugins requestReady filters
store the byte stream and content type in the request handler
call plugin responseComplete
call plugin sendResponse
request handler outputs the response
Extreme tip!
You could even substitute a core service completely by changing SERVICE parameter and hence bypassing the core service (not that this make much sense though).
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:
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.
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).
A server plugin must set server metadata
metadata.txt:
server=True
A server plugin initialisation is similar to a desktop plugin, in __init__.py
def serverClassFactory(serverIface):
from SimpleServer import SimpleServer
return SimpleServer(serverIface)
# Import server and core
from qgis.server import *
from qgis.core import *
class SimpleServer:
def __init__(self, serverIface):
serverIface.registerFilter( \
SimpleHelloFilter(serverIface)
, 100 )
class SimpleHelloFilter(QgsServerFilter):
def requestReady(self):
QgsMessageLog.logMessage("requestReady")
def sendResponse(self):
QgsMessageLog.logMessage("sendResponse")
def responseComplete(self):
QgsMessageLog.logMessage("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!')
This is the server equivalent of desktop's iface
getEnv()
registerFilter()
A Server plugin can have (not must) a desktop interface to configure itself
One-click install and deployment
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
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 to
- Martin Dobias
- Marco Hugentobler