├── HelloServer.py ├── README.rst ├── __init__.py ├── filters ├── ExceptionFilter.py ├── GetFeatureInfoCSSFilter.py ├── HelloFilter.py ├── ParamsFilter.py ├── WatermarkFilter.py ├── __init__.py └── media │ ├── RemoteConsole.html │ └── watermark.png └── metadata.txt /HelloServer.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | *************************************************************************** 5 | HelloServer.py 6 | --------------------- 7 | Date : August 2014 8 | Copyright : (C) 2014-2015 by Alessandro Pasotti 9 | Email : apasotti at gmail dot com 10 | *************************************************************************** 11 | * * 12 | * This program is free software; you can redistribute it and/or modify * 13 | * it under the terms of the GNU General Public License as published by * 14 | * the Free Software Foundation; either version 2 of the License, or * 15 | * (at your option) any later version. * 16 | * * 17 | *************************************************************************** 18 | """ 19 | 20 | __author__ = 'Alessandro Pasotti' 21 | __date__ = 'August 2014' 22 | __copyright__ = '(C) 2014, Alessandro Pasotti - ItOpen' 23 | 24 | import sys 25 | import os 26 | 27 | from qgis.server import * 28 | from qgis.core import * 29 | 30 | from . import filters 31 | 32 | """ 33 | HTTP_AUTH BASIC 34 | May need proper fcgid apache configuration: 35 | 36 | RewriteEngine On 37 | 38 | RewriteCond %{HTTP:Authorization} . 39 | RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] 40 | 41 | 42 | """ 43 | 44 | DEFAULT_USERID = 'test' 45 | DEFAULT_PASSWORD = 'qgis' 46 | 47 | 48 | 49 | class HelloServerServer: 50 | """Test plugin for QGIS server 51 | this plugin loads all filters from the 'filters' directory and logs 52 | errors""" 53 | 54 | def __init__(self, serverIface): 55 | # Save reference to the QGIS server interface 56 | self.serverIface = serverIface 57 | priority = 1 58 | 59 | QgsMessageLog.logMessage("SUCCESS - HelloServer init", 'plugin', Qgis.Info) 60 | for filter_name in filters.local_modules: 61 | QgsLogger.debug("HelloServerServer - loading filter %s" % filter_name) 62 | try: 63 | serverIface.registerFilter( getattr(filters, filter_name)(serverIface), priority * 100 ) 64 | priority += 1 65 | except Exception as e: 66 | QgsLogger.debug("HelloServerServer - Error loading filter %s : %s" % (filter_name, e)) 67 | 68 | 69 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | This is an example Python plugin for QGIS Server. 2 | 3 | Python plugins support for QGIS Server has been introduced recently with QGIS 2.8 and it is enabled by default on most distributions. 4 | 5 | For more informations on how to use Python plugins for QGIS Server, please refer to a series of posts on our company website: 6 | 7 | http://www.itopen.it/category/gis/qgis/qgis-server/ 8 | 9 | 10 | Sample filters: 11 | 12 | Exception: 13 | http://localhost:8000/?SERVICE=EXCEPTION 14 | 15 | Watermark: any WMS GetMap 16 | 17 | HelloFilter: prints messages to the logs 18 | 19 | GetFeatureInfoCSS: injects CSS in GetFeatureInfo HTML response 20 | 21 | ParamsFilter: prints messages to the logs 22 | -------------------------------------------------------------------------------- /__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | This script initializes the plugin, making it known to QGIS. 4 | """ 5 | 6 | 7 | def serverClassFactory(serverIface): 8 | from .HelloServer import HelloServerServer 9 | return HelloServerServer(serverIface) 10 | 11 | -------------------------------------------------------------------------------- /filters/ExceptionFilter.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | *************************************************************************** 5 | ExceptionFilter: test filter for QgsMapServiceException 6 | --------------------- 7 | Date : August 2014 8 | Copyright : (C) 2014-2015 by Alessandro Pasotti 9 | Email : apasotti at gmail dot com 10 | *************************************************************************** 11 | * * 12 | * This program is free software; you can redistribute it and/or modify * 13 | * it under the terms of the GNU General Public License as published by * 14 | * the Free Software Foundation; either version 2 of the License, or * 15 | * (at your option) any later version. * 16 | * * 17 | *************************************************************************** 18 | """ 19 | 20 | 21 | from qgis.core import * 22 | from qgis.server import * 23 | 24 | 25 | class ExceptionFilter(QgsServerFilter): 26 | 27 | def __init__(self, serverIface): 28 | super().__init__(serverIface) 29 | 30 | def responseComplete(self): 31 | request = self.serverInterface().requestHandler() 32 | params = request.parameterMap() 33 | if params.get('SERVICE', '').upper() == 'EXCEPTION': 34 | QgsMessageLog.logMessage("ExceptionFilter.responseComplete exception raised!") 35 | # Not very "pythonic" way to raise exceptions but this allows for 36 | # a different path for wanted and unwanted exceptions: 37 | # * QgsMapServiceException will show in the service output as a ServiceExceptionReport XML document 38 | # * unhandled unwanted exceptions will only show up in the server logs 39 | request.setServiceException(QgsServerException('ExceptionFilter: Test exception raised from ExceptionFilter')) 40 | 41 | -------------------------------------------------------------------------------- /filters/GetFeatureInfoCSSFilter.py: -------------------------------------------------------------------------------- 1 | 2 | # -*- coding: utf-8 -*- 3 | 4 | """ 5 | *************************************************************************** 6 | QGIS Server Plugin Filters: this test filter adds a CSS to HTML 7 | get feature info response. 8 | --------------------- 9 | Date : April 2015 10 | Copyright : (C) 2015 by Alessandro Pasotti 11 | Email : apasotti at gmail dot com 12 | *************************************************************************** 13 | * * 14 | * This program is free software; you can redistribute it and/or modify * 15 | * it under the terms of the GNU General Public License as published by * 16 | * the Free Software Foundation; either version 2 of the License, or * 17 | * (at your option) any later version. * 18 | * * 19 | *************************************************************************** 20 | """ 21 | 22 | 23 | from qgis.server import * 24 | 25 | class GetFeatureInfoFilter(QgsServerFilter): 26 | 27 | def __init__(self, serverIface): 28 | super(GetFeatureInfoFilter, self).__init__(serverIface) 29 | 30 | def responseComplete(self): 31 | handler = self.serverInterface().requestHandler() 32 | params = handler.parameterMap( ) 33 | 34 | if (params.get('SERVICE', '').upper() == 'WMS' \ 35 | and params.get('REQUEST', '').upper() == 'GETFEATUREINFO' \ 36 | and params.get('INFO_FORMAT', '').upper() == 'TEXT/HTML' \ 37 | and not handler.exceptionRaised() ): 38 | body = handler.body() 39 | body.replace(b'', b"""""") 40 | handler.clearBody() 41 | handler.appendBody(body) 42 | 43 | 44 | -------------------------------------------------------------------------------- /filters/HelloFilter.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | *************************************************************************** 5 | QGIS Server Plugin Filters: say hello test filter, just logs calls and 6 | prints HelloServer! on plain text response 7 | --------------------- 8 | Date : October 2014 9 | Copyright : (C) 2014-2015 by Alessandro Pasotti 10 | Email : apasotti at gmail dot com 11 | *************************************************************************** 12 | * * 13 | * This program is free software; you can redistribute it and/or modify * 14 | * it under the terms of the GNU General Public License as published by * 15 | * the Free Software Foundation; either version 2 of the License, or * 16 | * (at your option) any later version. * 17 | * * 18 | *************************************************************************** 19 | """ 20 | 21 | from qgis.server import * 22 | from qgis.core import * 23 | 24 | class HelloFilter(QgsServerFilter): 25 | 26 | def __init__(self, serverIface): 27 | super().__init__(serverIface) 28 | 29 | def requestReady(self): 30 | QgsMessageLog.logMessage("HelloFilter.requestReady") 31 | 32 | def sendResponse(self): 33 | QgsMessageLog.logMessage("HelloFilter.sendResponse") 34 | 35 | def responseComplete(self): 36 | QgsMessageLog.logMessage("HelloFilter.responseComplete") 37 | request = self.serverInterface().requestHandler() 38 | params = request.parameterMap() 39 | if params.get('SERVICE', '').upper() == 'HELLO': 40 | request.clear() 41 | request.setResponseHeader('Content-type', 'text/plain') 42 | request.appendBody(b'HelloServer!') 43 | -------------------------------------------------------------------------------- /filters/ParamsFilter.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | *************************************************************************** 5 | QGIS Server Plugin Filters: this test filter changes the SERVICE params 6 | in the request, tests that the parameter was changed and then restores 7 | it to its original value 8 | --------------------- 9 | Date : October 2014 10 | Copyright : (C) 2014-2015 by Alessandro Pasotti 11 | Email : apasotti at gmail dot com 12 | *************************************************************************** 13 | * * 14 | * This program is free software; you can redistribute it and/or modify * 15 | * it under the terms of the GNU General Public License as published by * 16 | * the Free Software Foundation; either version 2 of the License, or * 17 | * (at your option) any later version. * 18 | * * 19 | *************************************************************************** 20 | """ 21 | 22 | from qgis.server import * 23 | from qgis.core import * 24 | 25 | class ParamsFilter(QgsServerFilter): 26 | 27 | def __init__(self, serverIface): 28 | super(ParamsFilter, self).__init__(serverIface) 29 | 30 | def requestReady(self): 31 | request = self.serverInterface().requestHandler() 32 | params = request.parameterMap( ) 33 | request.setParameter('TEST_NEW_PARAM', 'ParamsFilter') 34 | 35 | def responseComplete(self): 36 | request = self.serverInterface().requestHandler() 37 | params = request.parameterMap( ) 38 | if params.get('TEST_NEW_PARAM') == 'ParamsFilter': 39 | QgsMessageLog.logMessage("SUCCESS - ParamsFilter.responseComplete") 40 | else: 41 | QgsMessageLog.logMessage("FAIL - ParamsFilter.responseComplete") 42 | -------------------------------------------------------------------------------- /filters/WatermarkFilter.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | *************************************************************************** 5 | QGIS Server Plugin Filters: this test filter adds a watermark image to 6 | WMS GetImage calls. 7 | --------------------- 8 | Date : October 2014 9 | Copyright : (C) 2014-2015 by Alessandro Pasotti 10 | Email : apasotti at gmail dot com 11 | *************************************************************************** 12 | * * 13 | * This program is free software; you can redistribute it and/or modify * 14 | * it under the terms of the GNU General Public License as published by * 15 | * the Free Software Foundation; either version 2 of the License, or * 16 | * (at your option) any later version. * 17 | * * 18 | *************************************************************************** 19 | """ 20 | 21 | import os 22 | 23 | from qgis.server import * 24 | from qgis.core import * 25 | from qgis.PyQt.QtCore import * 26 | from qgis.PyQt.QtGui import * 27 | 28 | 29 | class WatermarkFilter(QgsServerFilter): 30 | 31 | def __init__(self, serverIface): 32 | super().__init__(serverIface) 33 | 34 | def responseComplete(self): 35 | request = self.serverInterface().requestHandler() 36 | params = request.parameterMap( ) 37 | # Do some checks 38 | if (params.get('SERVICE').upper() == 'WMS' \ 39 | and params.get('REQUEST').upper() == 'GETMAP' \ 40 | and not request.exceptionRaised() ): 41 | QgsMessageLog.logMessage("WatermarkFilter.responseComplete: image ready %s" % request.parameter("FORMAT")) 42 | # Get the image 43 | img = QImage() 44 | img.loadFromData(request.body()) 45 | # Adds the watermark 46 | watermark = QImage(os.path.join(os.path.dirname(__file__), 'media/watermark.png')) 47 | p = QPainter(img) 48 | p.drawImage(QRect( 20, 20, 40, 40), watermark) 49 | p.end() 50 | ba = QByteArray() 51 | buffer = QBuffer(ba) 52 | buffer.open(QIODevice.WriteOnly) 53 | img.save(buffer, "PNG" if "png" in request.parameter("FORMAT") else "JPG") 54 | # Set the body 55 | request.clearBody() 56 | request.appendBody(ba) 57 | 58 | 59 | -------------------------------------------------------------------------------- /filters/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | """ 4 | *************************************************************************** 5 | QGIS Server Plugin Filters - test filters. This init file will load 6 | all filter modules (matching *Filter.py in the current folder) 7 | --------------------- 8 | Date : October 2014 9 | Copyright : (C) 2014 by Alessandro Pasotti 10 | Email : apasotti at gmail dot com 11 | *************************************************************************** 12 | * * 13 | * This program is free software; you can redistribute it and/or modify * 14 | * it under the terms of the GNU General Public License as published by * 15 | * the Free Software Foundation; either version 2 of the License, or * 16 | * (at your option) any later version. * 17 | * * 18 | *************************************************************************** 19 | """ 20 | 21 | __all__ = ['FilterTestBase'] 22 | 23 | import pkgutil 24 | import inspect 25 | import os 26 | import glob 27 | 28 | modules = glob.glob(os.path.dirname(__file__)+"/*Filter.py") 29 | local_modules = [ os.path.basename(f)[:-3] for f in modules] 30 | 31 | 32 | for loader, name, is_pkg in pkgutil.walk_packages(__path__): 33 | module = loader.find_module(name).load_module(name) 34 | 35 | for name, value in inspect.getmembers(module): 36 | if not name in local_modules: 37 | continue 38 | globals()[name] = value 39 | __all__.append(name) 40 | -------------------------------------------------------------------------------- /filters/media/RemoteConsole.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | QGIS Server Python Console 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 23 | 24 | 64 | 65 | 66 | 67 |
68 |
69 |
70 |

