QGIS API Documentation 3.99.0-Master (09f76ad7019)
Loading...
Searching...
No Matches
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
18#include "qgsapplication.h"
19#include "qgsreadwritecontext.h"
22
23#include <QString>
24#include <QUrl>
25
26#include "moc_qgsvectortileprovidermetadata.cpp"
27
28using namespace Qt::StringLiterals;
29
31
32#define PROVIDER_KEY u"vectortile"_s
33#define PROVIDER_DESCRIPTION u"Vector tile provider"_s
34
35QgsVectorTileProviderMetadata::QgsVectorTileProviderMetadata()
36 : QgsProviderMetadata( PROVIDER_KEY, PROVIDER_DESCRIPTION )
37{
38}
39
40QIcon QgsVectorTileProviderMetadata::icon() const
41{
42 return QgsApplication::getThemeIcon( u"mIconVectorTileLayer.svg"_s );
43}
44
45QList<QgsDataItemProvider *> QgsVectorTileProviderMetadata::dataItemProviders() const
46{
47 QList< QgsDataItemProvider * > providers;
48 providers << new QgsVectorTileDataItemProvider;
49 return providers;
50}
51
52QMap<QString, QgsAbstractProviderConnection *> QgsVectorTileProviderMetadata::connections( bool cached )
53{
54 return connectionsProtected<QgsVectorTileProviderConnection, QgsVectorTileProviderConnection>( cached );
55}
56
57QgsAbstractProviderConnection *QgsVectorTileProviderMetadata::createConnection( const QString &name )
58{
59 return new QgsVectorTileProviderConnection( name );
60}
61
62void QgsVectorTileProviderMetadata::deleteConnection( const QString &name )
63{
64 deleteConnectionProtected<QgsVectorTileProviderConnection>( name );
65}
66
67void QgsVectorTileProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
68{
69 saveConnectionProtected( connection, name );
70}
71
72QgsProviderMetadata::ProviderCapabilities QgsVectorTileProviderMetadata::providerCapabilities() const
73{
74 return FileBasedUris;
75}
76
77QVariantMap QgsVectorTileProviderMetadata::decodeUri( const QString &uri ) const
78{
79 QgsDataSourceUri dsUri;
80 dsUri.setEncodedUri( uri );
81
82 QVariantMap uriComponents;
83 uriComponents.insert( u"type"_s, dsUri.param( u"type"_s ) );
84 if ( dsUri.hasParam( u"serviceType"_s ) )
85 uriComponents.insert( u"serviceType"_s, dsUri.param( u"serviceType"_s ) );
86
87 if ( uriComponents[ u"type"_s ] == "mbtiles"_L1 ||
88 ( uriComponents[ u"type"_s ] == "xyz"_L1 &&
89 !dsUri.param( u"url"_s ).startsWith( "http"_L1 ) ) )
90 {
91 uriComponents.insert( u"path"_s, dsUri.param( u"url"_s ) );
92 }
93 else
94 {
95 uriComponents.insert( u"url"_s, dsUri.param( u"url"_s ) );
96 }
97
98 if ( dsUri.hasParam( u"zmin"_s ) )
99 uriComponents.insert( u"zmin"_s, dsUri.param( u"zmin"_s ) );
100 if ( dsUri.hasParam( u"zmax"_s ) )
101 uriComponents.insert( u"zmax"_s, dsUri.param( u"zmax"_s ) );
102
103 dsUri.httpHeaders().updateMap( uriComponents );
104
105 if ( dsUri.hasParam( u"styleUrl"_s ) )
106 uriComponents.insert( u"styleUrl"_s, dsUri.param( u"styleUrl"_s ) );
107
108 const QString authcfg = dsUri.authConfigId();
109 if ( !authcfg.isEmpty() )
110 uriComponents.insert( u"authcfg"_s, authcfg );
111
112 return uriComponents;
113}
114
115QString QgsVectorTileProviderMetadata::encodeUri( const QVariantMap &parts ) const
116{
117 QgsDataSourceUri dsUri;
118 dsUri.setParam( u"type"_s, parts.value( u"type"_s ).toString() );
119 if ( parts.contains( u"serviceType"_s ) )
120 dsUri.setParam( u"serviceType"_s, parts[ u"serviceType"_s ].toString() );
121 dsUri.setParam( u"url"_s, parts.value( parts.contains( u"path"_s ) ? u"path"_s : u"url"_s ).toString() );
122
123 if ( parts.contains( u"zmin"_s ) )
124 dsUri.setParam( u"zmin"_s, parts[ u"zmin"_s ].toString() );
125 if ( parts.contains( u"zmax"_s ) )
126 dsUri.setParam( u"zmax"_s, parts[ u"zmax"_s ].toString() );
127
128 dsUri.httpHeaders().setFromMap( parts );
129
130 if ( parts.contains( u"styleUrl"_s ) )
131 dsUri.setParam( u"styleUrl"_s, parts[ u"styleUrl"_s ].toString() );
132
133 if ( parts.contains( u"authcfg"_s ) )
134 dsUri.setAuthConfigId( parts[ u"authcfg"_s ].toString() );
135
136 return dsUri.encodedUri();
137}
138
139QString QgsVectorTileProviderMetadata::absoluteToRelativeUri( const QString &uri, const QgsReadWriteContext &context ) const
140{
141 QgsDataSourceUri dsUri;
142 dsUri.setEncodedUri( uri );
143
144 const QString sourceType = dsUri.param( u"type"_s );
145 QString sourcePath = dsUri.param( u"url"_s );
146 if ( sourceType == "xyz"_L1 )
147 {
148 const QUrl sourceUrl( sourcePath );
149 if ( sourceUrl.isLocalFile() )
150 {
151 // relative path will become "file:./x.txt"
152 const QString relSrcUrl = context.pathResolver().writePath( sourceUrl.toLocalFile() );
153 dsUri.removeParam( u"url"_s ); // needed because setParam() would insert second "url" key
154 dsUri.setParam( u"url"_s, QUrl::fromLocalFile( relSrcUrl ).toString( QUrl::DecodeReserved ) );
155 return dsUri.encodedUri();
156 }
157 }
158 else if ( sourceType == "mbtiles"_L1 )
159 {
160 sourcePath = context.pathResolver().writePath( sourcePath );
161 dsUri.removeParam( u"url"_s ); // needed because setParam() would insert second "url" key
162 dsUri.setParam( u"url"_s, sourcePath );
163 return dsUri.encodedUri();
164 }
165
166 return uri;
167}
168
169QString QgsVectorTileProviderMetadata::relativeToAbsoluteUri( const QString &uri, const QgsReadWriteContext &context ) const
170{
171 QgsDataSourceUri dsUri;
172 dsUri.setEncodedUri( uri );
173
174 const QString sourceType = dsUri.param( u"type"_s );
175 QString sourcePath = dsUri.param( u"url"_s );
176 if ( sourceType == "xyz"_L1 )
177 {
178 const QUrl sourceUrl( sourcePath );
179 if ( sourceUrl.isLocalFile() ) // file-based URL? convert to relative path
180 {
181 const QString absSrcUrl = context.pathResolver().readPath( sourceUrl.toLocalFile() );
182 dsUri.removeParam( u"url"_s ); // needed because setParam() would insert second "url" key
183 dsUri.setParam( u"url"_s, QUrl::fromLocalFile( absSrcUrl ).toString( QUrl::DecodeReserved ) );
184 return dsUri.encodedUri();
185 }
186 }
187 else if ( sourceType == "mbtiles"_L1 )
188 {
189 sourcePath = context.pathResolver().readPath( sourcePath );
190 dsUri.removeParam( u"url"_s ); // needed because setParam() would insert second "url" key
191 dsUri.setParam( u"url"_s, sourcePath );
192 return dsUri.encodedUri();
193 }
194
195 return uri;
196}
197
198QList<Qgis::LayerType> QgsVectorTileProviderMetadata::supportedLayerTypes() const
199{
201}
202
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
Definition qgis.h:198
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.
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.
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...
QFlags< ProviderCapability > ProviderCapabilities
A container for the context for various read/write operations on objects.
const QgsPathResolver & pathResolver() const
Returns path resolver for conversion between relative and absolute paths.