QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
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::settingsUrlPrefix = new QgsSettingsEntryString( QStringLiteral( "urlprefix" ), sTreeConnectionArcgis );
55const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsContentEndpoint = new QgsSettingsEntryString( QStringLiteral( "content-endpoint" ), sTreeConnectionArcgis );
56const QgsSettingsEntryString *QgsArcGisConnectionSettings::settingsCommunityEndpoint = new QgsSettingsEntryString( QStringLiteral( "community-endpoint" ), sTreeConnectionArcgis );
57
58
59const QgsSettingsEntryString *QgsOwsConnection::settingsUrl = new QgsSettingsEntryString( QStringLiteral( "url" ), sTreeOwsConnections, QString() ) ;
60const QgsSettingsEntryVariantMap *QgsOwsConnection::settingsHeaders = new QgsSettingsEntryVariantMap( QStringLiteral( "http-header" ), sTreeOwsConnections ) ;
61const QgsSettingsEntryString *QgsOwsConnection::settingsVersion = new QgsSettingsEntryString( QStringLiteral( "version" ), sTreeOwsConnections, QString() ) ;
62const QgsSettingsEntryBool *QgsOwsConnection::settingsIgnoreGetMapURI = new QgsSettingsEntryBool( QStringLiteral( "ignore-get-map-uri" ), sTreeOwsConnections, false ) ;
63const QgsSettingsEntryBool *QgsOwsConnection::settingsIgnoreGetFeatureInfoURI = new QgsSettingsEntryBool( QStringLiteral( "ignore-get-feature-info-uri" ), sTreeOwsConnections, false ) ;
64const QgsSettingsEntryBool *QgsOwsConnection::settingsSmoothPixmapTransform = new QgsSettingsEntryBool( QStringLiteral( "smooth-pixmap-transform" ), sTreeOwsConnections, false ) ;
65const QgsSettingsEntryBool *QgsOwsConnection::settingsReportedLayerExtents = new QgsSettingsEntryBool( QStringLiteral( "reported-layer-extents" ), sTreeOwsConnections, false ) ;
68const QgsSettingsEntryString *QgsOwsConnection::settingsMaxNumFeatures = new QgsSettingsEntryString( QStringLiteral( "max-num-features" ), sTreeOwsConnections ) ;
69const QgsSettingsEntryString *QgsOwsConnection::settingsPagesize = new QgsSettingsEntryString( QStringLiteral( "page-size" ), sTreeOwsConnections ) ;
70const QgsSettingsEntryString *QgsOwsConnection::settingsPagingEnabled = new QgsSettingsEntryString( QStringLiteral( "paging-enabled" ), sTreeOwsConnections, QString( "default" ) ) ;
71const QgsSettingsEntryBool *QgsOwsConnection::settingsPreferCoordinatesForWfsT11 = new QgsSettingsEntryBool( QStringLiteral( "prefer-coordinates-for-wfs-T11" ), sTreeOwsConnections, false ) ;
72const QgsSettingsEntryBool *QgsOwsConnection::settingsIgnoreAxisOrientation = new QgsSettingsEntryBool( QStringLiteral( "ignore-axis-orientation" ), sTreeOwsConnections, false ) ;
73const QgsSettingsEntryBool *QgsOwsConnection::settingsInvertAxisOrientation = new QgsSettingsEntryBool( QStringLiteral( "invert-axis-orientation" ), sTreeOwsConnections, false ) ;
74const QgsSettingsEntryString *QgsOwsConnection::settingsUsername = new QgsSettingsEntryString( QStringLiteral( "username" ), sTreeOwsConnections ) ;
75const QgsSettingsEntryString *QgsOwsConnection::settingsPassword = new QgsSettingsEntryString( QStringLiteral( "password" ), sTreeOwsConnections ) ;
76const QgsSettingsEntryString *QgsOwsConnection::settingsAuthCfg = new QgsSettingsEntryString( QStringLiteral( "authcfg" ), sTreeOwsConnections ) ;
77const QgsSettingsEntryInteger *QgsOwsConnection::settingsFeatureCount = new QgsSettingsEntryInteger( QStringLiteral( "feature-count" ), sTreeOwsConnections, 10 );
78
79QgsOwsConnection::QgsOwsConnection( const QString &service, const QString &connName )
80 : mConnName( connName )
81 , mService( service )
82{
83 QgsDebugMsgLevel( "theConnName = " + connName, 4 );
84
85 const QString url = settingsUrl->value( {mService.toLower(), mConnName} );
86 mConnectionInfo = url;
87 mUri.setParam( QStringLiteral( "url" ), url );
88
89 // Check for credentials and prepend to the connection info
90 const QString username = settingsUsername->value( {mService.toLower(), mConnName} );
91 const QString password = settingsPassword->value( {mService.toLower(), mConnName} );
92 if ( !username.isEmpty() )
93 {
94 // check for a password, if none prompt to get it
95 mUri.setUsername( username );
96 mUri.setPassword( password );
97 }
98
99 const QString authcfg = settingsAuthCfg->value( {mService.toLower(), mConnName} );
100 if ( !authcfg.isEmpty() )
101 {
102 mUri.setAuthConfigId( authcfg );
103 }
104 mConnectionInfo.append( ",authcfg=" + authcfg );
105
106 QgsHttpHeaders httpHeaders( settingsHeaders->value( {mService.toLower(), mConnName} ) );
107 mUri.setHttpHeaders( httpHeaders );
108 const QString referer = httpHeaders[QgsHttpHeaders::KEY_REFERER].toString();
109 if ( !referer.isEmpty() )
110 {
111 mConnectionInfo.append( ",referer=" + referer );
112 }
113
114 if ( mService.compare( QLatin1String( "WMS" ), Qt::CaseInsensitive ) == 0
115 || mService.compare( QLatin1String( "WCS" ), Qt::CaseInsensitive ) == 0 )
116 {
118 }
119 else if ( mService.compare( QLatin1String( "WFS" ), Qt::CaseInsensitive ) == 0 )
120 {
122 }
123
124 QgsDebugMsgLevel( QStringLiteral( "encoded uri: '%1'." ).arg( QString( mUri.encodedUri() ) ), 4 );
125}
126
128{
129 return mConnName;
130}
131
133{
134 return mConnectionInfo;
135}
136
138{
139 return mService;
140}
141
143{
144 return mUri;
145}
146
148{
150 addCommonConnectionSettings( uri, settingsKey );
152
153 const QgsSettings settings;
154 uri.httpHeaders().setFromSettings( settings, settingsKey );
155
156 if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetMapURI" ), false ).toBool() )
157 {
158 uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
159 }
160 if ( settings.value( settingsKey + QStringLiteral( "/ignoreGetFeatureInfoURI" ), false ).toBool() )
161 {
162 uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
163 }
164 if ( settings.value( settingsKey + QStringLiteral( "/smoothPixmapTransform" ), false ).toBool() )
165 {
166 uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
167 }
168 if ( settings.value( settingsKey + QStringLiteral( "/ignoreReportedLayerExtents" ), false ).toBool() )
169 {
170 uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
171 }
172 const QString dpiMode = settings.value( settingsKey + QStringLiteral( "/dpiMode" ), QStringLiteral( "all" ) ).toString();
173 if ( !dpiMode.isEmpty() )
174 {
175 uri.setParam( QStringLiteral( "dpiMode" ), dpiMode );
176 }
177 const QString tilePixelRatio = settings.value( settingsKey + QStringLiteral( "/tilePixelRatio" ), "0" ).toString();
178 if ( tilePixelRatio != QLatin1String( "0" ) )
179 {
180 uri.setParam( QStringLiteral( "tilePixelRatio" ), tilePixelRatio );
181 }
182
183 return uri;
184}
185
186QgsDataSourceUri &QgsOwsConnection::addWmsWcsConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connName )
187{
188 addCommonConnectionSettings( uri, service, connName );
189
190 uri.setHttpHeaders( QgsHttpHeaders( settingsHeaders->value( {service.toLower(), connName} ) ) );
191
192 if ( settingsIgnoreGetMapURI->value( {service.toLower(), connName} ) )
193 {
194 uri.setParam( QStringLiteral( "IgnoreGetMapUrl" ), QStringLiteral( "1" ) );
195 }
196 if ( settingsIgnoreGetFeatureInfoURI->value( {service.toLower(), connName} ) )
197 {
198 uri.setParam( QStringLiteral( "IgnoreGetFeatureInfoUrl" ), QStringLiteral( "1" ) );
199 }
200 if ( settingsSmoothPixmapTransform->value( {service.toLower(), connName} ) )
201 {
202 uri.setParam( QStringLiteral( "SmoothPixmapTransform" ), QStringLiteral( "1" ) );
203 }
204 if ( settingsReportedLayerExtents->value( {service.toLower(), connName} ) )
205 {
206 uri.setParam( QStringLiteral( "IgnoreReportedLayerExtents" ), QStringLiteral( "1" ) );
207 }
208 if ( settingsDpiMode->exists( {service.toLower(), connName} ) )
209 {
210 uri.setParam( QStringLiteral( "dpiMode" ), QString::number( static_cast<int>( settingsDpiMode->value( {service.toLower(), connName} ) ) ) );
211 }
212 if ( settingsTilePixelRatio->exists( {service.toLower(), connName} ) )
213 {
214 uri.setParam( QStringLiteral( "tilePixelRatio" ), QString::number( static_cast<int>( settingsTilePixelRatio->value( {service.toLower(), connName} ) ) ) );
215 }
216 if ( settingsFeatureCount->exists( {service.toLower(), connName} ) )
217 {
218 uri.setParam( QStringLiteral( "featureCount" ), QString::number( settingsFeatureCount->value( {service.toLower(), connName} ) ) );
219 }
220
221 return uri;
222}
223
225{
227 addCommonConnectionSettings( uri, settingsKey );
229
230 const QgsSettings settings;
231 const QString version = settings.value( settingsKey + "/version" ).toString();
232 if ( !version.isEmpty() )
233 {
234 uri.setParam( QStringLiteral( "version" ), version );
235 }
236
237 const QString maxnumfeatures = settings.value( settingsKey + QStringLiteral( "/maxnumfeatures" ) ).toString();
238 if ( !maxnumfeatures.isEmpty() )
239 {
240 uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumfeatures );
241 }
242
243 return uri;
244}
245
246QgsDataSourceUri &QgsOwsConnection::addWfsConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connName )
247{
248 addCommonConnectionSettings( uri, service, connName );
249
250 const QString version = settingsVersion->value( {service.toLower(), connName} );
251 if ( !version.isEmpty() )
252 {
253 uri.setParam( QStringLiteral( "version" ), version );
254 }
255
256 const QString maxnumFeatures = settingsMaxNumFeatures->value( {service.toLower(), connName} );
257 if ( !maxnumFeatures.isEmpty() )
258 {
259 uri.setParam( QStringLiteral( "maxNumFeatures" ), maxnumFeatures );
260 }
261
262 return uri;
263}
264
265QStringList QgsOwsConnection::connectionList( const QString &service )
266{
268}
269
270QString QgsOwsConnection::selectedConnection( const QString &service )
271{
273}
274
275void QgsOwsConnection::setSelectedConnection( const QString &service, const QString &name )
276{
278}
279
280void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &key )
281{
282 const QgsSettings settings;
283
284 if ( settings.value( key + QStringLiteral( "/ignoreAxisOrientation" ), false ).toBool() )
285 {
286 uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
287 }
288 if ( settings.value( key + QStringLiteral( "/invertAxisOrientation" ), false ).toBool() )
289 {
290 uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
291 }
292}
293
294void QgsOwsConnection::addCommonConnectionSettings( QgsDataSourceUri &uri, const QString &service, const QString &connectionName )
295{
296
297 if ( settingsIgnoreAxisOrientation->value( {service.toLower(), connectionName} ) )
298 {
299 uri.setParam( QStringLiteral( "IgnoreAxisOrientation" ), QStringLiteral( "1" ) );
300 }
301 if ( settingsInvertAxisOrientation->value( {service.toLower(), connectionName} ) )
302 {
303 uri.setParam( QStringLiteral( "InvertAxisOrientation" ), QStringLiteral( "1" ) );
304 }
305}
306
307void QgsOwsConnection::deleteConnection( const QString &service, const QString &name )
308{
309 sTreeOwsConnections->deleteItem( name, {service.toLower()} );
310}
@ 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:6494
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6493
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39