17#include "moc_qgsmbtilesvectortiledataprovider.cpp"
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" );
37QgsMbTilesVectorTileDataProvider::QgsMbTilesVectorTileDataProvider(
const QString &uri,
const ProviderOptions &providerOptions,
Qgis::DataProviderReadFlags flags )
42 const QString sourcePath = dsUri.
param( QStringLiteral(
"url" ) );
47 QgsDebugError( QStringLiteral(
"failed to open MBTiles file: " ) + sourcePath );
52 const QString format = reader.metadataValue( QStringLiteral(
"format" ) );
53 if ( format != QLatin1String(
"pbf" ) )
55 QgsDebugError( QStringLiteral(
"Cannot open MBTiles for vector tiles. Format = " ) + format );
60 QgsDebugMsgLevel( QStringLiteral(
"name: " ) + reader.metadataValue( QStringLiteral(
"name" ) ), 2 );
64 bool minZoomOk, maxZoomOk;
65 const int minZoom = reader.metadataValue( QStringLiteral(
"minzoom" ) ).toInt( &minZoomOk );
66 const int maxZoom = reader.metadataValue( QStringLiteral(
"maxzoom" ) ).toInt( &maxZoomOk );
68 mMatrixSet.dropMatricesOutsideZoomRange( minZoom, 99 );
70 mMatrixSet.dropMatricesOutsideZoomRange( 0, maxZoom );
71 QgsDebugMsgLevel( QStringLiteral(
"zoom range: %1 - %2" ).arg( mMatrixSet.minimumZoom() ).arg( mMatrixSet.maximumZoom() ), 2 );
76 ct.setBallparkTransformsAreAppropriate(
true );
79 mExtent = ct.transformBoundingBox( r );
83 QgsDebugError( QStringLiteral(
"Could not transform layer extent to layer CRS" ) );
89QgsMbTilesVectorTileDataProvider::QgsMbTilesVectorTileDataProvider(
const QgsMbTilesVectorTileDataProvider &other )
92 mIsValid = other.mIsValid;
93 mExtent = other.mExtent;
94 mMatrixSet = other.mMatrixSet;
102QString QgsMbTilesVectorTileDataProvider::name()
const
106 return MB_TILES_VECTOR_TILE_DATA_PROVIDER_KEY;
109QString QgsMbTilesVectorTileDataProvider::description()
const
113 return MB_TILES_VECTOR_TILE_DATA_PROVIDER_DESCRIPTION;
119 return new QgsMbTilesVectorTileDataProvider( *
this );
122QString QgsMbTilesVectorTileDataProvider::sourcePath()
const
128 return dsUri.param( QStringLiteral(
"url" ) );
131bool QgsMbTilesVectorTileDataProvider::isValid()
const
138QgsRectangle QgsMbTilesVectorTileDataProvider::extent()
const
166 QgsMbTiles mbReader( dsUri.param( QStringLiteral(
"url" ) ) );
178 QgsMbTiles mbReader( dsUri.param( QStringLiteral(
"url" ) ) );
181 QList<QgsVectorTileRawData> rawTiles;
182 rawTiles.reserve( tiles.size() );
183 for (
QgsTileXYZ id : std::as_const( tiles ) )
188 const QByteArray rawData = loadFromMBTiles( mbReader,
id, feedback );
189 if ( !rawData.isEmpty() )
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() )
213 QgsDebugError( QStringLiteral(
"Failed to decompress tile " ) +
id.toString() );
217 QgsDebugMsgLevel( QStringLiteral(
"Tile blob size %1 -> uncompressed size %2" ).arg( gzippedTileData.size() ).arg( data.size() ), 2 );
226QgsMbTilesVectorTileDataProviderMetadata::QgsMbTilesVectorTileDataProviderMetadata()
227 :
QgsProviderMetadata( QgsMbTilesVectorTileDataProvider::MB_TILES_VECTOR_TILE_DATA_PROVIDER_KEY,
228 QgsMbTilesVectorTileDataProvider::MB_TILES_VECTOR_TILE_DATA_PROVIDER_DESCRIPTION )
234 return ProviderMetadataCapability::LayerTypesForUri
235 | ProviderMetadataCapability::PriorityForUri
236 | ProviderMetadataCapability::QuerySublayers;
241 return new QgsMbTilesVectorTileDataProvider( uri, options, flags );
244QIcon QgsMbTilesVectorTileDataProviderMetadata::icon()
const
251 return FileBasedUris;
267 return QObject::tr(
"Mbtiles Vector Tiles" ) + QStringLiteral(
" (*.mbtiles *.MBTILES)" );
275 const QFileInfo fi( uri );
282 const QVariantMap parts = decodeUri( uri );
283 fileName = parts.value( QStringLiteral(
"path" ) ).toString();
286 if ( fileName.isEmpty() )
289 if ( QFileInfo( fileName ).suffix().compare( QLatin1String(
"mbtiles" ), Qt::CaseInsensitive ) == 0 )
292 parts.insert( QStringLiteral(
"path" ), fileName );
298 details.
setUri( encodeUri( parts ) );
311 if ( reader.metadataValue(
"format" ) == QLatin1String(
"pbf" ) )
314 details.
setUri( encodeUri( parts ) );
326int QgsMbTilesVectorTileDataProviderMetadata::priorityForUri(
const QString &uri )
const
334QList<Qgis::LayerType> QgsMbTilesVectorTileDataProviderMetadata::validLayerTypesForUri(
const QString &uri )
const
336 const QFileInfo fi( uri );
337 if ( fi.isFile() && fi.suffix().compare( QLatin1String(
"mbtiles" ), Qt::CaseInsensitive ) == 0 )
342 const QVariantMap parts = decodeUri( uri );
343 if ( parts.value( QStringLiteral(
"path" ) ).toString().endsWith(
".mbtiles", Qt::CaseSensitivity::CaseInsensitive ) )
349QVariantMap QgsMbTilesVectorTileDataProviderMetadata::decodeUri(
const QString &uri )
const
354 QVariantMap uriComponents;
355 uriComponents.insert( QStringLiteral(
"type" ), QStringLiteral(
"mbtiles" ) );
356 uriComponents.insert( QStringLiteral(
"path" ), dsUri.
param( QStringLiteral(
"url" ) ) );
358 return uriComponents;
361QString QgsMbTilesVectorTileDataProviderMetadata::encodeUri(
const QVariantMap &parts )
const
364 dsUri.
setParam( QStringLiteral(
"type" ), QStringLiteral(
"mbtiles" ) );
365 dsUri.
setParam( QStringLiteral(
"url" ), parts.value( parts.contains( QStringLiteral(
"path" ) ) ? QStringLiteral(
"path" ) : QStringLiteral(
"url" ) ).toString() );
369QString QgsMbTilesVectorTileDataProviderMetadata::absoluteToRelativeUri(
const QString &uri,
const QgsReadWriteContext &context )
const
371 QVariantMap parts = decodeUri( uri );
373 const QString originalPath = parts.value( QStringLiteral(
"path" ) ).toString();
376 return encodeUri( parts );
379QString QgsMbTilesVectorTileDataProviderMetadata::relativeToAbsoluteUri(
const QString &uri,
const QgsReadWriteContext &context )
const
381 QVariantMap parts = decodeUri( uri );
383 const QString originalPath = parts.value( QStringLiteral(
"path" ) ).toString();
386 return encodeUri( parts );
389QList<Qgis::LayerType> QgsMbTilesVectorTileDataProviderMetadata::supportedLayerTypes()
const
QFlags< DataProviderFlag > DataProviderFlags
Data provider flags.
FileFilterType
Type of file filters.
@ TiledScene
Tiled scene layers.
@ VectorTile
Vector tile 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.
QFlags< SublayerQueryFlag > SublayerQueryFlags
Sublayer query flags.
@ VectorTile
Vector tile layer. Added in QGIS 3.14.
RendererUsage
Usage of the renderer.
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).
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.
bool isCanceled() const
Tells whether the operation has been canceled already.
Utility class for reading and writing MBTiles files (which are SQLite3 databases).
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.
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.
Stores coordinates of a tile in a tile matrix set.
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)
#define QgsDebugError(str)
#define QGIS_PROTECT_QOBJECT_THREAD_ACCESS
Setting options for creating vector data providers.