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