Merge pull request #7859 from rldhont/fix-server-sld-param-support

[Bugfix][Server] Add WMS SLD parameter support
This commit is contained in:
rldhont 2018-09-29 18:10:54 +02:00 committed by GitHub
commit ad7d03c64f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 307 additions and 26 deletions

View File

@ -138,6 +138,28 @@ Converts the parameter into a color.
:param ok: True if there's no error during the conversion, false otherwise
:return: A color
%End
QUrl toUrl( bool &ok ) const;
%Docstring
Converts the parameter into an url.
:param ok: True if there's no error during the conversion, false otherwise
:return: An url
.. versionadded:: 3.4
%End
QString loadUrl( bool &ok ) const;
%Docstring
Loads the data associated to the parameter converted into an url.
:param ok: True if there's no error during the load, false otherwise
:return: The content loaded
.. versionadded:: 3.4
%End
static void raiseError( const QString &msg );

View File

@ -154,20 +154,6 @@ void QgsRequestHandler::setupParameters()
{
const QgsServerRequest::Parameters parameters = mRequest.parameters();
// SLD
QString value = parameters.value( QStringLiteral( "SLD" ) );
if ( !value.isEmpty() )
{
QgsMessageLog::logMessage( QStringLiteral( "http and ftp methods not supported with Qt5." ) );
}
// SLD_BODY
value = parameters.value( QStringLiteral( "SLD_BODY" ) );
if ( ! value.isEmpty() )
{
mRequest.setParameter( QStringLiteral( "SLD" ), value );
}
//feature info format?
QString infoFormat = parameters.value( QStringLiteral( "INFO_FORMAT" ) );
if ( !infoFormat.isEmpty() )

View File

@ -17,6 +17,12 @@
#include "qgsserverparameters.h"
#include "qgsserverexception.h"
#include "qgsnetworkcontentfetcher.h"
#include "qgsmessagelog.h"
#include <QObject>
#include <QUrl>
#include <QNetworkReply>
#include <QNetworkRequest>
//
// QgsServerParameterDefinition
@ -200,6 +206,92 @@ QgsRectangle QgsServerParameterDefinition::toRectangle( bool &ok ) const
return extent;
}
QString QgsServerParameterDefinition::loadUrl( bool &ok ) const
{
ok = true;
// Get URL
QUrl url = toUrl( ok );
if ( !ok )
{
return QString();
}
// fetching content
QgsNetworkContentFetcher fetcher;
QEventLoop loop;
QObject::connect( &fetcher, &QgsNetworkContentFetcher::finished, &loop, &QEventLoop::quit );
QgsMessageLog::logMessage(
QObject::tr( "Request started [url: %1]" ).arg( url.toString() ),
QStringLiteral( "Server" ) );
QNetworkRequest request( url );
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
request.setAttribute( QNetworkRequest::CacheSaveControlAttribute, true );
fetcher.fetchContent( request );
//wait until content fetched
loop.exec( QEventLoop::ExcludeUserInputEvents );
QNetworkReply *reply = fetcher.reply();
if ( !reply )
{
ok = false;
QgsMessageLog::logMessage(
QObject::tr( "Request failed [error: no reply - url: %1]" ).arg( url.toString() ),
QStringLiteral( "Server" ) );
return QString();
}
QVariant status = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute );
if ( !status.isNull() && status.toInt() >= 400 )
{
ok = false;
if ( reply->error() != QNetworkReply::NoError )
{
QgsMessageLog::logMessage(
QObject::tr( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
QStringLiteral( "Server" ) );
}
QVariant phrase = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute );
QgsMessageLog::logMessage(
QObject::tr( "Request error [status: %1 - reason phrase: %2] for %3" ).arg( status.toInt() ).arg( phrase.toString(), reply->url().toString() ),
QStringLiteral( "Server" ) );
return QString();
}
if ( reply->error() != QNetworkReply::NoError )
{
ok = false;
QgsMessageLog::logMessage(
QObject::tr( "Request failed [error: %1 - url: %2]" ).arg( reply->errorString(), reply->url().toString() ),
QStringLiteral( "Server" ) );
return QString();
}
QgsMessageLog::logMessage(
QObject::tr( "Request finished [url: %1]" ).arg( url.toString() ),
QStringLiteral( "Server" ) );
QString content = fetcher.contentAsString();
ok = ( !content.isEmpty() );
return content;
}
QUrl QgsServerParameterDefinition::toUrl( bool &ok ) const
{
ok = true;
QUrl val;
if ( !mValue.toString().isEmpty() )
{
val = mValue.toUrl();
}
ok = ( !val.isEmpty() && val.isValid() );
return val;
}
int QgsServerParameterDefinition::toInt( bool &ok ) const
{
ok = true;

View File

@ -137,6 +137,22 @@ class SERVER_EXPORT QgsServerParameterDefinition
*/
QColor toColor( bool &ok ) const;
/**
* Converts the parameter into an url.
* \param ok True if there's no error during the conversion, false otherwise
* \returns An url
* \since QGIS 3.4
*/
QUrl toUrl( bool &ok ) const;
/**
* Loads the data associated to the parameter converted into an url.
* \param ok True if there's no error during the load, false otherwise
* \returns The content loaded
* \since QGIS 3.4
*/
QString loadUrl( bool &ok ) const;
/**
* Raises an exception in case of an invalid parameters.
* \param msg The message describing the exception

View File

@ -19,6 +19,7 @@
#include "qgsdatasourceuri.h"
#include "qgsmessagelog.h"
#include <iostream>
#include <QUrl>
namespace QgsWms
{
@ -85,6 +86,36 @@ namespace QgsWms
return val;
}
QString QgsWmsParameter::loadUrl() const
{
// Check URL -- it will be used in error messages
const QUrl url = toUrl();
bool ok = false;
const QString content = QgsServerParameterDefinition::loadUrl( ok );
if ( !ok )
{
const QString msg = QString( "%1 request error for %2" ).arg( name( mName ), url.toString() );
QgsServerParameterDefinition::raiseError( msg );
}
return content;
}
QUrl QgsWmsParameter::toUrl() const
{
bool ok = false;
const QUrl url = QgsServerParameterDefinition::toUrl( ok );
if ( !ok )
{
raiseError();
}
return url;
}
QColor QgsWmsParameter::toColor() const
{
bool ok = false;
@ -336,6 +367,9 @@ namespace QgsWms
const QgsWmsParameter pSld( QgsWmsParameter::SLD );
save( pSld );
const QgsWmsParameter pSldBody( QgsWmsParameter::SLD_BODY );
save( pSldBody );
const QgsWmsParameter pLayer( QgsWmsParameter::LAYER );
save( pLayer );
@ -470,6 +504,16 @@ namespace QgsWms
: QgsWmsParameters()
{
load( parameters.urlQuery() );
const QString sld = mWmsParameters[ QgsWmsParameter::SLD ].toString();
if ( !sld.isEmpty() )
{
const QString sldBody = mWmsParameters[ QgsWmsParameter::SLD ].loadUrl();
if ( !sldBody.isEmpty() )
{
loadParameter( QgsWmsParameter::name( QgsWmsParameter::SLD_BODY ), sldBody );
}
}
}
bool QgsWmsParameters::loadParameter( const QString &key, const QString &value )
@ -1159,9 +1203,9 @@ namespace QgsWms
return mWmsParameters[ QgsWmsParameter::WMS_PRECISION ].toInt();
}
QString QgsWmsParameters::sld() const
QString QgsWmsParameters::sldBody() const
{
return mWmsParameters[ QgsWmsParameter::SLD ].toString();
return mWmsParameters[ QgsWmsParameter::SLD_BODY ].toString();
}
QStringList QgsWmsParameters::filters() const

View File

@ -132,6 +132,7 @@ namespace QgsWms
SYMBOLWIDTH,
OPACITIES,
SLD,
SLD_BODY,
FI_POLYGON_TOLERANCE,
FI_LINE_TOLERANCE,
FI_POINT_TOLERANCE,
@ -251,6 +252,22 @@ namespace QgsWms
*/
QColor toColor() const;
/**
* Converts the parameter into an url.
* \returns An url
* \throws QgsBadRequestException Invalid parameter exception
* \since QGIS 3.4
*/
QUrl toUrl() const;
/**
* Loads the data associated to the parameter converted into an url.
* \returns The content loaded
* \throws QgsBadRequestException Invalid parameter exception
* \since QGIS 3.4
*/
QString loadUrl() const;
/**
* Raises an error in case of an invalid conversion.
* \throws QgsBadRequestException Invalid parameter exception
@ -377,10 +394,10 @@ namespace QgsWms
QgsRectangle bboxAsRectangle() const;
/**
* Returns SLD if defined or an empty string.
* \returns sld
* Returns SLD_body if defined or an empty string.
* \returns sld body
*/
QString sld() const;
QString sldBody() const;
/**
* Returns the list of feature selection found in SELECTION parameter.

View File

@ -170,7 +170,7 @@ namespace QgsWms
QList<QgsMapLayer *> layers;
QList<QgsWmsParametersLayer> params = mWmsParameters.layersParameters();
QString sld = mWmsParameters.sld();
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
layers = sldStylizedLayers( sld );
else
@ -312,7 +312,7 @@ namespace QgsWms
restorer.reset( new QgsLayerRestorer( mNicknameLayers.values() ) );
// init stylized layers according to LAYERS/STYLES or SLD
QString sld = mWmsParameters.sld();
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
{
layers = sldStylizedLayers( sld );
@ -661,7 +661,7 @@ namespace QgsWms
restorer.reset( new QgsLayerRestorer( mNicknameLayers.values() ) );
// init stylized layers according to LAYERS/STYLES or SLD
QString sld = mWmsParameters.sld();
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
{
layers = sldStylizedLayers( sld );
@ -749,7 +749,7 @@ namespace QgsWms
restorer.reset( new QgsLayerRestorer( mNicknameLayers.values() ) );
// init stylized layers according to LAYERS/STYLES or SLD
QString sld = mWmsParameters.sld();
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
{
layers = sldStylizedLayers( sld );
@ -903,7 +903,7 @@ namespace QgsWms
restorer.reset( new QgsLayerRestorer( mNicknameLayers.values() ) );
// init stylized layers according to LAYERS/STYLES or SLD
QString sld = mWmsParameters.sld();
QString sld = mWmsParameters.sldBody();
if ( !sld.isEmpty() )
layers = sldStylizedLayers( sld );
else

View File

@ -1108,6 +1108,21 @@ class TestQgsServerWMSGetMap(QgsServerTestBase):
self._img_diff_error(r, h, "WMS_GetMap_Annotations")
def test_wms_getmap_sld(self):
import socketserver
import threading
import http.server
# Bring up a simple HTTP server
os.chdir(unitTestDataPath() + '')
handler = http.server.SimpleHTTPRequestHandler
httpd = socketserver.TCPServer(('localhost', 0), handler)
port = httpd.server_address[1]
httpd_thread = threading.Thread(target=httpd.serve_forever)
httpd_thread.setDaemon(True)
httpd_thread.start()
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
@ -1130,7 +1145,62 @@ class TestQgsServerWMSGetMap(QgsServerTestBase):
"REQUEST": "GetMap",
"VERSION": "1.1.1",
"SERVICE": "WMS",
"SLD": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ogc=\"http://www.opengis.net/ogc\" xsi:schemaLocation=\"http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd\" version=\"1.1.0\" xmlns:se=\"http://www.opengis.net/se\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"> <NamedLayer> <se:Name>db_point</se:Name> <UserStyle> <se:Name>db_point_style</se:Name> <se:FeatureTypeStyle> <se:Rule> <se:Name>Single symbol</se:Name> <ogc:Filter xmlns:ogc=\"http://www.opengis.net/ogc\"> <ogc:PropertyIsEqualTo> <ogc:PropertyName>gid</ogc:PropertyName> <ogc:Literal>1</ogc:Literal> </ogc:PropertyIsEqualTo> </ogc:Filter> <se:PointSymbolizer uom=\"http://www.opengeospatial.org/se/units/metre\"> <se:Graphic> <se:Mark> <se:WellKnownName>square</se:WellKnownName> <se:Fill> <se:SvgParameter name=\"fill\">5e86a1</se:SvgParameter> </se:Fill> <se:Stroke> <se:SvgParameter name=\"stroke\">000000</se:SvgParameter> </se:Stroke> </se:Mark> <se:Size>0.007</se:Size> </se:Graphic> </se:PointSymbolizer> </se:Rule> </se:FeatureTypeStyle> </UserStyle> </NamedLayer> </StyledLayerDescriptor>",
"SLD": "http://localhost:" + str(port) + "/qgis_local_server/db_point.sld",
"BBOX": "-16817707,-4710778,5696513,14587125",
"WIDTH": "500",
"HEIGHT": "500",
"LAYERS": "db_point",
"STYLES": "",
"FORMAT": "image/png",
"CRS": "EPSG:3857"
}.items())])
r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_SLD")
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetMap",
"LAYERS": "Country,db_point",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"HEIGHT": "500",
"WIDTH": "500",
"CRS": "EPSG:3857"
}.items())])
r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_SLDRestored")
httpd.server_close()
def test_wms_getmap_sld_body(self):
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetMap",
"LAYERS": "Country,db_point",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",
"HEIGHT": "500",
"WIDTH": "500",
"CRS": "EPSG:3857"
}.items())])
r, h = self._result(self._execute_request(qs))
self._img_diff_error(r, h, "WMS_GetMap_SLDRestored")
qs = "?" + "&".join(["%s=%s" % i for i in list({
"MAP": urllib.parse.quote(self.projectPath),
"REQUEST": "GetMap",
"VERSION": "1.1.1",
"SERVICE": "WMS",
"SLD_BODY": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ogc=\"http://www.opengis.net/ogc\" xsi:schemaLocation=\"http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd\" version=\"1.1.0\" xmlns:se=\"http://www.opengis.net/se\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"> <NamedLayer> <se:Name>db_point</se:Name> <UserStyle> <se:Name>db_point_style</se:Name> <se:FeatureTypeStyle> <se:Rule> <se:Name>Single symbol</se:Name> <ogc:Filter xmlns:ogc=\"http://www.opengis.net/ogc\"> <ogc:PropertyIsEqualTo> <ogc:PropertyName>gid</ogc:PropertyName> <ogc:Literal>1</ogc:Literal> </ogc:PropertyIsEqualTo> </ogc:Filter> <se:PointSymbolizer uom=\"http://www.opengeospatial.org/se/units/metre\"> <se:Graphic> <se:Mark> <se:WellKnownName>square</se:WellKnownName> <se:Fill> <se:SvgParameter name=\"fill\">5e86a1</se:SvgParameter> </se:Fill> <se:Stroke> <se:SvgParameter name=\"stroke\">000000</se:SvgParameter> </se:Stroke> </se:Mark> <se:Size>0.007</se:Size> </se:Graphic> </se:PointSymbolizer> </se:Rule> </se:FeatureTypeStyle> </UserStyle> </NamedLayer> </StyledLayerDescriptor>",
"BBOX": "-16817707,-4710778,5696513,14587125",
"WIDTH": "500",
"HEIGHT": "500",
@ -1230,7 +1300,7 @@ class TestQgsServerWMSGetMap(QgsServerTestBase):
"SERVICE": "WMS",
"VERSION": "1.1.1",
"REQUEST": "GetMap",
"SLD": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ogc=\"http://www.opengis.net/ogc\" xsi:schemaLocation=\"http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd\" version=\"1.1.0\" xmlns:se=\"http://www.opengis.net/se\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"> <NamedLayer> <se:Name>CountryGroup</se:Name></NamedLayer> </StyledLayerDescriptor>",
"SLD_BODY": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><StyledLayerDescriptor xmlns=\"http://www.opengis.net/sld\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:ogc=\"http://www.opengis.net/ogc\" xsi:schemaLocation=\"http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd\" version=\"1.1.0\" xmlns:se=\"http://www.opengis.net/se\" xmlns:xlink=\"http://www.w3.org/1999/xlink\"> <NamedLayer> <se:Name>CountryGroup</se:Name></NamedLayer> </StyledLayerDescriptor>",
"STYLES": "",
"FORMAT": "image/png",
"BBOX": "-16817707,-4710778,5696513,14587125",

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<StyledLayerDescriptor xmlns="http://www.opengis.net/sld" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ogc="http://www.opengis.net/ogc" xsi:schemaLocation="http://www.opengis.net/sld http://schemas.opengis.net/sld/1.1.0/StyledLayerDescriptor.xsd" version="1.1.0" xmlns:se="http://www.opengis.net/se" xmlns:xlink="http://www.w3.org/1999/xlink">
<NamedLayer>
<se:Name>db_point</se:Name>
<UserStyle>
<se:Name>db_point_style</se:Name>
<se:FeatureTypeStyle>
<se:Rule>
<se:Name>Single symbol</se:Name>
<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
<ogc:PropertyIsEqualTo>
<ogc:PropertyName>gid</ogc:PropertyName>
<ogc:Literal>1</ogc:Literal>
</ogc:PropertyIsEqualTo>
</ogc:Filter>
<se:PointSymbolizer uom="http://www.opengeospatial.org/se/units/metre">
<se:Graphic>
<se:Mark>
<se:WellKnownName>square</se:WellKnownName>
<se:Fill>
<se:SvgParameter name="fill">5e86a1</se:SvgParameter>
</se:Fill>
<se:Stroke>
<se:SvgParameter name="stroke">000000</se:SvgParameter>
</se:Stroke>
</se:Mark>
<se:Size>0.007</se:Size>
</se:Graphic>
</se:PointSymbolizer>
</se:Rule>
</se:FeatureTypeStyle>
</UserStyle>
</NamedLayer>
</StyledLayerDescriptor>