Skip to content

Commit 05e047d

Browse files
committed
Generalize some WFS/WMS connection setting handling
1 parent 4c87338 commit 05e047d

File tree

5 files changed

+203
-33
lines changed

5 files changed

+203
-33
lines changed

python/core/qgsowsconnection.sip

+16
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,22 @@ class QgsOwsConnection : QObject
5555
:rtype: QgsDataSourceUri
5656
%End
5757

58+
static QgsDataSourceUri &addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey );
59+
%Docstring
60+
Adds uri parameters relating to the settings for a WMS or WCS connection to a QgsDataSourceUri ``uri``.
61+
Connection settings are taken from the specified QSettings ``settingsKey``.
62+
.. versionadded:: 3.0
63+
:rtype: QgsDataSourceUri
64+
%End
65+
66+
static QgsDataSourceUri &addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey );
67+
%Docstring
68+
Adds uri parameters relating to the settings for a WFS connection to a QgsDataSourceUri ``uri``.
69+
Connection settings are taken from the specified QSettings ``settingsKey``.
70+
.. versionadded:: 3.0
71+
:rtype: QgsDataSourceUri
72+
%End
73+
5874
static QStringList connectionList( const QString &service );
5975
%Docstring
6076
Returns the list of connections for the specified service

src/core/qgsowsconnection.cpp

+70-33
Original file line numberDiff line numberDiff line change
@@ -65,42 +65,14 @@ QgsOwsConnection::QgsOwsConnection( const QString &service, const QString &connN
6565
}
6666
mConnectionInfo.append( ",authcfg=" + authcfg );
6767

68-
QString referer = settings.value( key + "/referer" ).toString();
69-
if ( !referer.isEmpty() )
70-
{
71-
mUri.setParam( QStringLiteral( "referer" ), referer );
72-
}
73-
74-
bool ignoreGetMap = settings.value( key + "/ignoreGetMapURI", false ).toBool();
75-
bool ignoreGetFeatureInfo = settings.value( key + "/ignoreGetFeatureInfoURI", false ).toBool();
76-
bool ignoreAxisOrientation = settings.value( key + "/ignoreAxisOrientation", false ).toBool();
77-
bool invertAxisOrientation = settings.value( key + "/invertAxisOrientation", false ).toBool();
78-
bool smoothPixmapTransform = settings.value( key + "/smoothPixmapTransform", false ).toBool();
79-
QString dpiMode = settings.value( key + "/dpiMode", "all" ).toString();
80-
81-
if ( ignoreGetMap )
82-
{
83-
mUri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
84-
}
85-
if ( ignoreGetFeatureInfo )
86-
{
87-
mUri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
88-
}
89-
if ( ignoreAxisOrientation )
90-
{
91-
mUri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
92-
}
93-
if ( invertAxisOrientation )
94-
{
95-
mUri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
96-
}
97-
if ( smoothPixmapTransform )
68+
if ( mService.compare( QStringLiteral( "WMS" ), Qt::CaseInsensitive ) == 0
69+
|| mService.compare( QStringLiteral( "WCS" ), Qt::CaseInsensitive ) == 0 )
9870
{
99-
mUri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
71+
addWmsWcsConnectionSettings( mUri, key );
10072
}
101-
if ( !dpiMode.isEmpty() )
73+
else if ( mService.compare( QStringLiteral( "WFS" ), Qt::CaseInsensitive ) == 0 )
10274
{
103-
mUri.setParam( QStringLiteral( "dpiMode" ), dpiMode );
75+
addWfsConnectionSettings( mUri, key );
10476
}
10577

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

