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