QGIS API Documentation 3.34.0-Prizren (ffbdd678812)
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 "qgssettings.h"
27#include "qgshttpheaders.h"
30
31#include <QPicture>
32#include <QUrl>
33#include <QNetworkRequest>
34#include <QNetworkReply>
35
36
37const QgsSettingsEntryString *QgsXyzConnectionSettings::settingsUrl = new QgsSettingsEntryString( QStringLiteral( "url" ), sTreeXyzConnections, QString() ) ;
38const QgsSettingsEntryVariantMap *QgsXyzConnectionSettings::settingsHeaders = new QgsSettingsEntryVariantMap( QStringLiteral( "http-header" ), sTreeXyzConnections ) ;
39const QgsSettingsEntryInteger *QgsXyzConnectionSettings::settingsZmin = new QgsSettingsEntryInteger( QStringLiteral( "zmin" ), sTreeXyzConnections, -1 );
40const QgsSettingsEntryInteger *QgsXyzConnectionSettings::settingsZmax = new QgsSettingsEntryInteger( QStringLiteral( "zmax" ), sTreeXyzConnections, -1 );
41const QgsSettingsEntryDouble *QgsXyzConnectionSettings::settingsTilePixelRatio = new QgsSettingsEntryDouble( QStringLiteral( "tile-pixel-ratio" ), sTreeXyzConnections, 0, QStringLiteral( "0 = unknown (not scaled), 1.0 = 256x256, 2.0 = 512x512" ) ) ;
42const QgsSettingsEntryBool *QgsXyzConnectionSettings::settingsHidden = new QgsSettingsEntryBool( QStringLiteral( "hidden" ), sTreeXyzConnections, false ) ;
43const QgsSettingsEntryString *QgsXyzConnectionSettings::settingsInterpretation = new QgsSettingsEntryString( QStringLiteral( "interpretation" ), sTreeXyzConnections, QString() ) ;
44const QgsSettingsEntryString *QgsXyzConnectionSettings::settingsUsername = new QgsSettingsEntryString( QStringLiteral( "username" ), sTreeXyzConnections ) ;
45const QgsSettingsEntryString *QgsXyzConnectionSettings::settingsPassword = new QgsSettingsEntryString( QStringLiteral( "password" ), sTreeXyzConnections ) ;
46const QgsSettingsEntryString *QgsXyzConnectionSettings::settingsAuthcfg = new QgsSettingsEntryString( QStringLiteral( "authcfg" ), sTreeXyzConnections ) ;
47
48
49const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsUrl = new QgsSettingsEntryString( QStringLiteral( "url" ), sTreeConnectionArcgis );
50const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsAuthcfg = new QgsSettingsEntryString( QStringLiteral( "authcfg" ), sTreeConnectionArcgis );
51const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsUsername = new QgsSettingsEntryString( QStringLiteral( "username" ), sTreeConnectionArcgis );
52const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsPassword = new QgsSettingsEntryString( QStringLiteral( "password" ), sTreeConnectionArcgis );
53const QgsSettingsEntryVariantMap *QgsArcGisConnectionSettings::settingsHeaders = new QgsSettingsEntryVariantMap( QStringLiteral( "http-header" ), sTreeConnectionArcgis );
54const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsContentEndpoint = new QgsSettingsEntryString( QStringLiteral( "content-endpoint" ), sTreeConnectionArcgis );
55const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsCommunityEndpoint = new QgsSettingsEntryString( QStringLiteral( "community-endpoint" ), sTreeConnectionArcgis );
56
57
58const QgsSettingsEntryString *QgsOwsConnection::settingsUrl = new QgsSettingsEntryString( QStringLiteral( "url" ), sTreeOwsConnections, QString() ) ;
59const QgsSettingsEntryVariantMap *QgsOwsConnection::settingsHeaders = new QgsSettingsEntryVariantMap( QStringLiteral( "http-header" ), sTreeOwsConnections ) ;
60const QgsSettingsEntryString *QgsOwsConnection::settingsVersion = new QgsSettingsEntryString( QStringLiteral( "version" ), sTreeOwsConnections, QString() ) ;
61const QgsSettingsEntryBool *QgsOwsConnection::settingsIgnoreGetMapURI = new QgsSettingsEntryBool( QStringLiteral( "ignore-get-map-uri" ), sTreeOwsConnections, false ) ;
62const QgsSettingsEntryBool *QgsOwsConnection::settingsIgnoreGetFeatureInfoURI = new QgsSettingsEntryBool( QStringLiteral( "ignore-get-feature-info-uri" ), sTreeOwsConnections, false ) ;
63const QgsSettingsEntryBool *QgsOwsConnection::settingsSmoothPixmapTransform = new QgsSettingsEntryBool( QStringLiteral( "smooth-pixmap-transform" ), sTreeOwsConnections, false ) ;
64const QgsSettingsEntryBool *QgsOwsConnection::settingsReportedLayerExtents = new QgsSettingsEntryBool( QStringLiteral( "reported-layer-extents" ), sTreeOwsConnections, false ) ;
67const QgsSettingsEntryString *QgsOwsConnection::settingsMaxNumFeatures = new QgsSettingsEntryString( QStringLiteral( "max-num-features" ), sTreeOwsConnections ) ;
68const QgsSettingsEntryString *QgsOwsConnection::settingsPagesize = new QgsSettingsEntryString( QStringLiteral( "page-size" ), sTreeOwsConnections ) ;
69const QgsSettingsEntryBool *QgsOwsConnection::settingsPagingEnabled = new QgsSettingsEntryBool( QStringLiteral( "paging-enabled" ), sTreeOwsConnections, true ) ;
70const QgsSettingsEntryBool *QgsOwsConnection::settingsPreferCoordinatesForWfsT11 = new QgsSettingsEntryBool( QStringLiteral( "prefer-coordinates-for-wfs-T11" ), sTreeOwsConnections, false ) ;
71const QgsSettingsEntryBool *QgsOwsConnection::settingsIgnoreAxisOrientation = new QgsSettingsEntryBool( QStringLiteral( "ignore-axis-orientation" ), sTreeOwsConnections, false ) ;
72const QgsSettingsEntryBool *QgsOwsConnection::settingsInvertAxisOrientation = new QgsSettingsEntryBool( QStringLiteral( "invert-axis-orientation" ), sTreeOwsConnections, false ) ;
73const QgsSettingsEntryString *QgsOwsConnection::settingsUsername = new QgsSettingsEntryString( QStringLiteral( "username" ), sTreeOwsConnections ) ;
74const QgsSettingsEntryString *QgsOwsConnection::settingsPassword = new QgsSettingsEntryString( QStringLiteral( "password" ), sTreeOwsConnections ) ;
75const QgsSettingsEntryString *QgsOwsConnection::settingsAuthCfg = new QgsSettingsEntryString( QStringLiteral( "authcfg" ), sTreeOwsConnections ) ;
76
77QgsOwsConnection::QgsOwsConnection( const QString &service, const QString &connName )
78 : mConnName( connName )
79 , mService( service )
80{
81 QgsDebugMsgLevel( "theConnName = " + connName, 4 );
82
83 const QString url = settingsUrl->value( {mService.toLower(), mConnName} );
84 mConnectionInfo = url;
85 mUri.setParam( QStringLiteral( "url" ), url );
86
87 // Check for credentials and prepend to the connection info
88 const QString username = settingsUsername->value( {mService.toLower(), mConnName} );
89 const QString password = settingsPassword->value( {mService.toLower(), mConnName} );
90 if ( !username.isEmpty() )
91 {
92 // check for a password, if none prompt to get it
93 mUri.setUsername( username );
94 mUri.setPassword( password );
95 }
96
97 const QString authcfg = settingsAuthCfg->value( {mService.toLower(), mConnName} );
98 if ( !authcfg.isEmpty() )
99 {
100 mUri.setAuthConfigId( authcfg );
101 }
102 mConnectionInfo.append( ",authcfg=" + authcfg );
103
104 QgsHttpHeaders httpHeaders( settingsHeaders->value( {mService.toLower(), mConnName} ) );
105 mUri.setHttpHeaders( httpHeaders );
106 const QString referer = httpHeaders[QgsHttpHeaders::KEY_REFERER].toString();
107 if ( !referer.isEmpty() )
108 {
109 mConnectionInfo.append( ",referer=" + referer );
110 }
111
112 if ( mService.compare( QLatin1String( "WMS" ), Qt::CaseInsensitive ) == 0
113 || mService.compare( QLatin1String( "WCS" ), Qt::CaseInsensitive ) == 0 )
114 {
116 }
117 else if ( mService.compare( QLatin1String( "WFS" ), Qt::CaseInsensitive ) == 0 )
118 {
120 }
121
122 QgsDebugMsgLevel( QStringLiteral( "encoded uri: '%1'." ).arg( QString( mUri.encodedUri() ) ), 4 );
123}
124
126{
127 return mConnName;
128}
129
131{
132 return mConnectionInfo;
133}
134
136{
137 return mService;
138}
139
141{
142 return mUri;
143}
144
146{
148 addCommonConnectionSettings( uri, settingsKey );
150
151 const QgsSettings settings;
152 uri.httpHeaders().setFromSettings( settings, settingsKey );
153
154 if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetMapURI" ), false ).toBool() )
155 {
156 uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
157 }
158 if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetFeatureInfoURI" ), false ).toBool() )
159 {
160 uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
161 }
162 if ( settings.value( settingsKey + QStringLiteral( "/smoothPixmapTransform" ), false ).toBool() )
163 {
164 uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
165 }
166 if ( settings.value( settingsKey + QStringLiteral( "/ignoreReportedLayerExtents" ), false ).toBool() )
167 {
168 uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
169 }
170 const QString dpiMode = settings.value( settingsKey + QStringLiteral( "/dpiMode" ), QStringLiteral( "all" ) ).toString();
171 if ( !dpiMode.isEmpty() )
172 {
173 uri.setParam( QStringLiteral( "dpiMode" ), dpiMode );
174 }
175 const QString tilePixelRatio = settings.value( settingsKey + QStringLiteral( "/tilePixelRatio" ), "0" ).toString();
176 if ( tilePixelRatio != QLatin1String( "0" ) )
177 {
178 uri.setParam( QStringLiteral( "tilePixelRatio" ), tilePixelRatio );
179 }
180
181 return uri;
182}
183
184QgsDataSourceUri &QgsOwsConnection::addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connName )
185{
186 addCommonConnectionSettings( uri, service, connName );
187
188 uri.setHttpHeaders( QgsHttpHeaders( settingsHeaders->value( {service.toLower(), connName} ) ) );
189
190 if ( settingsIgnoreGetMapURI->value( {service.toLower(), connName} ) )
191 {
192 uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
193 }
194 if ( settingsIgnoreGetFeatureInfoURI->value( {service.toLower(), connName} ) )
195 {
196 uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
197 }
198 if ( settingsSmoothPixmapTransform->value( {service.toLower(), connName} ) )
199 {
200 uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
201 }
202 if ( settingsReportedLayerExtents->value( {service.toLower(), connName} ) )
203 {
204 uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
205 }
206 if ( settingsDpiMode->exists( {service.toLower(), connName} ) )
207 {
208 uri.setParam( QStringLiteral( "dpiMode" ), QString::number( static_cast<int>( settingsDpiMode->value( {service.toLower(), connName} ) ) ) );
209 }
210 if ( settingsTilePixelRatio->exists( {service.toLower(), connName} ) )
211 {
212 uri.setParam( QStringLiteral( "tilePixelRatio" ), QString::number( static_cast<int>( settingsTilePixelRatio->value( {service.toLower(), connName} ) ) ) );
213 }
214
215 return uri;
216}
217
219{
221 addCommonConnectionSettings( uri, settingsKey );
223
224 const QgsSettings settings;
225 const QString version = settings.value( settingsKey + "/version" ).toString();
226 if ( !version.isEmpty() )
227 {
228 uri.setParam( QStringLiteral( "version" ), version );
229 }
230
231 const QString maxnumfeatures = settings.value( settingsKey + QStringLiteral( "/maxnumfeatures" ) ).toString();
232 if ( !maxnumfeatures.isEmpty() )
233 {
234 uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumfeatures );
235 }
236
237 return uri;
238}
239
240QgsDataSourceUri &QgsOwsConnection::addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connName )
241{
242 addCommonConnectionSettings( uri, service, connName );
243
244 const QString version = settingsVersion->value( {service.toLower(), connName} );
245 if ( !version.isEmpty() )
246 {
247 uri.setParam( QStringLiteral( "version" ), version );
248 }
249
250 const QString maxnumFeatures = settingsMaxNumFeatures->value( {service.toLower(), connName} );
251 if ( !maxnumFeatures.isEmpty() )
252 {
253 uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumFeatures );
254 }
255
256 return uri;
257}
258
259QStringList QgsOwsConnection::connectionList( const QString &service )
260{
262}
263
264QString QgsOwsConnection::selectedConnection( const QString &service )
265{
267}
268
269void QgsOwsConnection::setSelectedConnection( const QString &service, const QString &name )
270{
272}
273
274void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &key )
275{
276 const QgsSettings settings;
277
278 if ( settings.value( key + QStringLiteral( "/ignoreAxisOrientation" ), false ).toBool() )
279 {
280 uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
281 }
282 if ( settings.value( key + QStringLiteral( "/invertAxisOrientation" ), false ).toBool() )
283 {
284 uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
285 }
286}
287
288void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connectionName )
289{
290
291 if ( settingsIgnoreAxisOrientation->value( {service.toLower(), connectionName} ) )
292 {
293 uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
294 }
295 if ( settingsInvertAxisOrientation->value( {service.toLower(), connectionName} ) )
296 {
297 uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
298 }
299}
300
301void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
302{
303 sTreeOwsConnections->deleteItem( name, {service.toLower()} );
304}
@ 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 * 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 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 QgsSettingsEntryBool * 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:63
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:4916
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:4915
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39