101+
QgsDataSourceUri &QgsOwsConnection::addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey )
102+
{
103+
addCommonConnectionSettings( uri, settingsKey );
104+
105+
QgsSettings settings;
106+
QString referer = settings.value( settingsKey + "/referer" ).toString();
107+
if ( !referer.isEmpty() )
108+
{
109+
uri.setParam( QStringLiteral( "referer" ), referer );
110+
}
111+
if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetMapURI" ), false ).toBool() )
112+
{
113+
uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
114+
}
115+
if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetFeatureInfoURI" ), false ).toBool() )
116+
{
117+
uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
118+
}
119+
if ( settings.value( settingsKey + QStringLiteral( "/smoothPixmapTransform" ), false ).toBool() )
120+
{
121+
uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
122+
}
123+
QString dpiMode = settings.value( settingsKey + QStringLiteral( "/dpiMode" ), QStringLiteral( "all" ) ).toString();
124+
if ( !dpiMode.isEmpty() )
125+
{
126+
uri.setParam( QStringLiteral( "dpiMode" ), dpiMode );
127+
}
128+
129+
return uri;
130+
}
131+
132+
QgsDataSourceUri &QgsOwsConnection::addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey )
133+
{
134+
addCommonConnectionSettings( uri, settingsKey );
135+
136+
QgsSettings settings;
137+
QString version = settings.value( settingsKey + "/version" ).toString();
138+
if ( !version.isEmpty() )
139+
{
140+
uri.setParam( QStringLiteral( "version" ), version );
141+
}
142+
143+
QString maxnumfeatures = settings.value( settingsKey + QStringLiteral( "/maxnumfeatures" ) ).toString();
144+
if ( !maxnumfeatures.isEmpty() )
145+
{
146+
uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumfeatures );
147+
}
148+
149+
return uri;
150+
}
151+
129152
QStringList QgsOwsConnection::connectionList( const QString &service )
130153
{
131154
QgsSettings settings;
@@ -145,6 +168,20 @@ void QgsOwsConnection::setSelectedConnection( const QString &service, const QStr
145168
settings.setValue( "qgis/connections-" + service.toLower() + "/selected", name );
146169
}
147170

171+
void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &key )
172+
{
173+
QgsSettings settings;
174+
175+
if ( settings.value( key + QStringLiteral( "/ignoreAxisOrientation" ), false ).toBool() )
176+
{
177+
uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
178+
}
179+
if ( settings.value( key + QStringLiteral( "/invertAxisOrientation" ), false ).toBool() )
180+
{
181+
uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
182+
}
183+
}
184+
148185
void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
149186
{
150187
QgsSettings settings;

src/core/qgsowsconnection.h

+16
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ class CORE_EXPORT QgsOwsConnection : public QObject
6767
*/
6868
QgsDataSourceUri uri() const;
6969

70+
/**
71+
* Adds uri parameters relating to the settings for a WMS or WCS connection to a QgsDataSourceUri \a uri.
72+
* Connection settings are taken from the specified QSettings \a settingsKey.
73+
* \since QGIS 3.0
74+
*/
75+
static QgsDataSourceUri &addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey );
76+
77+
/**
78+
* Adds uri parameters relating to the settings for a WFS connection to a QgsDataSourceUri \a uri.
79+
* Connection settings are taken from the specified QSettings \a settingsKey.
80+
* \since QGIS 3.0
81+
*/
82+
static QgsDataSourceUri &addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey );
83+
7084
//! Returns the list of connections for the specified service
7185
static QStringList connectionList( const QString &service );
7286

@@ -87,6 +101,8 @@ class CORE_EXPORT QgsOwsConnection : public QObject
87101
QString mService;
88102
QString mConnectionInfo;
89103

104+
static void addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &settingsKey );
105+
90106
};
91107

