QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsvectortileconnection.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectortileconnection.cpp
3 ---------------------
4 begin : March 2020
5 copyright : (C) 2020 by Martin Dobias
6 email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgsdatasourceuri.h"
19#include "qgshttpheaders.h"
21
22
23#include <QFileInfo>
24
26
27const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsUrl = new QgsSettingsEntryString( QStringLiteral( "url" ), sTreeConnectionVectorTile );
28const QgsSettingsEntryInteger *QgsVectorTileProviderConnection::settingsZmin = new QgsSettingsEntryInteger( QStringLiteral( "zmin" ), sTreeConnectionVectorTile, -1 );
29const QgsSettingsEntryInteger *QgsVectorTileProviderConnection::settingsZmax = new QgsSettingsEntryInteger( QStringLiteral( "zmax" ), sTreeConnectionVectorTile, -1 );
30const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsAuthcfg = new QgsSettingsEntryString( QStringLiteral( "authcfg" ), sTreeConnectionVectorTile );
31const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsUsername = new QgsSettingsEntryString( QStringLiteral( "username" ), sTreeConnectionVectorTile );
32const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsPassword = new QgsSettingsEntryString( QStringLiteral( "password" ), sTreeConnectionVectorTile );
33const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsStyleUrl = new QgsSettingsEntryString( QStringLiteral( "styleUrl" ), sTreeConnectionVectorTile );
34const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsServiceType = new QgsSettingsEntryString( QStringLiteral( "service-type" ), sTreeConnectionVectorTile );
35const QgsSettingsEntryVariantMap *QgsVectorTileProviderConnection::settingsHeaders = new QgsSettingsEntryVariantMap( QStringLiteral( "http-header" ), sTreeConnectionVectorTile );
36
37
38QString QgsVectorTileProviderConnection::encodedUri( const QgsVectorTileProviderConnection::Data &conn )
39{
41
42 const QFileInfo info( conn.url );
43 QString suffix = info.suffix().toLower();
44 if ( suffix.startsWith( QLatin1String( "mbtiles" ) ) )
45 {
46 uri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
47 }
48 else
49 {
50 uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
51 }
52
53 uri.setParam( QStringLiteral( "url" ), conn.url );
54 if ( conn.zMin != -1 )
55 uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
56 if ( conn.zMax != -1 )
57 uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
58 if ( !conn.authCfg.isEmpty() )
59 uri.setAuthConfigId( conn.authCfg );
60 if ( !conn.username.isEmpty() )
61 uri.setUsername( conn.username );
62 if ( !conn.password.isEmpty() )
63 uri.setPassword( conn.password );
64 if ( !conn.styleUrl.isEmpty() )
65 uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
66
67 uri.setHttpHeaders( conn.httpHeaders );
68
69 switch ( conn.serviceType )
70 {
71 case Generic:
72 break;
73
74 case ArcgisVectorTileService:
75 uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
76 break;
77 }
78
79 return uri.encodedUri();
80}
81
82QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::decodedUri( const QString &uri )
83{
84 QgsDataSourceUri dsUri;
85 dsUri.setEncodedUri( uri );
86
87 QgsVectorTileProviderConnection::Data conn;
88 conn.url = dsUri.param( QStringLiteral( "url" ) );
89 conn.zMin = dsUri.hasParam( QStringLiteral( "zmin" ) ) ? dsUri.param( QStringLiteral( "zmin" ) ).toInt() : -1;
90 conn.zMax = dsUri.hasParam( QStringLiteral( "zmax" ) ) ? dsUri.param( QStringLiteral( "zmax" ) ).toInt() : -1;
91 conn.authCfg = dsUri.authConfigId();
92 conn.username = dsUri.username();
93 conn.password = dsUri.password();
94 conn.styleUrl = dsUri.param( QStringLiteral( "styleUrl" ) );
95
96 conn.httpHeaders = dsUri.httpHeaders();
97
98 if ( dsUri.hasParam( QStringLiteral( "serviceType" ) ) )
99 {
100 if ( dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
101 conn.serviceType = ArcgisVectorTileService;
102 }
103 return conn;
104}
105
106QString QgsVectorTileProviderConnection::encodedLayerUri( const QgsVectorTileProviderConnection::Data &conn )
107{
108 // compared to encodedUri() this one also adds type=xyz to the URI
110
111 const QFileInfo info( conn.url );
112 QString suffix = info.suffix().toLower();
113 if ( suffix.startsWith( QLatin1String( "mbtiles" ) ) )
114 {
115 uri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
116 }
117 else
118 {
119 uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
120 }
121
122 uri.setParam( QStringLiteral( "url" ), conn.url );
123 if ( conn.zMin != -1 )
124 uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
125 if ( conn.zMax != -1 )
126 uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
127 if ( !conn.authCfg.isEmpty() )
128 uri.setAuthConfigId( conn.authCfg );
129 if ( !conn.username.isEmpty() )
130 uri.setUsername( conn.username );
131 if ( !conn.password.isEmpty() )
132 uri.setPassword( conn.password );
133 if ( !conn.styleUrl.isEmpty() )
134 uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
135
136 uri.setHttpHeaders( conn.httpHeaders );
137
138 switch ( conn.serviceType )
139 {
140 case Generic:
141 break;
142
143 case ArcgisVectorTileService:
144 uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
145 break;
146 }
147
148 return uri.encodedUri();
149}
150
151QStringList QgsVectorTileProviderConnection::connectionList()
152{
153 return QgsVectorTileProviderConnection::sTreeConnectionVectorTile->items();
154}
155
156QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::connection( const QString &name )
157{
158 if ( !settingsUrl->exists( name ) )
159 return QgsVectorTileProviderConnection::Data();
160
161 QgsVectorTileProviderConnection::Data conn;
162 conn.url = settingsUrl->value( name );
163 conn.zMin = settingsZmin->value( name );
164 conn.zMax = settingsZmax->value( name );
165 conn.authCfg = settingsAuthcfg->value( name );
166 conn.username = settingsUsername->value( name );
167 conn.password = settingsPassword->value( name );
168 conn.styleUrl = settingsStyleUrl->value( name );
169
170 if ( settingsHeaders->exists( name ) )
171 conn.httpHeaders = QgsHttpHeaders( settingsHeaders->value( name ) );
172
173 if ( settingsServiceType->exists( name ) && settingsServiceType->value( name ) == QLatin1String( "arcgis" ) )
174 conn.serviceType = ArcgisVectorTileService;
175
176 return conn;
177}
178
179void QgsVectorTileProviderConnection::deleteConnection( const QString &name )
180{
181 sTreeConnectionVectorTile->deleteItem( name );
182}
183
184void QgsVectorTileProviderConnection::addConnection( const QString &name, QgsVectorTileProviderConnection::Data conn )
185{
186 settingsUrl->setValue( conn.url, name );
187 settingsZmin->setValue( conn.zMin, name );
188 settingsZmax->setValue( conn.zMax, name );
189 settingsAuthcfg->setValue( conn.authCfg, name );
190 settingsUsername->setValue( conn.username, name );
191 settingsPassword->setValue( conn.password, name );
192 settingsStyleUrl->setValue( conn.styleUrl, name );
193
194 settingsHeaders->setValue( conn.httpHeaders.headers(), name );
195
196 switch ( conn.serviceType )
197 {
198 case Generic:
199 break;
200
201 case ArcgisVectorTileService:
202 settingsServiceType->setValue( QStringLiteral( "arcgis" ), name );
203 break;
204 }
205}
206
207QString QgsVectorTileProviderConnection::selectedConnection()
208{
209 return sTreeConnectionVectorTile->selectedItem();
210}
211
212void QgsVectorTileProviderConnection::setSelectedConnection( const QString &name )
213{
214 sTreeConnectionVectorTile->setSelectedItem( name );
215}
216
217
218QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &name )
220{
221 setUri( encodedUri( connection( name ) ) );
222}
223
224QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &uri, const QVariantMap &configuration )
225 : QgsAbstractProviderConnection( uri, configuration )
226{
227}
228
229void QgsVectorTileProviderConnection::store( const QString &name ) const
230{
231 addConnection( name, decodedUri( uri() ) );
232}
233
234void QgsVectorTileProviderConnection::remove( const QString &name ) const
235{
236 deleteConnection( name );
237}
238
The QgsAbstractProviderConnection provides an interface for data provider connections.
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.
bool hasParam(const QString &key) const
Returns true if a parameter with the specified key exists.
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
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.
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
QString username() const
Returns the username stored in the URI.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
QString password() const
Returns the password stored in the URI.
QString authConfigId() const
Returns any associated authentication configuration ID stored in 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.
An integer settings entry.
A string settings entry.
A string list settings entry.