71 | QGIS LOGO 72 | QGIS Server Python Console

73 |
74 | 75 |
76 | 77 |
78 |
79 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 142 | 143 | 144 | -------------------------------------------------------------------------------- /filters/media/watermark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/elpaso/qgis-helloserver/42d0b0bc45fe8bc03cb93214abe97f2966588429/filters/media/watermark.png -------------------------------------------------------------------------------- /metadata.txt: -------------------------------------------------------------------------------- 1 | [general] 2 | name=HelloServer 3 | qgisMinimumVersion=3 4 | description=This is a test plugin for the QGIS MapServer 5 | version=version 1.2 6 | author=Alessandro Pasotti (ItOpen) 7 | email=apasotti@gmail.com 8 | ; if True it's a server plugin 9 | server=True 10 | 11 | changelog= 12 | 13 | 14 | tags=hello world, test, server 15 | 16 | tracker=https://github.com/elpaso/qgis-helloserver 17 | homepage=https://github.com/elpaso/qgis-helloserver 18 | repository=https://github.com/elpaso/qgis-helloserver 19 | 20 | category=server 21 | 22 | experimental=True 23 | 24 | about=This is a test plugin *** DO NOT USE IN PRODUCTION!!! ***. It provides various 25 | testing methods, see https://github.com/elpaso/qgis-helloserver/blob/master/README.rst 26 | 27 | 28 | --------------------------------------------------------------------------------