QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgsvectortileprovidermetadata.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsvectortileprovidermetadata.cpp
3 --------------------------------------
4 Date : 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
20#include "qgsapplication.h"
21#include "qgsreadwritecontext.h"
22
23#include <QUrl>
24
26
27#define PROVIDER_KEY QStringLiteral( "vectortile" )
28#define PROVIDER_DESCRIPTION QStringLiteral( "Vector tile provider" )
29
30QgsVectorTileProviderMetadata::QgsVectorTileProviderMetadata()
31 : QgsProviderMetadata( PROVIDER_KEY, PROVIDER_DESCRIPTION )
32{
33}
34
35QIcon QgsVectorTileProviderMetadata::icon() const
36{
37 return QgsApplication::getThemeIcon( QStringLiteral( "mIconVectorTileLayer.svg" ) );
38}
39
40QList<QgsDataItemProvider *> QgsVectorTileProviderMetadata::dataItemProviders() const
41{
42 QList< QgsDataItemProvider * > providers;
43 providers << new QgsVectorTileDataItemProvider;
44 return providers;
45}
46
47QMap<QString, QgsAbstractProviderConnection *> QgsVectorTileProviderMetadata::connections( bool cached )
48{
49 return connectionsProtected<QgsVectorTileProviderConnection, QgsVectorTileProviderConnection>( cached );
50}
51
52QgsAbstractProviderConnection *QgsVectorTileProviderMetadata::createConnection( const QString &name )
53{
54 return new QgsVectorTileProviderConnection( name );
55}
56
57void QgsVectorTileProviderMetadata::deleteConnection( const QString &name )
58{
59 deleteConnectionProtected<QgsVectorTileProviderConnection>( name );
60}
61
62void QgsVectorTileProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
63{
64 saveConnectionProtected( connection, name );
65}
66
67QgsProviderMetadata::ProviderCapabilities QgsVectorTileProviderMetadata::providerCapabilities() const
68{
69 return FileBasedUris;
70}
71
72QVariantMap QgsVectorTileProviderMetadata::decodeUri( const QString &uri ) const
73{
74 QgsDataSourceUri dsUri;
75 dsUri.setEncodedUri( uri );
76
77 QVariantMap uriComponents;
78 uriComponents.insert( QStringLiteral( "type" ), dsUri.param( QStringLiteral( "type" ) ) );
79 if ( dsUri.hasParam( QStringLiteral( "serviceType" ) ) )
80 uriComponents.insert( QStringLiteral( "serviceType" ), dsUri.param( QStringLiteral( "serviceType" ) ) );
81
82 if ( uriComponents[ QStringLiteral( "type" ) ] == QLatin1String( "mbtiles" ) ||
83 ( uriComponents[ QStringLiteral( "type" ) ] == QLatin1String( "xyz" ) &&
84 !dsUri.param( QStringLiteral( "url" ) ).startsWith( QLatin1String( "http" ) ) ) )
85 {
86 uriComponents.insert( QStringLiteral( "path" ), dsUri.param( QStringLiteral( "url" ) ) );
87 }
88 else
89 {
90 uriComponents.insert( QStringLiteral( "url" ), dsUri.param( QStringLiteral( "url" ) ) );
91 }
92
93 if ( dsUri.hasParam( QStringLiteral( "zmin" ) ) )
94 uriComponents.insert( QStringLiteral( "zmin" ), dsUri.param( QStringLiteral( "zmin" ) ) );
95 if ( dsUri.hasParam( QStringLiteral( "zmax" ) ) )
96 uriComponents.insert( QStringLiteral( "zmax" ), dsUri.param( QStringLiteral( "zmax" ) ) );
97
98 dsUri.httpHeaders().updateMap( uriComponents );
99
100 if ( dsUri.hasParam( QStringLiteral( "styleUrl" ) ) )
101 uriComponents.insert( QStringLiteral( "styleUrl" ), dsUri.param( QStringLiteral( "styleUrl" ) ) );
102
103 const QString authcfg = dsUri.authConfigId();
104 if ( !authcfg.isEmpty() )
105 uriComponents.insert( QStringLiteral( "authcfg" ), authcfg );
106
107 return uriComponents;
108}
109
110QString QgsVectorTileProviderMetadata::encodeUri( const QVariantMap &parts ) const
111{
112 QgsDataSourceUri dsUri;
113 dsUri.setParam( QStringLiteral( "type" ), parts.value( QStringLiteral( "type" ) ).toString() );
114 if ( parts.contains( QStringLiteral( "serviceType" ) ) )
115 dsUri.setParam( QStringLiteral( "serviceType" ), parts[ QStringLiteral( "serviceType" ) ].toString() );
116 dsUri.setParam( QStringLiteral( "url" ), parts.value( parts.contains( QStringLiteral( "path" ) ) ? QStringLiteral( "path" ) : QStringLiteral( "url" ) ).toString() );
117
118 if ( parts.contains( QStringLiteral( "zmin" ) ) )
119 dsUri.setParam( QStringLiteral( "zmin" ), parts[ QStringLiteral( "zmin" ) ].toString() );
120 if ( parts.contains( QStringLiteral( "zmax" ) ) )
121 dsUri.setParam( QStringLiteral( "zmax" ), parts[ QStringLiteral( "zmax" ) ].toString() );
122
123 dsUri.httpHeaders().setFromMap( parts );
124
125 if ( parts.contains( QStringLiteral( "styleUrl" ) ) )
126 dsUri.setParam( QStringLiteral( "styleUrl" ), parts[ QStringLiteral( "styleUrl" ) ].toString() );
127
128 if ( parts.contains( QStringLiteral( "authcfg" ) ) )
129 dsUri.setAuthConfigId( parts[ QStringLiteral( "authcfg" ) ].toString() );
130
131 return dsUri.encodedUri();
132}
133
134QString QgsVectorTileProviderMetadata::absoluteToRelativeUri( const QString &uri, const QgsReadWriteContext &context ) const
135{
136 QgsDataSourceUri dsUri;
137 dsUri.setEncodedUri( uri );
138
139 const QString sourceType = dsUri.param( QStringLiteral( "type" ) );
140 QString sourcePath = dsUri.param( QStringLiteral( "url" ) );
141 if ( sourceType == QLatin1String( "xyz" ) )
142 {
143 const QUrl sourceUrl( sourcePath );
144 if ( sourceUrl.isLocalFile() )
145 {
146 // relative path will become "file:./x.txt"
147 const QString relSrcUrl = context.pathResolver().writePath( sourceUrl.toLocalFile() );
148 dsUri.removeParam( QStringLiteral( "url" ) ); // needed because setParam() would insert second "url" key
149 dsUri.setParam( QStringLiteral( "url" ), QUrl::fromLocalFile( relSrcUrl ).toString() );
150 return dsUri.encodedUri();
151 }
152 }
153 else if ( sourceType == QLatin1String( "mbtiles" ) )
154 {
155 sourcePath = context.pathResolver().writePath( sourcePath );
156 dsUri.removeParam( QStringLiteral( "url" ) ); // needed because setParam() would insert second "url" key
157 dsUri.setParam( QStringLiteral( "url" ), sourcePath );
158 return dsUri.encodedUri();
159 }
160
161 return uri;
162}
163
164QString QgsVectorTileProviderMetadata::relativeToAbsoluteUri( const QString &uri, const QgsReadWriteContext &context ) const
165{
166 QgsDataSourceUri dsUri;
167 dsUri.setEncodedUri( uri );
168
169 const QString sourceType = dsUri.param( QStringLiteral( "type" ) );
170 QString sourcePath = dsUri.param( QStringLiteral( "url" ) );
171 if ( sourceType == QLatin1String( "xyz" ) )
172 {
173 const QUrl sourceUrl( sourcePath );
174 if ( sourceUrl.isLocalFile() ) // file-based URL? convert to relative path
175 {
176 const QString absSrcUrl = context.pathResolver().readPath( sourceUrl.toLocalFile() );
177 dsUri.removeParam( QStringLiteral( "url" ) ); // needed because setParam() would insert second "url" key
178 dsUri.setParam( QStringLiteral( "url" ), QUrl::fromLocalFile( absSrcUrl ).toString() );
179 return dsUri.encodedUri();
180 }
181 }
182 else if ( sourceType == QLatin1String( "mbtiles" ) )
183 {
184 sourcePath = context.pathResolver().readPath( sourcePath );
185 dsUri.removeParam( QStringLiteral( "url" ) ); // needed because setParam() would insert second "url" key
186 dsUri.setParam( QStringLiteral( "url" ), sourcePath );
187 return dsUri.encodedUri();
188 }
189
190 return uri;
191}
192
193QList<Qgis::LayerType> QgsVectorTileProviderMetadata::supportedLayerTypes() const
194{
195 return { Qgis::LayerType::VectorTile };
196}
197
The QgsAbstractProviderConnection provides an interface for data provider connections.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
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.
int removeParam(const QString &key)
Removes a generic parameter by key.
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.
QString param(const QString &key) const
Returns a generic parameter value corresponding to the specified key.
void setParam(const QString &key, const QString &value)
Sets a generic parameter value on the URI.
QString authConfigId() const
Returns any associated authentication configuration ID stored in the URI.
bool updateMap(QVariantMap &map) const
Updates a map by adding all the HTTP headers.
void setFromMap(const QVariantMap &map)
Loads headers from the map.
QString writePath(const QString &filename) const
Prepare a filename to save it to the project file.
QString readPath(const QString &filename) const
Turn filename read from the project file to an absolute path.
Holds data provider key, description, and associated shared library file or function pointer informat...
The class is used as a container of context for various read/write operations on other objects.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.