QGIS API Documentation 3.41.0-Master (45a0abf3bec)
Loading...
Searching...
No Matches
qgsmbtilesvectortiledataprovider.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsmbtilesvectortiledataprovider.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_qgsmbtilesvectortiledataprovider.cpp"
18#include "qgsthreadingutils.h"
19#include "qgsmbtiles.h"
20#include "qgstiles.h"
21#include "qgsvectortileloader.h"
22#include "qgsziputils.h"
23#include "qgslogger.h"
24#include "qgsapplication.h"
26#include "qgsproviderutils.h"
28
29#include <QIcon>
30#include <QFileInfo>
31
33
34QString QgsMbTilesVectorTileDataProvider::MB_TILES_VECTOR_TILE_DATA_PROVIDER_KEY = QStringLiteral( "mbtilesvectortiles" );
35QString QgsMbTilesVectorTileDataProvider::MB_TILES_VECTOR_TILE_DATA_PROVIDER_DESCRIPTION = QObject::tr( "MBTile Vector Tiles data provider" );
36
37QgsMbTilesVectorTileDataProvider::QgsMbTilesVectorTileDataProvider( const QString &uri, const ProviderOptions &providerOptions, Qgis::DataProviderReadFlags flags )
38 : QgsVectorTileDataProvider( uri, providerOptions, flags )
39{
40 QgsDataSourceUri dsUri;
41 dsUri.setEncodedUri( uri );
42 const QString sourcePath = dsUri.param( QStringLiteral( "url" ) );
43
44 QgsMbTiles reader( sourcePath );
45 if ( !reader.open() )
46 {
47 QgsDebugError( QStringLiteral( "failed to open MBTiles file: " ) + sourcePath );
48 mIsValid = false;
49 return;
50 }
51
52 const QString format = reader.metadataValue( QStringLiteral( "format" ) );
53 if ( format != QLatin1String( "pbf" ) )
54 {
55 QgsDebugError( QStringLiteral( "Cannot open MBTiles for vector tiles. Format = " ) + format );
56 mIsValid = false;
57 return;
58 }
59
60 QgsDebugMsgLevel( QStringLiteral( "name: " ) + reader.metadataValue( QStringLiteral( "name" ) ), 2 );
61
63
64 bool minZoomOk, maxZoomOk;
65 const int minZoom = reader.metadataValue( QStringLiteral( "minzoom" ) ).toInt( &minZoomOk );
66 const int maxZoom = reader.metadataValue( QStringLiteral( "maxzoom" ) ).toInt( &maxZoomOk );
67 if ( minZoomOk )
68 mMatrixSet.dropMatricesOutsideZoomRange( minZoom, 99 );
69 if ( maxZoomOk )
70 mMatrixSet.dropMatricesOutsideZoomRange( 0, maxZoom );
71 QgsDebugMsgLevel( QStringLiteral( "zoom range: %1 - %2" ).arg( mMatrixSet.minimumZoom() ).arg( mMatrixSet.maximumZoom() ), 2 );
72
73 QgsRectangle r = reader.extent();
74 QgsCoordinateTransform ct( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ),
75 QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) ), transformContext() );
76 ct.setBallparkTransformsAreAppropriate( true );
77 try
78 {
79 mExtent = ct.transformBoundingBox( r );
80 }
81 catch ( QgsCsException & )
82 {
83 QgsDebugError( QStringLiteral( "Could not transform layer extent to layer CRS" ) );
84 }
85
86 mIsValid = true;
87}
88
89QgsMbTilesVectorTileDataProvider::QgsMbTilesVectorTileDataProvider( const QgsMbTilesVectorTileDataProvider &other )
91{
92 mIsValid = other.mIsValid;
93 mExtent = other.mExtent;
94 mMatrixSet = other.mMatrixSet;
95}
96
97Qgis::DataProviderFlags QgsMbTilesVectorTileDataProvider::flags() const
98{
100}
101
102QString QgsMbTilesVectorTileDataProvider::name() const
103{
105
106 return MB_TILES_VECTOR_TILE_DATA_PROVIDER_KEY;
107}
108
109QString QgsMbTilesVectorTileDataProvider::description() const
110{
112
113 return MB_TILES_VECTOR_TILE_DATA_PROVIDER_DESCRIPTION;
114}
115
116QgsVectorTileDataProvider *QgsMbTilesVectorTileDataProvider::clone() const
117{
119 return new QgsMbTilesVectorTileDataProvider( *this );
120}
121
122QString QgsMbTilesVectorTileDataProvider::sourcePath() const
123{
125
126 QgsDataSourceUri dsUri;
127 dsUri.setEncodedUri( dataSourceUri() );
128 return dsUri.param( QStringLiteral( "url" ) );
129}
130
131bool QgsMbTilesVectorTileDataProvider::isValid() const
132{
134
135 return mIsValid;
136}
137
138QgsRectangle QgsMbTilesVectorTileDataProvider::extent() const
139{
141
142 return mExtent;
143}
144
145QgsCoordinateReferenceSystem QgsMbTilesVectorTileDataProvider::crs() const
146{
148
149 return QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:3857" ) );
150}
151
152const QgsVectorTileMatrixSet &QgsMbTilesVectorTileDataProvider::tileMatrixSet() const
153{
155
156 return mMatrixSet;
157}
158
159QgsVectorTileRawData QgsMbTilesVectorTileDataProvider::readTile( const QgsTileMatrixSet &, const QgsTileXYZ &id, QgsFeedback *feedback ) const
160{
162
163 QgsDataSourceUri dsUri;
164 dsUri.setEncodedUri( dataSourceUri() );
165
166 QgsMbTiles mbReader( dsUri.param( QStringLiteral( "url" ) ) );
167 mbReader.open();
168 return QgsVectorTileRawData( id, loadFromMBTiles( mbReader, id, feedback ) );
169}
170
171QList<QgsVectorTileRawData> QgsMbTilesVectorTileDataProvider::readTiles( const QgsTileMatrixSet &, const QVector<QgsTileXYZ> &tiles, QgsFeedback *feedback, Qgis::RendererUsage ) const
172{
174
175 QgsDataSourceUri dsUri;
176 dsUri.setEncodedUri( dataSourceUri() );
177
178 QgsMbTiles mbReader( dsUri.param( QStringLiteral( "url" ) ) );
179 mbReader.open();
180
181 QList<QgsVectorTileRawData> rawTiles;
182 rawTiles.reserve( tiles.size() );
183 for ( QgsTileXYZ id : std::as_const( tiles ) )
184 {
185 if ( feedback && feedback->isCanceled() )
186 break;
187
188 const QByteArray rawData = loadFromMBTiles( mbReader, id, feedback );
189 if ( !rawData.isEmpty() )
190 {
191 rawTiles.append( QgsVectorTileRawData( id, rawData ) );
192 }
193 }
194 return rawTiles;
195}
196
197QByteArray QgsMbTilesVectorTileDataProvider::loadFromMBTiles( QgsMbTiles &mbTileReader, const QgsTileXYZ &id, QgsFeedback *feedback )
198{
199 // MBTiles uses TMS specs with Y starting at the bottom while XYZ uses Y starting at the top
200 const int rowTMS = static_cast<int>( pow( 2, id.zoomLevel() ) - id.row() - 1 );
201 QByteArray gzippedTileData = mbTileReader.tileData( id.zoomLevel(), id.column(), rowTMS );
202 if ( gzippedTileData.isEmpty() )
203 {
204 return QByteArray();
205 }
206
207 if ( feedback && feedback->isCanceled() )
208 return QByteArray();
209
210 QByteArray data;
211 if ( !QgsZipUtils::decodeGzip( gzippedTileData, data ) )
212 {
213 QgsDebugError( QStringLiteral( "Failed to decompress tile " ) + id.toString() );
214 return QByteArray();
215 }
216
217 QgsDebugMsgLevel( QStringLiteral( "Tile blob size %1 -> uncompressed size %2" ).arg( gzippedTileData.size() ).arg( data.size() ), 2 );
218 return data;
219}
220
221
222//
223// QgsMbTilesVectorTileDataProviderMetadata
224//
225
226QgsMbTilesVectorTileDataProviderMetadata::QgsMbTilesVectorTileDataProviderMetadata()
227 : QgsProviderMetadata( QgsMbTilesVectorTileDataProvider::MB_TILES_VECTOR_TILE_DATA_PROVIDER_KEY,
228 QgsMbTilesVectorTileDataProvider::MB_TILES_VECTOR_TILE_DATA_PROVIDER_DESCRIPTION )
229{
230}
231
232QgsProviderMetadata::ProviderMetadataCapabilities QgsMbTilesVectorTileDataProviderMetadata::capabilities() const
233{
234 return ProviderMetadataCapability::LayerTypesForUri
235 | ProviderMetadataCapability::PriorityForUri
236 | ProviderMetadataCapability::QuerySublayers;
237}
238
239QgsMbTilesVectorTileDataProvider *QgsMbTilesVectorTileDataProviderMetadata::createProvider( const QString &uri, const QgsDataProvider::ProviderOptions &options, Qgis::DataProviderReadFlags flags )
240{
241 return new QgsMbTilesVectorTileDataProvider( uri, options, flags );
242}
243
244QIcon QgsMbTilesVectorTileDataProviderMetadata::icon() const
245{
246 return QgsApplication::getThemeIcon( QStringLiteral( "mIconVectorTileLayer.svg" ) );
247}
248
249QgsProviderMetadata::ProviderCapabilities QgsMbTilesVectorTileDataProviderMetadata::providerCapabilities() const
250{
251 return FileBasedUris;
252}
253
254QString QgsMbTilesVectorTileDataProviderMetadata::filters( Qgis::FileFilterType type )
255{
256 switch ( type )
257 {
264 return QString();
265
267 return QObject::tr( "Mbtiles Vector Tiles" ) + QStringLiteral( " (*.mbtiles *.MBTILES)" );
268 }
269 return QString();
270}
271
272QList<QgsProviderSublayerDetails> QgsMbTilesVectorTileDataProviderMetadata::querySublayers( const QString &uri, Qgis::SublayerQueryFlags flags, QgsFeedback * ) const
273{
274 QString fileName;
275 const QFileInfo fi( uri );
276 if ( fi.isFile() )
277 {
278 fileName = uri;
279 }
280 else
281 {
282 const QVariantMap parts = decodeUri( uri );
283 fileName = parts.value( QStringLiteral( "path" ) ).toString();
284 }
285
286 if ( fileName.isEmpty() )
287 return {};
288
289 if ( QFileInfo( fileName ).suffix().compare( QLatin1String( "mbtiles" ), Qt::CaseInsensitive ) == 0 )
290 {
291 QVariantMap parts;
292 parts.insert( QStringLiteral( "path" ), fileName );
293
295 {
296 // fast scan -- assume vector tile are available
298 details.setUri( encodeUri( parts ) );
299 details.setProviderKey( key() );
301 details.setSkippedContainerScan( true );
303 return {details};
304 }
305 else
306 {
307 // slower scan, check actual mbtiles format
308 QgsMbTiles reader( fileName );
309 if ( reader.open() )
310 {
311 if ( reader.metadataValue( "format" ) == QLatin1String( "pbf" ) )
312 {
314 details.setUri( encodeUri( parts ) );
315 details.setProviderKey( key() );
318 return {details};
319 }
320 }
321 }
322 }
323 return {};
324}
325
326int QgsMbTilesVectorTileDataProviderMetadata::priorityForUri( const QString &uri ) const
327{
328 if ( validLayerTypesForUri( uri ).contains( Qgis::LayerType::VectorTile ) )
329 return 100;
330
331 return 0;
332}
333
334QList<Qgis::LayerType> QgsMbTilesVectorTileDataProviderMetadata::validLayerTypesForUri( const QString &uri ) const
335{
336 const QFileInfo fi( uri );
337 if ( fi.isFile() && fi.suffix().compare( QLatin1String( "mbtiles" ), Qt::CaseInsensitive ) == 0 )
338 {
340 }
341
342 const QVariantMap parts = decodeUri( uri );
343 if ( parts.value( QStringLiteral( "path" ) ).toString().endsWith( ".mbtiles", Qt::CaseSensitivity::CaseInsensitive ) )
345
346 return {};
347}
348
349QVariantMap QgsMbTilesVectorTileDataProviderMetadata::decodeUri( const QString &uri ) const
350{
351 QgsDataSourceUri dsUri;
352 dsUri.setEncodedUri( uri );
353
354 QVariantMap uriComponents;
355 uriComponents.insert( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
356 uriComponents.insert( QStringLiteral( "path" ), dsUri.param( QStringLiteral( "url" ) ) );
357
358 return uriComponents;
359}
360
361QString QgsMbTilesVectorTileDataProviderMetadata::encodeUri( const QVariantMap &parts ) const
362{
363 QgsDataSourceUri dsUri;
364 dsUri.setParam( QStringLiteral( "type" ), QStringLiteral( "mbtiles" ) );
365 dsUri.setParam( QStringLiteral( "url" ), parts.value( parts.contains( QStringLiteral( "path" ) ) ? QStringLiteral( "path" ) : QStringLiteral( "url" ) ).toString() );
366 return dsUri.encodedUri();
367}
368
369QString QgsMbTilesVectorTileDataProviderMetadata::absoluteToRelativeUri( const QString &uri, const QgsReadWriteContext &context ) const
370{
371 QVariantMap parts = decodeUri( uri );
372
373 const QString originalPath = parts.value( QStringLiteral( "path" ) ).toString();
374 parts.insert( QStringLiteral( "path" ), context.pathResolver().writePath( originalPath ) );
375
376 return encodeUri( parts );
377}
378
379QString QgsMbTilesVectorTileDataProviderMetadata::relativeToAbsoluteUri( const QString &uri, const QgsReadWriteContext &context ) const
380{
381 QVariantMap parts = decodeUri( uri );
382
383 const QString originalPath = parts.value( QStringLiteral( "path" ) ).toString();
384 parts.insert( QStringLiteral( "path" ), context.pathResolver().readPath( originalPath ) );
385
386 return encodeUri( parts );
387}
388
389QList<Qgis::LayerType> QgsMbTilesVectorTileDataProviderMetadata::supportedLayerTypes() const
390{
392}
393
394
396
397
QFlags< DataProviderFlag > DataProviderFlags
Data provider flags.
Definition qgis.h:2147
FileFilterType
Type of file filters.
Definition qgis.h:1285
@ TiledScene
Tiled scene layers.
@ Vector
Vector layers.
@ VectorTile
Vector tile layers.
@ Mesh
Mesh layers.
@ Raster
Raster layers.
@ MeshDataset
Mesh datasets.
@ PointCloud
Point clouds.
@ FastExtent2D
Provider's 2D extent retrieval via QgsDataProvider::extent() is always guaranteed to be trivial/fast ...
@ FastScan
Indicates that the provider must scan for sublayers using the fastest possible approach – e....
QFlags< DataProviderReadFlag > DataProviderReadFlags
Flags which control data provider construction.
Definition qgis.h:450
QFlags< SublayerQueryFlag > SublayerQueryFlags
Sublayer query flags.
Definition qgis.h:1309
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
RendererUsage
Usage of the renderer.
Definition qgis.h:3196
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
This class represents a coordinate reference system (CRS).
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
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.
void setEncodedUri(const QByteArray &uri)
Sets the complete encoded uri.
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.
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
Utility class for reading and writing MBTiles files (which are SQLite3 databases).
Definition qgsmbtiles.h:39
QByteArray tileData(int z, int x, int y) const
Returns raw tile data for given tile.
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< ProviderMetadataCapability > ProviderMetadataCapabilities
QFlags< ProviderCapability > ProviderCapabilities
Contains details about a sub layer available from a dataset.
void setUri(const QString &uri)
Sets the layer's uri.
void setType(Qgis::LayerType type)
Sets the layer type.
void setName(const QString &name)
Sets the layer's name.
void setProviderKey(const QString &key)
Sets the associated data provider key.
void setSkippedContainerScan(bool skipped)
Set to true if the layer is a potential dataset container and an in-depth scan of its contents was sk...
static QString suggestLayerNameFromFilePath(const QString &path)
Suggests a suitable layer name given only a file path.
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.
A rectangle specified with double values.
Defines a set of tile matrices for multiple zoom levels.
Definition qgstiles.h:252
Stores coordinates of a tile in a tile matrix set.
Definition qgstiles.h:40
Base class for vector tile layer data providers.
Encapsulates properties of a vector tile matrix set, including tile origins and scaling information.
static QgsVectorTileMatrixSet fromWebMercator(int minimumZoom=0, int maximumZoom=14)
Returns a vector tile structure corresponding to the standard web mercator/GoogleCRS84Quad setup.
Keeps track of raw tile data from one or more sources that need to be decoded.
CORE_EXPORT bool decodeGzip(const QByteArray &bytesIn, QByteArray &bytesOut)
Decodes gzip byte stream, returns true on success.
#define QgsDebugMsgLevel(str, level)
Definition qgslogger.h:39
#define QgsDebugError(str)
Definition qgslogger.h:38
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
Setting options for creating vector data providers.