Skip to content

Commit

Permalink
Generalize some WFS/WMS connection setting handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 12, 2017
1 parent 4c87338 commit 05e047d
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 33 deletions.
16 changes: 16 additions & 0 deletions python/core/qgsowsconnection.sip
Expand Up @@ -55,6 +55,22 @@ class QgsOwsConnection : QObject
:rtype: QgsDataSourceUri
%End

static QgsDataSourceUri &addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey );
%Docstring
Adds uri parameters relating to the settings for a WMS or WCS connection to a QgsDataSourceUri ``uri``.
Connection settings are taken from the specified QSettings ``settingsKey``.
.. versionadded:: 3.0
:rtype: QgsDataSourceUri
%End

static QgsDataSourceUri &addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey );
%Docstring
Adds uri parameters relating to the settings for a WFS connection to a QgsDataSourceUri ``uri``.
Connection settings are taken from the specified QSettings ``settingsKey``.
.. versionadded:: 3.0
:rtype: QgsDataSourceUri
%End

static QStringList connectionList( const QString &service );
%Docstring
Returns the list of connections for the specified service
Expand Down
103 changes: 70 additions & 33 deletions src/core/qgsowsconnection.cpp
Expand Up @@ -65,42 +65,14 @@ QgsOwsConnection::QgsOwsConnection( const QString &service, const QString &connN
}
mConnectionInfo.append( ",authcfg=" + authcfg );

QString referer = settings.value( key + "/referer" ).toString();
if ( !referer.isEmpty() )
{
mUri.setParam( QStringLiteral( "referer" ), referer );
}

bool ignoreGetMap = settings.value( key + "/ignoreGetMapURI", false ).toBool();
bool ignoreGetFeatureInfo = settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool();
bool ignoreAxisOrientation = settings.value( key + "/ignoreAxisOrientation", false ).toBool();
bool invertAxisOrientation = settings.value( key + "/invertAxisOrientation", false ).toBool();
bool smoothPixmapTransform = settings.value( key + "/smoothPixmapTransform", false ).toBool();
QString dpiMode = settings.value( key + "/dpiMode", "all" ).toString();

if ( ignoreGetMap )
{
mUri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
}
if ( ignoreGetFeatureInfo )
{
mUri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
}
if ( ignoreAxisOrientation )
{
mUri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
}
if ( invertAxisOrientation )
{
mUri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
}
if ( smoothPixmapTransform )
if ( mService.compare( QStringLiteral( "WMS" ), Qt::CaseInsensitive ) == 0
|| mService.compare( QStringLiteral( "WCS" ), Qt::CaseInsensitive ) == 0 )
{
mUri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
addWmsWcsConnectionSettings( mUri, key );
}
if ( !dpiMode.isEmpty() )
else if ( mService.compare( QStringLiteral( "WFS" ), Qt::CaseInsensitive ) == 0 )
{
mUri.setParam( QStringLiteral( "dpiMode" ), dpiMode );
addWfsConnectionSettings( mUri, key );
}

QgsDebugMsg( QString( "encoded uri: '%1'." ).arg( QString( mUri.encodedUri() ) ) );
Expand All @@ -126,6 +98,57 @@ QgsDataSourceUri QgsOwsConnection::uri() const
return mUri;
}

QgsDataSourceUri &QgsOwsConnection::addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey )
{
addCommonConnectionSettings( uri, settingsKey );

QgsSettings settings;
QString referer = settings.value( settingsKey + "/referer" ).toString();
if ( !referer.isEmpty() )
{
uri.setParam( QStringLiteral( "referer" ), referer );
}
if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetMapURI" ), false ).toBool() )
{
uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
}
if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetFeatureInfoURI" ), false ).toBool() )
{
uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
}
if ( settings.value( settingsKey + QStringLiteral( "/smoothPixmapTransform" ), false ).toBool() )
{
uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
}
QString dpiMode = settings.value( settingsKey + QStringLiteral( "/dpiMode" ), QStringLiteral( "all" ) ).toString();
if ( !dpiMode.isEmpty() )
{
uri.setParam( QStringLiteral( "dpiMode" ), dpiMode );
}

return uri;
}

QgsDataSourceUri &QgsOwsConnection::addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey )
{
addCommonConnectionSettings( uri, settingsKey );

QgsSettings settings;
QString version = settings.value( settingsKey + "/version" ).toString();
if ( !version.isEmpty() )
{
uri.setParam( QStringLiteral( "version" ), version );
}

QString maxnumfeatures = settings.value( settingsKey + QStringLiteral( "/maxnumfeatures" ) ).toString();
if ( !maxnumfeatures.isEmpty() )
{
uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumfeatures );
}

return uri;
}

QStringList QgsOwsConnection::connectionList( const QString &service )
{
QgsSettings settings;
Expand All @@ -145,6 +168,20 @@ void QgsOwsConnection::setSelectedConnection( const QString &service, const QStr
settings.setValue( "qgis/connections-" + service.toLower() + "/selected", name );
}

void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &key )
{
QgsSettings settings;

if ( settings.value( key + QStringLiteral( "/ignoreAxisOrientation" ), false ).toBool() )
{
uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
}
if ( settings.value( key + QStringLiteral( "/invertAxisOrientation" ), false ).toBool() )
{
uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
}
}

void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
{
QgsSettings settings;
Expand Down
16 changes: 16 additions & 0 deletions src/core/qgsowsconnection.h
Expand Up @@ -67,6 +67,20 @@ class CORE_EXPORT QgsOwsConnection : public QObject
*/
QgsDataSourceUri uri() const;

/**
* Adds uri parameters relating to the settings for a WMS or WCS connection to a QgsDataSourceUri \a uri.
* Connection settings are taken from the specified QSettings \a settingsKey.
* \since QGIS 3.0
*/
static QgsDataSourceUri &addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey );

