QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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#include <QFileInfo>
23
25
26const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsUrl = new QgsSettingsEntryString( QStringLiteral( "url" ), sTreeConnectionVectorTile );
27const QgsSettingsEntryInteger *QgsVectorTileProviderConnection::settingsZmin = new QgsSettingsEntryInteger( QStringLiteral( "zmin" ), sTreeConnectionVectorTile, -1 );
28const QgsSettingsEntryInteger *QgsVectorTileProviderConnection::settingsZmax = new QgsSettingsEntryInteger( QStringLiteral( "zmax" ), sTreeConnectionVectorTile, -1 );
29const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsAuthcfg = new QgsSettingsEntryString( QStringLiteral( "authcfg" ), sTreeConnectionVectorTile );
30const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsUsername = new QgsSettingsEntryString( QStringLiteral( "username" ), sTreeConnectionVectorTile );
31const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsPassword = new QgsSettingsEntryString( QStringLiteral( "password" ), sTreeConnectionVectorTile );
32const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsStyleUrl = new QgsSettingsEntryString( QStringLiteral( "styleUrl" ), sTreeConnectionVectorTile );
33const QgsSettingsEntryString *QgsVectorTileProviderConnection::settingsServiceType = new QgsSettingsEntryString( QStringLiteral( "service-type" ), sTreeConnectionVectorTile );
34const QgsSettingsEntryVariantMap *QgsVectorTileProviderConnection::settingsHeaders = new QgsSettingsEntryVariantMap( QStringLiteral( "http-header" ), sTreeConnectionVectorTile );
35
36
37QString QgsVectorTileProviderConnection::encodedUri( const QgsVectorTileProviderConnection::Data &conn )
38{
40
41 const QFileInfo info( conn.url );
42 QString suffix = info.suffix().toLower();
43 if ( suffix.startsWith( QLatin1String( "mbtiles" ) ) )
44 {
45 uri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
46 }
47 else
48 {
49 uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
50 }
51
52 uri.setParam( QStringLiteral( "url" ), conn.url );
53 if ( conn.zMin != -1 )
54 uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
55 if ( conn.zMax != -1 )
56 uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
57 if ( !conn.authCfg.isEmpty() )
58 uri.setAuthConfigId( conn.authCfg );
59 if ( !conn.username.isEmpty() )
60 uri.setUsername( conn.username );
61 if ( !conn.password.isEmpty() )
62 uri.setPassword( conn.password );
63 if ( !conn.styleUrl.isEmpty() )
64 uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
65
66 uri.setHttpHeaders( conn.httpHeaders );
67
68 switch ( conn.serviceType )
69 {
70 case Generic:
71 break;
72
73 case ArcgisVectorTileService:
74 uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
75 break;
76 }
77
78 return uri.encodedUri();
79}
80
81QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::decodedUri( const QString &uri )
82{
83 QgsDataSourceUri dsUri;
84 dsUri.setEncodedUri( uri );
85
86 QgsVectorTileProviderConnection::Data conn;
87 conn.url = dsUri.param( QStringLiteral( "url" ) );
88 conn.zMin = dsUri.hasParam( QStringLiteral( "zmin" ) ) ? dsUri.param( QStringLiteral( "zmin" ) ).toInt() : -1;
89 conn.zMax = dsUri.hasParam( QStringLiteral( "zmax" ) ) ? dsUri.param( QStringLiteral( "zmax" ) ).toInt() : -1;
90 conn.authCfg = dsUri.authConfigId();
91 conn.username = dsUri.username();
92 conn.password = dsUri.password();
93 conn.styleUrl = dsUri.param( QStringLiteral( "styleUrl" ) );
94
95 conn.httpHeaders = dsUri.httpHeaders();
96
97 if ( dsUri.hasParam( QStringLiteral( "serviceType" ) ) )
98 {
99 if ( dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
100 conn.serviceType = ArcgisVectorTileService;
101 }
102 return conn;
103}
104
105QString QgsVectorTileProviderConnection::encodedLayerUri( const QgsVectorTileProviderConnection::Data &conn )
106{
107 // compared to encodedUri() this one also adds type=xyz to the URI
109
110 const QFileInfo info( conn.url );
111 QString suffix = info.suffix().toLower();
112 if ( suffix.startsWith( QLatin1String( "mbtiles" ) ) )
113 {
114 uri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
115 }
116 else
117 {
118 uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
119 }
120
121 uri.setParam( QStringLiteral( "url" ), conn.url );
122 if ( conn.zMin != -1 )
123 uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
124 if ( conn.zMax != -1 )
125 uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
126 if ( !conn.authCfg.isEmpty() )
127 uri.setAuthConfigId( conn.authCfg );
128 if ( !conn.username.isEmpty() )
129 uri.setUsername( conn.username );
130 if ( !conn.password.isEmpty() )
131 uri.setPassword( conn.password );
132 if ( !conn.styleUrl.isEmpty() )
133 uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
134
135 uri.setHttpHeaders( conn.httpHeaders );
136
137 switch ( conn.serviceType )
138 {
139 case Generic:
140 break;
141
142 case ArcgisVectorTileService:
143 uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
144 break;
145 }
146
147 return uri.encodedUri();
148}
149
150QStringList QgsVectorTileProviderConnection::connectionList()
151{
152 return QgsVectorTileProviderConnection::sTreeConnectionVectorTile->items();
153}
154
155QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::connection( const QString &name )
156{
157 if ( !settingsUrl->exists( name ) && !settingsStyleUrl->exists( name ) )
158 return QgsVectorTileProviderConnection::Data();
159
160 QgsVectorTileProviderConnection::Data conn;
161 conn.url = settingsUrl->value( name );
162 conn.zMin = settingsZmin->value( name );
163 conn.zMax = settingsZmax->value( name );
164 conn.authCfg = settingsAuthcfg->value( name );
165 conn.username = settingsUsername->value( name );
166 conn.password = settingsPassword->value( name );
167 conn.styleUrl = settingsStyleUrl->value( name );
168
169 if ( settingsHeaders->exists( name ) )
170 conn.httpHeaders = QgsHttpHeaders( settingsHeaders->value( name ) );
171
172 if ( settingsServiceType->exists( name ) && settingsServiceType->value( name ) == QLatin1String( "arcgis" ) )
173 conn.serviceType = ArcgisVectorTileService;
174
175 return conn;
176}
177
178void QgsVectorTileProviderConnection::deleteConnection( const QString &name )
179{
180 sTreeConnectionVectorTile->deleteItem( name );
181}
182
183void QgsVectorTileProviderConnection::addConnection( const QString &name, QgsVectorTileProviderConnection::Data conn )
184{
185 settingsUrl->setValue( conn.url, name );
186 settingsZmin->setValue( conn.zMin, name );
187 settingsZmax->setValue( conn.zMax, name );
188 settingsAuthcfg->setValue( conn.authCfg, name );
189 settingsUsername->setValue( conn.username, name );
190 settingsPassword->setValue( conn.password, name );
191 settingsStyleUrl->setValue( conn.styleUrl, name );
192
193 settingsHeaders->setValue( conn.httpHeaders.headers(), name );
194
195 switch ( conn.serviceType )
196 {
197 case Generic:
198 break;
199
200 case ArcgisVectorTileService:
201 settingsServiceType->setValue( QStringLiteral( "arcgis" ), name );
202 break;
203 }
204}
205
206QString QgsVectorTileProviderConnection::selectedConnection()
207{
208 return sTreeConnectionVectorTile->selectedItem();
209}
210
211void QgsVectorTileProviderConnection::setSelectedConnection( const QString &name )
212{
213 sTreeConnectionVectorTile->setSelectedItem( name );
214}
215
216
217QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &name )
219{
220 setUri( encodedUri( connection( name ) ) );
221}
222
223QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &uri, const QVariantMap &configuration )
224 : QgsAbstractProviderConnection( uri, configuration )
225{
226}
227
228void QgsVectorTileProviderConnection::store( const QString &name ) const
229{
230 addConnection( name, decodedUri( uri() ) );
231}
232
233void QgsVectorTileProviderConnection::remove( const QString &name ) const
234{
235 deleteConnection( name );
236}
237
An interface for data provider connections.
Stores the component parts of a 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.
Implements simple HTTP header management.
An integer settings entry.
A string settings entry.
A string list settings entry.