QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgsowsconnection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsowsconnection.cpp - selector for WMS servers, etc.
3 -------------------
4 begin : 3 April 2005
5 copyright :
6 original : (C) 2005 by Brendan Morley email : morb at ozemail dot com dot au
7 wms search : (C) 2009 Mathias Walker <mwa at sourcepole.ch>, Sourcepole AG
8 wms-c support : (C) 2010 Juergen E. Fischer < jef at norbit dot de >, norBIT GmbH
9 generalized : (C) 2012 Radim Blazek, based on qgswmsconnection.cpp
10
11 ***************************************************************************/
12
13/***************************************************************************
14 * *
15 * This program is free software; you can redistribute it and/or modify *
16 * it under the terms of the GNU General Public License as published by *
17 * the Free Software Foundation; either version 2 of the License, or *
18 * (at your option) any later version. *
19 * *
20 ***************************************************************************/
21
22#include "qgis.h" // GEO_EPSG_CRS_ID
23#include "qgsdatasourceuri.h"
24#include "qgslogger.h"
25#include "qgsowsconnection.h"
26#include "moc_qgsowsconnection.cpp"
27#include "qgssettings.h"
28#include "qgshttpheaders.h"
31
32#include <QPicture>
33#include <QUrl>
34#include <QNetworkRequest>
35#include <QNetworkReply>
36
37
38const QgsSettingsEntryString *QgsXyzConnectionSettings::settingsUrl = new QgsSettingsEntryString( QStringLiteral( "url" ), sTreeXyzConnections, QString() ) ;
39const QgsSettingsEntryVariantMap *QgsXyzConnectionSettings::settingsHeaders = new QgsSettingsEntryVariantMap( QStringLiteral( "http-header" ), sTreeXyzConnections ) ;
40const QgsSettingsEntryInteger *QgsXyzConnectionSettings::settingsZmin = new QgsSettingsEntryInteger( QStringLiteral( "zmin" ), sTreeXyzConnections, -1 );
41const QgsSettingsEntryInteger *QgsXyzConnectionSettings::settingsZmax = new QgsSettingsEntryInteger( QStringLiteral( "zmax" ), sTreeXyzConnections, -1 );
42const QgsSettingsEntryDouble *QgsXyzConnectionSettings::settingsTilePixelRatio = new QgsSettingsEntryDouble( QStringLiteral( "tile-pixel-ratio" ), sTreeXyzConnections, 0, QStringLiteral( "0 = unknown (not scaled), 1.0 = 256x256, 2.0 = 512x512" ) ) ;
43const QgsSettingsEntryBool *QgsXyzConnectionSettings::settingsHidden = new QgsSettingsEntryBool( QStringLiteral( "hidden" ), sTreeXyzConnections, false ) ;
44const QgsSettingsEntryString *QgsXyzConnectionSettings::settingsInterpretation = new QgsSettingsEntryString( QStringLiteral( "interpretation" ), sTreeXyzConnections, QString() ) ;
45const QgsSettingsEntryString *QgsXyzConnectionSettings::settingsUsername = new QgsSettingsEntryString( QStringLiteral( "username" ), sTreeXyzConnections ) ;
46const QgsSettingsEntryString *QgsXyzConnectionSettings::settingsPassword = new QgsSettingsEntryString( QStringLiteral( "password" ), sTreeXyzConnections ) ;
47const QgsSettingsEntryString *QgsXyzConnectionSettings::settingsAuthcfg = new QgsSettingsEntryString( QStringLiteral( "authcfg" ), sTreeXyzConnections ) ;
48
49
50const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsUrl = new QgsSettingsEntryString( QStringLiteral( "url" ), sTreeConnectionArcgis );
51const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsAuthcfg = new QgsSettingsEntryString( QStringLiteral( "authcfg" ), sTreeConnectionArcgis );
52const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsUsername = new QgsSettingsEntryString( QStringLiteral( "username" ), sTreeConnectionArcgis );
53const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsPassword = new QgsSettingsEntryString( QStringLiteral( "password" ), sTreeConnectionArcgis );
54const QgsSettingsEntryVariantMap *QgsArcGisConnectionSettings::settingsHeaders = new QgsSettingsEntryVariantMap( QStringLiteral( "http-header" ), sTreeConnectionArcgis );
55const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsUrlPrefix = new QgsSettingsEntryString( QStringLiteral( "urlprefix" ), sTreeConnectionArcgis );
56const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsContentEndpoint = new QgsSettingsEntryString( QStringLiteral( "content-endpoint" ), sTreeConnectionArcgis );
57const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsCommunityEndpoint = new QgsSettingsEntryString( QStringLiteral( "community-endpoint" ), sTreeConnectionArcgis );
58
59
60const QgsSettingsEntryString *QgsOwsConnection::settingsUrl = new QgsSettingsEntryString( QStringLiteral( "url" ), sTreeOwsConnections, QString() ) ;
61const QgsSettingsEntryVariantMap *QgsOwsConnection::settingsHeaders = new QgsSettingsEntryVariantMap( QStringLiteral( "http-header" ), sTreeOwsConnections ) ;
62const QgsSettingsEntryString *QgsOwsConnection::settingsVersion = new QgsSettingsEntryString( QStringLiteral( "version" ), sTreeOwsConnections, QString() ) ;
63const QgsSettingsEntryBool *QgsOwsConnection::settingsIgnoreGetMapURI = new QgsSettingsEntryBool( QStringLiteral( "ignore-get-map-uri" ), sTreeOwsConnections, false ) ;
64const QgsSettingsEntryBool *QgsOwsConnection::settingsIgnoreGetFeatureInfoURI = new QgsSettingsEntryBool( QStringLiteral( "ignore-get-feature-info-uri" ), sTreeOwsConnections, false ) ;
65const QgsSettingsEntryBool *QgsOwsConnection::settingsSmoothPixmapTransform = new QgsSettingsEntryBool( QStringLiteral( "smooth-pixmap-transform" ), sTreeOwsConnections, false ) ;
66const QgsSettingsEntryBool *QgsOwsConnection::settingsReportedLayerExtents = new QgsSettingsEntryBool( QStringLiteral( "reported-layer-extents" ), sTreeOwsConnections, false ) ;
69const QgsSettingsEntryString *QgsOwsConnection::settingsMaxNumFeatures = new QgsSettingsEntryString( QStringLiteral( "max-num-features" ), sTreeOwsConnections ) ;
70const QgsSettingsEntryString *QgsOwsConnection::settingsPagesize = new QgsSettingsEntryString( QStringLiteral( "page-size" ), sTreeOwsConnections ) ;
71const QgsSettingsEntryString *QgsOwsConnection::settingsPagingEnabled = new QgsSettingsEntryString( QStringLiteral( "paging-enabled" ), sTreeOwsConnections, QString( "default" ) ) ;
72const QgsSettingsEntryBool *QgsOwsConnection::settingsPreferCoordinatesForWfsT11 = new QgsSettingsEntryBool( QStringLiteral( "prefer-coordinates-for-wfs-T11" ), sTreeOwsConnections, false ) ;
73const QgsSettingsEntryBool *QgsOwsConnection::settingsIgnoreAxisOrientation = new QgsSettingsEntryBool( QStringLiteral( "ignore-axis-orientation" ), sTreeOwsConnections, false ) ;
74const QgsSettingsEntryBool *QgsOwsConnection::settingsInvertAxisOrientation = new QgsSettingsEntryBool( QStringLiteral( "invert-axis-orientation" ), sTreeOwsConnections, false ) ;
75const QgsSettingsEntryString *QgsOwsConnection::settingsUsername = new QgsSettingsEntryString( QStringLiteral( "username" ), sTreeOwsConnections ) ;
76const QgsSettingsEntryString *QgsOwsConnection::settingsPassword = new QgsSettingsEntryString( QStringLiteral( "password" ), sTreeOwsConnections ) ;
77const QgsSettingsEntryString *QgsOwsConnection::settingsAuthCfg = new QgsSettingsEntryString( QStringLiteral( "authcfg" ), sTreeOwsConnections ) ;
78const QgsSettingsEntryInteger *QgsOwsConnection::settingsFeatureCount = new QgsSettingsEntryInteger( QStringLiteral( "feature-count" ), sTreeOwsConnections, 10 );
79
80QgsOwsConnection::QgsOwsConnection( const QString &service, const QString &connName )
81 : mConnName( connName )
82 , mService( service )
83{
84 QgsDebugMsgLevel( "theConnName = " + connName, 4 );
85
86 const QString url = settingsUrl->value( {mService.toLower(), mConnName} );
87 mConnectionInfo = url;
88 mUri.setParam( QStringLiteral( "url" ), url );
89
90 // Check for credentials and prepend to the connection info
91 const QString username = settingsUsername->value( {mService.toLower(), mConnName} );
92 const QString password = settingsPassword->value( {mService.toLower(), mConnName} );
93 if ( !username.isEmpty() )
94 {
95 // check for a password, if none prompt to get it
96 mUri.setUsername( username );
97 mUri.setPassword( password );
98 }
99
100 const QString authcfg = settingsAuthCfg->value( {mService.toLower(), mConnName} );
101 if ( !authcfg.isEmpty() )
102 {
103 mUri.setAuthConfigId( authcfg );
104 }
105 mConnectionInfo.append( ",authcfg=" + authcfg );
106
107 QgsHttpHeaders httpHeaders( settingsHeaders->value( {mService.toLower(), mConnName} ) );
108 mUri.setHttpHeaders( httpHeaders );
109 const QString referer = httpHeaders[QgsHttpHeaders::KEY_REFERER].toString();
110 if ( !referer.isEmpty() )
111 {
112 mConnectionInfo.append( ",referer=" + referer );
113 }
114
115 if ( mService.compare( QLatin1String( "WMS" ), Qt::CaseInsensitive ) == 0
116 || mService.compare( QLatin1String( "WCS" ), Qt::CaseInsensitive ) == 0 )
117 {
119 }
120 else if ( mService.compare( QLatin1String( "WFS" ), Qt::CaseInsensitive ) == 0 )
121 {
123 }
124
125 QgsDebugMsgLevel( QStringLiteral( "encoded uri: '%1'." ).arg( QString( mUri.encodedUri() ) ), 4 );
126}
127
129{
130 return mConnName;
131}
132
134{
135 return mConnectionInfo;
136}
137
139{
140 return mService;
141}
142
144{
145 return mUri;
146}
147
149{
151 addCommonConnectionSettings( uri, settingsKey );
153
154 const QgsSettings settings;
155 uri.httpHeaders().setFromSettings( settings, settingsKey );
156
157 if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetMapURI" ), false ).toBool() )
158 {
159 uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
160 }
161 if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetFeatureInfoURI" ), false ).toBool() )
162 {
163 uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
164 }
165 if ( settings.value( settingsKey + QStringLiteral( "/smoothPixmapTransform" ), false ).toBool() )
166 {
167 uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
168 }
169 if ( settings.value( settingsKey + QStringLiteral( "/ignoreReportedLayerExtents" ), false ).toBool() )
170 {
171 uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
172 }
173 const QString dpiMode = settings.value( settingsKey + QStringLiteral( "/dpiMode" ), QStringLiteral( "all" ) ).toString();
174 if ( !dpiMode.isEmpty() )
175 {
176 uri.setParam( QStringLiteral( "dpiMode" ), dpiMode );
177 }
178 const QString tilePixelRatio = settings.value( settingsKey + QStringLiteral( "/tilePixelRatio" ), "0" ).toString();
179 if ( tilePixelRatio != QLatin1String( "0" ) )
180 {
181 uri.setParam( QStringLiteral( "tilePixelRatio" ), tilePixelRatio );
182 }
183
184 return uri;
185}
186
187QgsDataSourceUri &QgsOwsConnection::addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connName )
188{
189 addCommonConnectionSettings( uri, service, connName );
190
191 uri.setHttpHeaders( QgsHttpHeaders( settingsHeaders->value( {service.toLower(), connName} ) ) );
192
193 if ( settingsIgnoreGetMapURI->value( {service.toLower(), connName} ) )
194 {
195 uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
196 }
197 if ( settingsIgnoreGetFeatureInfoURI->value( {service.toLower(), connName} ) )
198 {
199 uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
200 }
201 if ( settingsSmoothPixmapTransform->value( {service.toLower(), connName} ) )
202 {
203 uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
204 }
205 if ( settingsReportedLayerExtents->value( {service.toLower(), connName} ) )
206 {
207 uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
208 }
209 if ( settingsDpiMode->exists( {service.toLower(), connName} ) )
210 {
211 uri.setParam( QStringLiteral( "dpiMode" ), QString::number( static_cast<int>( settingsDpiMode->value( {service.toLower(), connName} ) ) ) );
212 }
213 if ( settingsTilePixelRatio->exists( {service.toLower(), connName} ) )
214 {
215 uri.setParam( QStringLiteral( "tilePixelRatio" ), QString::number( static_cast<int>( settingsTilePixelRatio->value( {service.toLower(), connName} ) ) ) );
216 }
217 if ( settingsFeatureCount->exists( {service.toLower(), connName} ) )
218 {
219 uri.setParam( QStringLiteral( "featureCount" ), QString::number( settingsFeatureCount->value( {service.toLower(), connName} ) ) );
220 }
221
222 return uri;
223}
224
226{
228 addCommonConnectionSettings( uri, settingsKey );
230
231 const QgsSettings settings;
232 const QString version = settings.value( settingsKey + "/version" ).toString();
233 if ( !version.isEmpty() )
234 {
235 uri.setParam( QStringLiteral( "version" ), version );
236 }
237
238 const QString maxnumfeatures = settings.value( settingsKey + QStringLiteral( "/maxnumfeatures" ) ).toString();
239 if ( !maxnumfeatures.isEmpty() )
240 {
241 uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumfeatures );
242 }
243
244 return uri;
245}
246
247QgsDataSourceUri &QgsOwsConnection::addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connName )
248{
249 addCommonConnectionSettings( uri, service, connName );
250
251 const QString version = settingsVersion->value( {service.toLower(), connName} );
252 if ( !version.isEmpty() )
253 {
254 uri.setParam( QStringLiteral( "version" ), version );
255 }
256
257 const QString maxnumFeatures = settingsMaxNumFeatures->value( {service.toLower(), connName} );
258 if ( !maxnumFeatures.isEmpty() )
259 {
260 uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumFeatures );
261 }
262
263 return uri;
264}
265
266QStringList QgsOwsConnection::connectionList( const QString &service )
267{
269}
270
271QString QgsOwsConnection::selectedConnection( const QString &service )
272{
274}
275
276void QgsOwsConnection::setSelectedConnection( const QString &service, const QString &name )
277{
279}
280
281void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &key )
282{
283 const QgsSettings settings;
284
285 if ( settings.value( key + QStringLiteral( "/ignoreAxisOrientation" ), false ).toBool() )
286 {
287 uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
288 }
289 if ( settings.value( key + QStringLiteral( "/invertAxisOrientation" ), false ).toBool() )
290 {
291 uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
292 }
293}
294
295void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connectionName )
296{
297
298 if ( settingsIgnoreAxisOrientation->value( {service.toLower(), connectionName} ) )
299 {
300 uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
301 }
302 if ( settingsInvertAxisOrientation->value( {service.toLower(), connectionName} ) )
303 {
304 uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
305 }
306}
307
308void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
309{
310 sTreeOwsConnections->deleteItem( name, {service.toLower()} );
311}
@ SaveEnumFlagAsInt
The enum/flag will be saved as an integer value instead of text.
@ Undefined
Undefined (not scale)
static const QgsSettingsEntryString * settingsUsername
static const QgsSettingsEntryString * settingsUrl
static const QgsSettingsEntryString * settingsPassword
static const QgsSettingsEntryVariantMap * settingsHeaders
static const QgsSettingsEntryString * settingsContentEndpoint
static const QgsSettingsEntryString * settingsUrlPrefix
static const QgsSettingsEntryString * settingsAuthcfg
static const QgsSettingsEntryString * settingsCommunityEndpoint
Class for storing the component parts of a RDBMS data source URI (e.g.
QByteArray encodedUri() const
Returns the complete encoded URI as a byte array.
void setAuthConfigId(const QString &authcfg)
Sets the authentication configuration ID for the URI.
QgsHttpHeaders httpHeaders() const
Returns http headers.
void setUsername(const QString &username)
Sets the username for the URI.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
void setHttpHeaders(const QgsHttpHeaders &headers)
Sets headers to headers.
void setPassword(const QString &password)
Sets the password for the URI.
This class implements simple http header management.
static const QString KEY_REFERER
Used in settings as the referer key.
void setFromSettings(const QgsSettings &settings, const QString &key=QString())
Loads headers from the settings.
static const QgsSettingsEntryInteger * settingsFeatureCount
static const QgsSettingsEntryBool * settingsPreferCoordinatesForWfsT11
static Q_DECL_DEPRECATED QgsDataSourceUri & addWmsWcsConnectionSettings(QgsDataSourceUri &uri, const QString &settingsKey)
Adds uri parameters relating to the settings for a WMS or WCS connection to a QgsDataSourceUri uri.
static QStringList connectionList(const QString &service)
Returns the list of connections for the specified service.
static const QgsSettingsEntryString * settingsPagingEnabled
static const QgsSettingsEntryString * settingsMaxNumFeatures
static QgsSettingsTreeNamedListNode * sTreeOwsConnections
QgsDataSourceUri mUri
static const QgsSettingsEntryBool * settingsIgnoreGetFeatureInfoURI
static const QgsSettingsEntryString * settingsPassword
static const QgsSettingsEntryBool * settingsReportedLayerExtents
static void setSelectedConnection(const QString &service, const QString &name)
Marks the specified connection for the specified service as selected.
static const QgsSettingsEntryEnumFlag< Qgis::DpiMode > * settingsDpiMode
static const QgsSettingsEntryBool * settingsIgnoreAxisOrientation
static const QgsSettingsEntryBool * settingsInvertAxisOrientation
static const QgsSettingsEntryString * settingsVersion
QString connectionInfo() const
Returns connection info string.
static void deleteConnection(const QString &service, const QString &name)
Deletes the connection for the specified service with the specified name.
static const QgsSettingsEntryString * settingsPagesize
static const QgsSettingsEntryString * settingsAuthCfg
static const QgsSettingsEntryEnumFlag< Qgis::TilePixelRatio > * settingsTilePixelRatio
static const QgsSettingsEntryVariantMap * settingsHeaders
static Q_DECL_DEPRECATED QgsDataSourceUri & addWfsConnectionSettings(QgsDataSourceUri &uri, const QString &settingsKey)
Adds uri parameters relating to the settings for a WFS connection to a QgsDataSourceUri uri.
QString connectionName() const
Returns the connection name.
static const QgsSettingsEntryString * settingsUsername
static const QgsSettingsEntryBool * settingsSmoothPixmapTransform
static const QgsSettingsEntryString * settingsUrl
static QString selectedConnection(const QString &service)
Retrieves the selected connection for the specified service.
static const QgsSettingsEntryBool * settingsIgnoreGetMapURI
QgsOwsConnection(const QString &service, const QString &connName)
Constructor.
QgsDataSourceUri uri() const
Returns the connection uri.
QString service() const
Returns a string representing the service type, e.g.
T value(const QString &dynamicKeyPart=QString()) const
Returns settings value.
bool exists(const QString &dynamicKeyPart=QString()) const
Returns true if the settings is contained in the underlying QSettings.
A boolean settings entry.
A double settings entry.
A template class for enum and flag settings entry.
An integer settings entry.
A string settings entry.
A string list settings entry.
void deleteItem(const QString &item, const QStringList &parentsNamedItems=QStringList())
Deletes a named item from the named list node.
QString selectedItem(const QStringList &parentsNamedItems=QStringList())
Returns the selected named item from the named list node.
void setSelectedItem(const QString &item, const QStringList &parentsNamedItems=QStringList())
Sets the selected named item from the named list node.
QStringList items(const QStringList &parentsNamedItems=QStringList()) const
Returns the list of items.
This class is a composition of two QSettings instances:
Definition qgssettings.h:64
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
static const QgsSettingsEntryString * settingsPassword
static const QgsSettingsEntryDouble * settingsTilePixelRatio
static const QgsSettingsEntryString * settingsInterpretation
static const QgsSettingsEntryString * settingsUsername
static const QgsSettingsEntryString * settingsAuthcfg
static const QgsSettingsEntryInteger * settingsZmin
static const QgsSettingsEntryBool * settingsHidden
static const QgsSettingsEntryInteger * settingsZmax
static const QgsSettingsEntryString * settingsUrl
static const QgsSettingsEntryVariantMap * settingsHeaders
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6535
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6534
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39