QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
39QIcon QgsVectorTileProviderMetadata::icon() const
40{
41 return QgsApplication::getThemeIcon( u"mIconVectorTileLayer.svg"_s );
42}
43
44QList<QgsDataItemProvider *> QgsVectorTileProviderMetadata::dataItemProviders() const
45{
46 QList< QgsDataItemProvider * > providers;
47 providers << new QgsVectorTileDataItemProvider;
48 return providers;
49}
50
51QMap<QString, QgsAbstractProviderConnection *> QgsVectorTileProviderMetadata::connections( bool cached )
52{
53 return connectionsProtected<QgsVectorTileProviderConnection, QgsVectorTileProviderConnection>( cached );
54}
55
56QgsAbstractProviderConnection *QgsVectorTileProviderMetadata::createConnection( const QString &name )
57{
58 return new QgsVectorTileProviderConnection( name );
59}
60
61void QgsVectorTileProviderMetadata::deleteConnection( const QString &name )
62{
63 deleteConnectionProtected<QgsVectorTileProviderConnection>( name );
64}
65
66void QgsVectorTileProviderMetadata::saveConnection( const QgsAbstractProviderConnection *connection, const QString &name )
67{
68 saveConnectionProtected( connection, name );
69}
70
71QgsProviderMetadata::ProviderCapabilities QgsVectorTileProviderMetadata::providerCapabilities() const
72{
73 return FileBasedUris;
74}
75
76QVariantMap QgsVectorTileProviderMetadata::decodeUri( const QString &uri ) const
77{
78 QgsDataSourceUri dsUri;
79 dsUri.setEncodedUri( uri );
80
81 QVariantMap uriComponents;
82 uriComponents.insert( u"type"_s, dsUri.param( u"type"_s ) );
83 if ( dsUri.hasParam( u"serviceType"_s ) )
84 uriComponents.insert( u"serviceType"_s, dsUri.param( u"serviceType"_s ) );
85
86 if ( uriComponents[u"type"_s] == "mbtiles"_L1 || ( uriComponents[u"type"_s] == "xyz"_L1 && !dsUri.param( u"url"_s ).startsWith( "http"_L1 ) ) )
87 {
88 uriComponents.insert( u"path"_s, dsUri.param( u"url"_s ) );
89 }
90 else
91 {
92 uriComponents.insert( u"url"_s, dsUri.param( u"url"_s ) );
93 }
94
95 if ( dsUri.hasParam( u"zmin"_s ) )
96 uriComponents.insert( u"zmin"_s, dsUri.param( u"zmin"_s ) );
97 if ( dsUri.hasParam( u"zmax"_s ) )
98 uriComponents.insert( u"zmax"_s, dsUri.param( u"zmax"_s ) );
99
100 dsUri.httpHeaders().updateMap( uriComponents );
101
102 if ( dsUri.hasParam( u"styleUrl"_s ) )
103 uriComponents.insert( u"styleUrl"_s, dsUri.param( u"styleUrl"_s ) );
104
105 const QString authcfg = dsUri.authConfigId();
106 if ( !authcfg.isEmpty() )
107 uriComponents.insert( u"authcfg"_s, authcfg );
108
109 return uriComponents;
110}
111
112QString QgsVectorTileProviderMetadata::encodeUri( const QVariantMap &parts ) const
113{
114 QgsDataSourceUri dsUri;
115 dsUri.setParam( u"type"_s, parts.value( u"type"_s ).toString() );
116 if ( parts.contains( u"serviceType"_s ) )
117 dsUri.setParam( u"serviceType"_s, parts[u"serviceType"_s].toString() );
118 dsUri.setParam( u"url"_s, parts.value( parts.contains( u"path"_s ) ? u"path"_s : u"url"_s ).toString() );
119
120 if ( parts.contains( u"zmin"_s ) )
121 dsUri.setParam( u"zmin"_s, parts[u"zmin"_s].toString() );
122 if ( parts.contains( u"zmax"_s ) )
123 dsUri.setParam( u"zmax"_s, parts[u"zmax"_s].toString() );
124
125 dsUri.httpHeaders().setFromMap( parts );
126
127 if ( parts.contains( u"styleUrl"_s ) )
128 dsUri.setParam( u"styleUrl"_s, parts[u"styleUrl"_s].toString() );
129
130 if ( parts.contains( u"authcfg"_s ) )
131 dsUri.setAuthConfigId( parts[u"authcfg"_s].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( u"type"_s );
142 QString sourcePath = dsUri.param( u"url"_s );
143 if ( sourceType == "xyz"_L1 )
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( u"url"_s ); // needed because setParam() would insert second "url" key
151 dsUri.setParam( u"url"_s, QUrl::fromLocalFile( relSrcUrl ).toString( QUrl::DecodeReserved ) );
152 return dsUri.encodedUri();
153 }
154 }
155 else if ( sourceType == "mbtiles"_L1 )
156 {
157 sourcePath = context.pathResolver().writePath( sourcePath );
158 dsUri.removeParam( u"url"_s ); // needed because setParam() would insert second "url" key
159 dsUri.setParam( u"url"_s, 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( u"type"_s );
172 QString sourcePath = dsUri.param( u"url"_s );
173 if ( sourceType == "xyz"_L1 )
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( u"url"_s ); // needed because setParam() would insert second "url" key
180 dsUri.setParam( u"url"_s, QUrl::fromLocalFile( absSrcUrl ).toString( QUrl::DecodeReserved ) );
181 return dsUri.encodedUri();
182 }
183 }
184 else if ( sourceType == "mbtiles"_L1 )
185 {
186 sourcePath = context.pathResolver().readPath( sourcePath );
187 dsUri.removeParam( u"url"_s ); // needed because setParam() would insert second "url" key
188 dsUri.setParam( u"url"_s, 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:211
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.