Created
October 5, 2016 16:09
-
-
Save marciuz/84e8c7c9ab87fc37589127965896fc91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
*************************************************************************** | |
QGIS Server Plugin Filters: this test filter adds a CSS to HTML | |
get feature info response. | |
--------------------- | |
Date : April 2015 | |
Copyright : (C) 2015 by Alessandro Pasotti | |
Email : apasotti at gmail dot com | |
*************************************************************************** | |
* * | |
* This program is free software; you can redistribute it and/or modify * | |
* it under the terms of the GNU General Public License as published by * | |
* the Free Software Foundation; either version 2 of the License, or * | |
* (at your option) any later version. * | |
* * | |
*************************************************************************** | |
""" | |
import os | |
from qgis.server import * | |
from qgis.core import * | |
from PyQt4.QtCore import * | |
from PyQt4.QtGui import * | |
import xmltodict, json | |
class GetFeatureInfoJSONFilter(QgsServerFilter): | |
def __init__(self, serverIface): | |
super(GetFeatureInfoJSONFilter, self).__init__(serverIface) | |
def requestReady(self): | |
"""Nothing to do here, but it would be the ideal point | |
to alter request **before** it gets processed, for example | |
you could set INFO_FORMAT to text/xml to get XML instead of | |
HTML in responseComplete""" | |
request = self.serverInterface().requestHandler() | |
params = request.parameterMap( ) | |
# request.setParameter('INFO_FORMAT', 'application/json') | |
pass | |
def responseComplete(self): | |
request = self.serverInterface().requestHandler() | |
params = request.parameterMap( ) | |
QgsMessageLog.logMessage("GetFeatureInfoJSON.responseComplete") | |
QgsMessageLog.logMessage( request.tostring()) | |
QgsMessageLog.logMessage(request) | |
if (params.get('SERVICE').upper() == 'WMS' \ | |
and params.get('REQUEST', '').upper() == 'GETCAPABILITIES' \ | |
and not request.exceptionRaised() ): | |
body = request.body() | |
# QgsMessageLog.logMessage("GetFeatureInfoJSON.responseComplete.enterCase") | |
body.replace('<GetFeatureInfo>', "<GetFeatureInfo>\n <Format>application/json</Format>") | |
request.clearBody() | |
request.appendBody(body) | |
elif (params.get('SERVICE').upper() == 'WMS' \ | |
and params.get('REQUEST', '').upper() == 'GETFEATUREINFO' \ | |
and params.get('INFO_FORMAT', '').upper() == 'APPLICATION/JSON' \ | |
and not request.exceptionRaised() ): | |
QgsMessageLog.logMessage("GetFeatureInfoJSON.responseComplete.enterCaseGETFEATUREINFO") | |
body = request.body() | |
o = xmltodict.parse(str(body)) | |
out_json = json.dumps(o) # '{"e": {"a": ["text", "text"]}}' | |
QgsMessageLog.logMessage(str(out_json)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
I've just install your plugin. But I've got the following error
Feature info format 'application/json' is not supported. Possibilities are 'text/plain', 'text/html' or 'text/xml'.
How can I fix this.
Regards