QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 "qgslogger.h"
19 #include "qgsdatasourceuri.h"
20 #include "qgssettings.h"
21 
23 
24 QString QgsVectorTileProviderConnection::encodedUri( const QgsVectorTileProviderConnection::Data &conn )
25 {
26  QgsDataSourceUri uri;
27  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
28  uri.setParam( QStringLiteral( "url" ), conn.url );
29  if ( conn.zMin != -1 )
30  uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
31  if ( conn.zMax != -1 )
32  uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
33  if ( !conn.authCfg.isEmpty() )
34  uri.setAuthConfigId( conn.authCfg );
35  if ( !conn.username.isEmpty() )
36  uri.setUsername( conn.username );
37  if ( !conn.password.isEmpty() )
38  uri.setPassword( conn.password );
39  if ( !conn.referer.isEmpty() )
40  uri.setParam( QStringLiteral( "referer" ), conn.referer );
41  if ( !conn.styleUrl.isEmpty() )
42  uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
43 
44  switch ( conn.serviceType )
45  {
46  case Generic:
47  break;
48 
49  case ArcgisVectorTileService:
50  uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
51  break;
52  }
53 
54  return uri.encodedUri();
55 }
56 
57 QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::decodedUri( const QString &uri )
58 {
59  QgsDataSourceUri dsUri;
60  dsUri.setEncodedUri( uri );
61 
62  QgsVectorTileProviderConnection::Data conn;
63  conn.url = dsUri.param( QStringLiteral( "url" ) );
64  conn.zMin = dsUri.hasParam( QStringLiteral( "zmin" ) ) ? dsUri.param( QStringLiteral( "zmin" ) ).toInt() : -1;
65  conn.zMax = dsUri.hasParam( QStringLiteral( "zmax" ) ) ? dsUri.param( QStringLiteral( "zmax" ) ).toInt() : -1;
66  conn.authCfg = dsUri.authConfigId();
67  conn.username = dsUri.username();
68  conn.password = dsUri.password();
69  conn.referer = dsUri.param( QStringLiteral( "referer" ) );
70  conn.styleUrl = dsUri.param( QStringLiteral( "styleUrl" ) );
71 
72  if ( dsUri.hasParam( QStringLiteral( "serviceType" ) ) )
73  {
74  if ( dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
75  conn.serviceType = ArcgisVectorTileService;
76  }
77  return conn;
78 }
79 
80 QString QgsVectorTileProviderConnection::encodedLayerUri( const QgsVectorTileProviderConnection::Data &conn )
81 {
82  // compared to encodedUri() this one also adds type=xyz to the URI
83  QgsDataSourceUri uri;
84  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
85  uri.setParam( QStringLiteral( "url" ), conn.url );
86  if ( conn.zMin != -1 )
87  uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
88  if ( conn.zMax != -1 )
89  uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
90  if ( !conn.authCfg.isEmpty() )
91  uri.setAuthConfigId( conn.authCfg );
92  if ( !conn.username.isEmpty() )
93  uri.setUsername( conn.username );
94  if ( !conn.password.isEmpty() )
95  uri.setPassword( conn.password );
96  if ( !conn.referer.isEmpty() )
97  uri.setParam( QStringLiteral( "referer" ), conn.referer );
98  if ( !conn.styleUrl.isEmpty() )
99  uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
100 
101  switch ( conn.serviceType )
102  {
103  case Generic:
104  break;
105 
106  case ArcgisVectorTileService:
107  uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
108  break;
109  }
110 
111  return uri.encodedUri();
112 }
113 
114 QStringList QgsVectorTileProviderConnection::connectionList()
115 {
116  QgsSettings settings;
117  settings.beginGroup( QStringLiteral( "qgis/connections-vector-tile" ) );
118  QStringList connList = settings.childGroups();
119 
120  return connList;
121 }
122 
123 QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::connection( const QString &name )
124 {
125  QgsSettings settings;
126  settings.beginGroup( "qgis/connections-vector-tile/" + name );
127 
128  if ( settings.value( "url" ).toString().isEmpty() )
129  return QgsVectorTileProviderConnection::Data();
130 
131  QgsVectorTileProviderConnection::Data conn;
132  conn.url = settings.value( QStringLiteral( "url" ) ).toString();
133  conn.zMin = settings.value( QStringLiteral( "zmin" ), -1 ).toInt();
134  conn.zMax = settings.value( QStringLiteral( "zmax" ), -1 ).toInt();
135  conn.authCfg = settings.value( QStringLiteral( "authcfg" ) ).toString();
136  conn.username = settings.value( QStringLiteral( "username" ) ).toString();
137  conn.password = settings.value( QStringLiteral( "password" ) ).toString();
138  conn.referer = settings.value( QStringLiteral( "referer" ) ).toString();
139  conn.styleUrl = settings.value( QStringLiteral( "styleUrl" ) ).toString();
140 
141  if ( settings.contains( QStringLiteral( "serviceType" ) ) )
142  {
143  if ( settings.value( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
144  conn.serviceType = ArcgisVectorTileService;
145  }
146 
147  return conn;
148 }
149 
150 void QgsVectorTileProviderConnection::deleteConnection( const QString &name )
151 {
152  QgsSettings settings;
153  settings.remove( "qgis/connections-vector-tile/" + name );
154 }
155 
156 void QgsVectorTileProviderConnection::addConnection( const QString &name, QgsVectorTileProviderConnection::Data conn )
157 {
158  QgsSettings settings;
159 
160  settings.beginGroup( "qgis/connections-vector-tile/" + name );
161  settings.setValue( QStringLiteral( "url" ), conn.url );
162  settings.setValue( QStringLiteral( "zmin" ), conn.zMin );
163  settings.setValue( QStringLiteral( "zmax" ), conn.zMax );
164  settings.setValue( QStringLiteral( "authcfg" ), conn.authCfg );
165  settings.setValue( QStringLiteral( "username" ), conn.username );
166  settings.setValue( QStringLiteral( "password" ), conn.password );
167  settings.setValue( QStringLiteral( "referer" ), conn.referer );
168  settings.setValue( QStringLiteral( "styleUrl" ), conn.styleUrl );
169 
170  switch ( conn.serviceType )
171  {
172  case Generic:
173  break;
174 
175  case ArcgisVectorTileService:
176  settings.setValue( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
177  break;
178  }
179 }
180 
181 QString QgsVectorTileProviderConnection::selectedConnection()
182 {
183  QgsSettings settings;
184  return settings.value( QStringLiteral( "qgis/connections-vector-tile/selected" ) ).toString();
185 }
186 
187 void QgsVectorTileProviderConnection::setSelectedConnection( const QString &name )
188 {
189  QgsSettings settings;
190  return settings.setValue( QStringLiteral( "qgis/connections-vector-tile/selected" ), name );
191 }
192 
193 //
194 
195 QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &name )
197 {
198  setUri( encodedUri( connection( name ) ) );
199 }
200 
201 QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &uri, const QVariantMap &configuration )
202  : QgsAbstractProviderConnection( uri, configuration )
203 {
204 }
205 
206 void QgsVectorTileProviderConnection::store( const QString &name ) const
207 {
208  addConnection( name, decodedUri( uri() ) );
209 }
210 
211 void QgsVectorTileProviderConnection::remove( const QString &name ) const
212 {
213  deleteConnection( name );
214 }
215 
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.
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 setPassword(const QString &password)
Sets the password for the URI.