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