QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
22 #include <QFileInfo>
23 
25 
26 QString QgsVectorTileProviderConnection::encodedUri( const QgsVectorTileProviderConnection::Data &conn )
27 {
28  QgsDataSourceUri uri;
29 
30  const QFileInfo info( conn.url );
31  QString suffix = info.suffix().toLower();
32  if ( suffix.startsWith( QStringLiteral( "mbtiles" ) ) )
33  {
34  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
35  }
36  else
37  {
38  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
39  }
40 
41  uri.setParam( QStringLiteral( "url" ), conn.url );
42  if ( conn.zMin != -1 )
43  uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
44  if ( conn.zMax != -1 )
45  uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
46  if ( !conn.authCfg.isEmpty() )
47  uri.setAuthConfigId( conn.authCfg );
48  if ( !conn.username.isEmpty() )
49  uri.setUsername( conn.username );
50  if ( !conn.password.isEmpty() )
51  uri.setPassword( conn.password );
52  if ( !conn.referer.isEmpty() )
53  uri.setParam( QStringLiteral( "referer" ), conn.referer );
54  if ( !conn.styleUrl.isEmpty() )
55  uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
56 
57  switch ( conn.serviceType )
58  {
59  case Generic:
60  break;
61 
62  case ArcgisVectorTileService:
63  uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
64  break;
65  }
66 
67  return uri.encodedUri();
68 }
69 
70 QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::decodedUri( const QString &uri )
71 {
72  QgsDataSourceUri dsUri;
73  dsUri.setEncodedUri( uri );
74 
75  QgsVectorTileProviderConnection::Data conn;
76  conn.url = dsUri.param( QStringLiteral( "url" ) );
77  conn.zMin = dsUri.hasParam( QStringLiteral( "zmin" ) ) ? dsUri.param( QStringLiteral( "zmin" ) ).toInt() : -1;
78  conn.zMax = dsUri.hasParam( QStringLiteral( "zmax" ) ) ? dsUri.param( QStringLiteral( "zmax" ) ).toInt() : -1;
79  conn.authCfg = dsUri.authConfigId();
80  conn.username = dsUri.username();
81  conn.password = dsUri.password();
82  conn.referer = dsUri.param( QStringLiteral( "referer" ) );
83  conn.styleUrl = dsUri.param( QStringLiteral( "styleUrl" ) );
84 
85  if ( dsUri.hasParam( QStringLiteral( "serviceType" ) ) )
86  {
87  if ( dsUri.param( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
88  conn.serviceType = ArcgisVectorTileService;
89  }
90  return conn;
91 }
92 
93 QString QgsVectorTileProviderConnection::encodedLayerUri( const QgsVectorTileProviderConnection::Data &conn )
94 {
95  // compared to encodedUri() this one also adds type=xyz to the URI
96  QgsDataSourceUri uri;
97 
98  const QFileInfo info( conn.url );
99  QString suffix = info.suffix().toLower();
100  if ( suffix.startsWith( QStringLiteral( "mbtiles" ) ) )
101  {
102  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
103  }
104  else
105  {
106  uri.setParam( QStringLiteral( "type" ), QStringLiteral( "xyz" ) );
107  }
108 
109  uri.setParam( QStringLiteral( "url" ), conn.url );
110  if ( conn.zMin != -1 )
111  uri.setParam( QStringLiteral( "zmin" ), QString::number( conn.zMin ) );
112  if ( conn.zMax != -1 )
113  uri.setParam( QStringLiteral( "zmax" ), QString::number( conn.zMax ) );
114  if ( !conn.authCfg.isEmpty() )
115  uri.setAuthConfigId( conn.authCfg );
116  if ( !conn.username.isEmpty() )
117  uri.setUsername( conn.username );
118  if ( !conn.password.isEmpty() )
119  uri.setPassword( conn.password );
120  if ( !conn.referer.isEmpty() )
121  uri.setParam( QStringLiteral( "referer" ), conn.referer );
122  if ( !conn.styleUrl.isEmpty() )
123  uri.setParam( QStringLiteral( "styleUrl" ), conn.styleUrl );
124 
125  switch ( conn.serviceType )
126  {
127  case Generic:
128  break;
129 
130  case ArcgisVectorTileService:
131  uri.setParam( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
132  break;
133  }
134 
135  return uri.encodedUri();
136 }
137 
138 QStringList QgsVectorTileProviderConnection::connectionList()
139 {
140  QgsSettings settings;
141  settings.beginGroup( QStringLiteral( "qgis/connections-vector-tile" ) );
142  QStringList connList = settings.childGroups();
143 
144  return connList;
145 }
146 
147 QgsVectorTileProviderConnection::Data QgsVectorTileProviderConnection::connection( const QString &name )
148 {
149  QgsSettings settings;
150  settings.beginGroup( "qgis/connections-vector-tile/" + name );
151 
152  if ( settings.value( "url" ).toString().isEmpty() )
153  return QgsVectorTileProviderConnection::Data();
154 
155  QgsVectorTileProviderConnection::Data conn;
156  conn.url = settings.value( QStringLiteral( "url" ) ).toString();
157  conn.zMin = settings.value( QStringLiteral( "zmin" ), -1 ).toInt();
158  conn.zMax = settings.value( QStringLiteral( "zmax" ), -1 ).toInt();
159  conn.authCfg = settings.value( QStringLiteral( "authcfg" ) ).toString();
160  conn.username = settings.value( QStringLiteral( "username" ) ).toString();
161  conn.password = settings.value( QStringLiteral( "password" ) ).toString();
162  conn.referer = settings.value( QStringLiteral( "referer" ) ).toString();
163  conn.styleUrl = settings.value( QStringLiteral( "styleUrl" ) ).toString();
164 
165  if ( settings.contains( QStringLiteral( "serviceType" ) ) )
166  {
167  if ( settings.value( QStringLiteral( "serviceType" ) ) == QLatin1String( "arcgis" ) )
168  conn.serviceType = ArcgisVectorTileService;
169  }
170 
171  return conn;
172 }
173 
174 void QgsVectorTileProviderConnection::deleteConnection( const QString &name )
175 {
176  QgsSettings settings;
177  settings.remove( "qgis/connections-vector-tile/" + name );
178 }
179 
180 void QgsVectorTileProviderConnection::addConnection( const QString &name, QgsVectorTileProviderConnection::Data conn )
181 {
182  QgsSettings settings;
183 
184  settings.beginGroup( "qgis/connections-vector-tile/" + name );
185  settings.setValue( QStringLiteral( "url" ), conn.url );
186  settings.setValue( QStringLiteral( "zmin" ), conn.zMin );
187  settings.setValue( QStringLiteral( "zmax" ), conn.zMax );
188  settings.setValue( QStringLiteral( "authcfg" ), conn.authCfg );
189  settings.setValue( QStringLiteral( "username" ), conn.username );
190  settings.setValue( QStringLiteral( "password" ), conn.password );
191  settings.setValue( QStringLiteral( "referer" ), conn.referer );
192  settings.setValue( QStringLiteral( "styleUrl" ), conn.styleUrl );
193 
194  switch ( conn.serviceType )
195  {
196  case Generic:
197  break;
198 
199  case ArcgisVectorTileService:
200  settings.setValue( QStringLiteral( "serviceType" ), QStringLiteral( "arcgis" ) );
201  break;
202  }
203 }
204 
205 QString QgsVectorTileProviderConnection::selectedConnection()
206 {
207  const QgsSettings settings;
208  return settings.value( QStringLiteral( "qgis/connections-vector-tile/selected" ) ).toString();
209 }
210 
211 void QgsVectorTileProviderConnection::setSelectedConnection( const QString &name )
212 {
213  QgsSettings settings;
214  return settings.setValue( QStringLiteral( "qgis/connections-vector-tile/selected" ), name );
215 }
216 
217 //
218 
219 QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &name )
221 {
222  setUri( encodedUri( connection( name ) ) );
223 }
224 
225 QgsVectorTileProviderConnection::QgsVectorTileProviderConnection( const QString &uri, const QVariantMap &configuration )
226  : QgsAbstractProviderConnection( uri, configuration )
227 {
228 }
229 
230 void QgsVectorTileProviderConnection::store( const QString &name ) const
231 {
232  addConnection( name, decodedUri( uri() ) );
233 }
234 
235 void QgsVectorTileProviderConnection::remove( const QString &name ) const
236 {
237  deleteConnection( name );
238 }
239 
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.
This class is a composition of two QSettings instances:
Definition: qgssettings.h:62
QStringList childGroups() const
Returns a list of all key top-level groups that contain keys that can be read using the QSettings obj...
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool contains(const QString &key, QgsSettings::Section section=QgsSettings::NoSection) const
Returns true if there exists a setting called key; returns false otherwise.
void beginGroup(const QString &prefix, QgsSettings::Section section=QgsSettings::NoSection)
Appends prefix to the current group.
Definition: qgssettings.cpp:89
void remove(const QString &key, QgsSettings::Section section=QgsSettings::NoSection)
Removes the setting key and any sub-settings of key in a section.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.