/**
* Adds uri parameters relating to the settings for a WFS connection to a QgsDataSourceUri \a uri.
* Connection settings are taken from the specified QSettings \a settingsKey.
* \since QGIS 3.0
*/
static QgsDataSourceUri &addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey );

//! Returns the list of connections for the specified service
static QStringList connectionList( const QString &service );

Expand All @@ -87,6 +101,8 @@ class CORE_EXPORT QgsOwsConnection : public QObject
QString mService;
QString mConnectionInfo;

static void addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey );

};


Expand Down
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -106,6 +106,7 @@ ADD_PYTHON_TEST(PyQgsOGRProviderGpkg test_provider_ogr_gpkg.py)
ADD_PYTHON_TEST(PyQgsOGRProviderSqlite test_provider_ogr_sqlite.py)
ADD_PYTHON_TEST(PyQgsOpacityWidget test_qgsopacitywidget.py)
ADD_PYTHON_TEST(PyQgsOptional test_qgsoptional.py)
ADD_PYTHON_TEST(PyQgsOwsConnection test_qgsowsconnection.py)
ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)
Expand Down
100 changes: 100 additions & 0 deletions tests/src/python/test_qgsowsconnection.py
@@ -0,0 +1,100 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsOwsConnection
.. note:: 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.
"""
__author__ = 'Nyall Dawson'
__date__ = '12.09.2017'
__copyright__ = 'Copyright 2017, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis # NOQA

from qgis.testing import unittest, start_app
from qgis.core import (QgsOwsConnection,
QgsDataSourceUri,
QgsSettings)
from qgis.PyQt.QtCore import QCoreApplication


class TestQgsOwsConnection(unittest.TestCase):

@classmethod
def setUpClass(cls):
"""Run before all tests"""
QCoreApplication.setOrganizationName("QGIS_Test")
QCoreApplication.setOrganizationDomain("QGIS_TestPyQgsColorScheme.com")
QCoreApplication.setApplicationName("QGIS_TestPyQgsColorScheme")
QgsSettings().clear()
start_app()

# setup some fake connections
settings = QgsSettings()
key = 'qgis/connections-wms/test/'
settings.setValue(key + 'url', 'aaa.bbb.com')
settings.setValue(key + 'referer', 'my_ref')
settings.setValue(key + 'ignoreGetMapURI', True)
settings.setValue(key + 'ignoreGetFeatureInfoURI', True)
settings.setValue(key + 'smoothPixmapTransform', True)
settings.setValue(key + 'dpiMode', 4)
settings.setValue(key + 'ignoreAxisOrientation', True)
settings.setValue(key + 'invertAxisOrientation', True)

key = 'qgis/connections-wfs/test/'
settings.setValue(key + 'url', 'ccc.ddd.com')
settings.setValue(key + 'version', '1.1.0')
settings.setValue(key + 'maxnumfeatures', '47')
settings.setValue(key + 'ignoreAxisOrientation', True)
settings.setValue(key + 'invertAxisOrientation', True)

def testWmsConnection(self):
c = QgsOwsConnection('WMS', 'test')
uri = c.uri()

self.assertEqual(uri.param('url'), 'aaa.bbb.com')
self.assertEqual(uri.param('referer'), 'my_ref')
self.assertEqual(uri.param('IgnoreGetMapUrl'), '1')
self.assertEqual(uri.param('IgnoreGetFeatureInfoUrl'), '1')
self.assertEqual(uri.param('SmoothPixmapTransform'), '1')
self.assertEqual(uri.param('dpiMode'), '4')
self.assertEqual(uri.param('IgnoreAxisOrientation'), '1')
self.assertEqual(uri.param('InvertAxisOrientation'), '1')

def testWmsSettings(self):
uri = QgsDataSourceUri()
QgsOwsConnection.addWmsWcsConnectionSettings(uri, 'qgis/connections-wms/test/')

self.assertEqual(uri.param('referer'), 'my_ref')
self.assertEqual(uri.param('IgnoreGetMapUrl'), '1')
self.assertEqual(uri.param('IgnoreGetFeatureInfoUrl'), '1')
self.assertEqual(uri.param('SmoothPixmapTransform'), '1')
self.assertEqual(uri.param('dpiMode'), '4')
self.assertEqual(uri.param('IgnoreAxisOrientation'), '1')
self.assertEqual(uri.param('InvertAxisOrientation'), '1')

def testWfsConnection(self):
c = QgsOwsConnection('WFS', 'test')
uri = c.uri()

self.assertEqual(uri.param('url'), 'ccc.ddd.com')
self.assertEqual(uri.param('version'), '1.1.0')
self.assertEqual(uri.param('maxNumFeatures'), '47')
self.assertEqual(uri.param('IgnoreAxisOrientation'), '1')
self.assertEqual(uri.param('InvertAxisOrientation'), '1')

def testWfsSettings(self):
uri = QgsDataSourceUri()
QgsOwsConnection.addWfsConnectionSettings(uri, 'qgis/connections-wfs/test/')

self.assertEqual(uri.param('version'), '1.1.0')
self.assertEqual(uri.param('maxNumFeatures'), '47')
self.assertEqual(uri.param('IgnoreAxisOrientation'), '1')
self.assertEqual(uri.param('InvertAxisOrientation'), '1')


if __name__ == "__main__":
unittest.main()

0 comments on commit 05e047d

Please sign in to comment.