92108

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ ADD_PYTHON_TEST(PyQgsOGRProviderGpkg test_provider_ogr_gpkg.py)
106106
ADD_PYTHON_TEST(PyQgsOGRProviderSqlite test_provider_ogr_sqlite.py)
107107
ADD_PYTHON_TEST(PyQgsOpacityWidget test_qgsopacitywidget.py)
108108
ADD_PYTHON_TEST(PyQgsOptional test_qgsoptional.py)
109+
ADD_PYTHON_TEST(PyQgsOwsConnection test_qgsowsconnection.py)
109110
ADD_PYTHON_TEST(PyQgsPalLabelingBase test_qgspallabeling_base.py)
110111
ADD_PYTHON_TEST(PyQgsPalLabelingCanvas test_qgspallabeling_canvas.py)
111112
ADD_PYTHON_TEST(PyQgsPalLabelingComposer test_qgspallabeling_composer.py)
+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsOwsConnection
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Nyall Dawson'
10+
__date__ = '12.09.2017'
11+
__copyright__ = 'Copyright 2017, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis # NOQA
16+
17+
from qgis.testing import unittest, start_app
18+
from qgis.core import (QgsOwsConnection,
19+
QgsDataSourceUri,
20+
QgsSettings)
21+
from qgis.PyQt.QtCore import QCoreApplication
22+
23+
24+
class TestQgsOwsConnection(unittest.TestCase):
25+
26+
@classmethod
27+
def setUpClass(cls):
28+
"""Run before all tests"""
29+
QCoreApplication.setOrganizationName("QGIS_Test")
30+
QCoreApplication.setOrganizationDomain("QGIS_TestPyQgsColorScheme.com")
31+
QCoreApplication.setApplicationName("QGIS_TestPyQgsColorScheme")
32+
QgsSettings().clear()
33+
start_app()
34+
35+
# setup some fake connections
36+
settings = QgsSettings()
37+
key = 'qgis/connections-wms/test/'
38+
settings.setValue(key + 'url', 'aaa.bbb.com')
39+
settings.setValue(key + 'referer', 'my_ref')
40+
settings.setValue(key + 'ignoreGetMapURI', True)
41+
settings.setValue(key + 'ignoreGetFeatureInfoURI', True)
42+
settings.setValue(key + 'smoothPixmapTransform', True)
43+
settings.setValue(key + 'dpiMode', 4)
44+
settings.setValue(key + 'ignoreAxisOrientation', True)
45+
settings.setValue(key + 'invertAxisOrientation', True)
46+
47+
key = 'qgis/connections-wfs/test/'
48+
settings.setValue(key + 'url', 'ccc.ddd.com')
49+
settings.setValue(key + 'version', '1.1.0')
50+
settings.setValue(key + 'maxnumfeatures', '47')
51+
settings.setValue(key + 'ignoreAxisOrientation', True)
52+
settings.setValue(key + 'invertAxisOrientation', True)
53+
54+
def testWmsConnection(self):
55+
c = QgsOwsConnection('WMS', 'test')
56+
uri = c.uri()
57+
58+
self.assertEqual(uri.param('url'), 'aaa.bbb.com')
59+
self.assertEqual(uri.param('referer'), 'my_ref')
60+
self.assertEqual(uri.param('IgnoreGetMapUrl'), '1')
61+
self.assertEqual(uri.param('IgnoreGetFeatureInfoUrl'), '1')
62+
self.assertEqual(uri.param('SmoothPixmapTransform'), '1')
63+
self.assertEqual(uri.param('dpiMode'), '4')
64+
self.assertEqual(uri.param('IgnoreAxisOrientation'), '1')
65+
self.assertEqual(uri.param('InvertAxisOrientation'), '1')
66+
67+
def testWmsSettings(self):
68+
uri = QgsDataSourceUri()
69+
QgsOwsConnection.addWmsWcsConnectionSettings(uri, 'qgis/connections-wms/test/')
70+
71+
self.assertEqual(uri.param('referer'), 'my_ref')
72+
self.assertEqual(uri.param('IgnoreGetMapUrl'), '1')
73+
self.assertEqual(uri.param('IgnoreGetFeatureInfoUrl'), '1')
74+
self.assertEqual(uri.param('SmoothPixmapTransform'), '1')
75+
self.assertEqual(uri.param('dpiMode'), '4')
76+
self.assertEqual(uri.param('IgnoreAxisOrientation'), '1')
77+
self.assertEqual(uri.param('InvertAxisOrientation'), '1')
78+
79+
def testWfsConnection(self):
80+
c = QgsOwsConnection('WFS', 'test')
81+
uri = c.uri()
82+
83+
self.assertEqual(uri.param('url'), 'ccc.ddd.com')
84+
self.assertEqual(uri.param('version'), '1.1.0')
85+
self.assertEqual(uri.param('maxNumFeatures'), '47')
86+
self.assertEqual(uri.param('IgnoreAxisOrientation'), '1')
87+
self.assertEqual(uri.param('InvertAxisOrientation'), '1')
88+
89+
def testWfsSettings(self):
90+
uri = QgsDataSourceUri()
91+
QgsOwsConnection.addWfsConnectionSettings(uri, 'qgis/connections-wfs/test/')
92+
93+
self.assertEqual(uri.param('version'), '1.1.0')
94+
self.assertEqual(uri.param('maxNumFeatures'), '47')
95+
self.assertEqual(uri.param('IgnoreAxisOrientation'), '1')
96+
self.assertEqual(uri.param('InvertAxisOrientation'), '1')
97+
98+
99+
if __name__ == "__main__":
100+
unittest.main()

0 commit comments

Comments
 (0)