27using namespace Qt::StringLiterals;
34 SetStylePostProcessor( QDomDocument &doc )
38 void postProcessLayer( QgsMapLayer *layer, QgsProcessingContext &, QgsProcessingFeedback * )
override
40 if ( QgsVectorTileLayer *tileLayer = qobject_cast<QgsVectorTileLayer *>( layer ) )
43 tileLayer->importNamedStyle( mDocument, errorMsg );
44 tileLayer->triggerRepaint();
49 QDomDocument mDocument;
52QString QgsDownloadVectorTilesAlgorithm::name()
const
54 return u
"downloadvectortiles"_s;
57QString QgsDownloadVectorTilesAlgorithm::displayName()
const
59 return QObject::tr(
"Download vector tiles" );
62QStringList QgsDownloadVectorTilesAlgorithm::tags()
const
64 return QObject::tr(
"vector,split,field,unique" ).split(
',' );
67QString QgsDownloadVectorTilesAlgorithm::group()
const
69 return QObject::tr(
"Vector tiles" );
72QString QgsDownloadVectorTilesAlgorithm::groupId()
const
74 return u
"vectortiles"_s;
77QString QgsDownloadVectorTilesAlgorithm::shortHelpString()
const
79 return QObject::tr(
"This algorithm downloads vector tiles of the input vector tile layer and saves them in the local vector tile file." );
82QString QgsDownloadVectorTilesAlgorithm::shortDescription()
const
84 return QObject::tr(
"Downloads vector tiles of the input vector tile layer and saves them in the local vector tile file." );
87QgsDownloadVectorTilesAlgorithm *QgsDownloadVectorTilesAlgorithm::createInstance()
const
89 return new QgsDownloadVectorTilesAlgorithm();
92void QgsDownloadVectorTilesAlgorithm::initAlgorithm(
const QVariantMap & )
103 QgsMapLayer *layer = parameterAsLayer( parameters, u
"INPUT"_s, context );
108 mProvider.reset( qgis::down_cast<const QgsVectorTileDataProvider *>( vtLayer->
dataProvider() )->clone() );
111 mLayerName = vtLayer->
name();
113 mExtent = parameterAsExtent( parameters, u
"EXTENT"_s, context, layer->
crs() );
115 mMaxZoom = parameterAsInt( parameters, u
"MAX_ZOOM"_s, context );
118 throw QgsProcessingException( QObject::tr(
"Requested maximum zoom level is bigger than available zoom level in the source layer. Please, select zoom level lower or equal to %1." ).arg( vtLayer->
sourceMaxZoom() ) );
121 mTileLimit =
static_cast<long long>( parameterAsInt( parameters, u
"TILE_LIMIT"_s, context ) );
123 mStyleDocument = QDomDocument( u
"qgis"_s );
126 if ( !errorMsg.isEmpty() )
128 feedback->
pushWarning( QObject::tr(
"Failed to get layer style: %1" ).arg( errorMsg ) );
136 const QString outputFile = parameterAsOutputLayer( parameters, u
"OUTPUT"_s, context );
139 long long tileCount = 0;
140 QMap<int, QgsTileRange> tileRanges;
141 for (
int i = 0; i <= mMaxZoom; i++ )
145 tileRanges.insert( i, tileRange );
148 if ( tileCount > mTileLimit )
150 throw QgsProcessingException( QObject::tr(
"Requested number of tiles %1 exceeds limit of %2 tiles. Please, select a smaller extent, reduce maximum zoom level or increase tile limit." ).arg( tileCount ).arg( mTileLimit ) );
153 auto writer = std::make_unique<QgsMbTiles>( outputFile );
154 if ( !writer->create() )
158 writer->setMetadataValue(
"format",
"pbf" );
159 writer->setMetadataValue(
"name", mLayerName );
160 writer->setMetadataValue(
"minzoom", QString::number( mSourceMinZoom ) );
161 writer->setMetadataValue(
"maxzoom", QString::number( mMaxZoom ) );
162 writer->setMetadataValue(
"crs", mTileMatrixSet.rootMatrix().crs().authid() );
166 ct.setBallparkTransformsAreAppropriate(
true );
167 QgsRectangle wgsExtent = ct.transformBoundingBox( mExtent );
168 QString boundsStr = QString(
"%1,%2,%3,%4" )
173 writer->setMetadataValue(
"bounds", boundsStr );
182 std::unique_ptr<QgsVectorTileLoader> loader;
183 QList<QgsVectorTileRawData> rawTiles;
185 QMap<int, QgsTileRange>::const_iterator it = tileRanges.constBegin();
186 while ( it != tileRanges.constEnd() )
191 multiStepFeedback.setCurrentStep( it.key() );
193 QgsTileMatrix tileMatrix = mTileMatrixSet.tileMatrix( it.key() );
194 tileCount =
static_cast<long long>( it.value().endColumn() - it.value().startColumn() + 1 ) * ( it.value().endRow() - it.value().startRow() + 1 );
198 long long tileNumber = 0;
207 const QByteArray data = rawTile.data.first();
209 if ( !data.isEmpty() )
211 QByteArray gzipTileData;
213 int rowTMS = pow( 2, rawTile.id.zoomLevel() ) - rawTile.id.row() - 1;
214 writer->setTileData( rawTile.id.zoomLevel(), rawTile.id.column(), rowTMS, gzipTileData );
217 multiStepFeedback.setProgress( 100.0 * ( tileNumber++ ) / tileCount );
229 results.insert( u
"OUTPUT"_s, outputFile );
@ VectorTile
Vector tile layers.
@ Export
Renderer used for printing or exporting to a file.
Represents a coordinate reference system (CRS).
Custom exception class for Coordinate Reference System related exceptions.
bool isCanceled() const
Tells whether the operation has been canceled already.
Base class for all map layer types.
virtual void exportNamedStyle(QDomDocument &doc, QString &errorMsg, const QgsReadWriteContext &context=QgsReadWriteContext(), QgsMapLayer::StyleCategories categories=QgsMapLayer::AllStyleCategories) const
Export the properties of this layer as named style in a QDomDocument.
QgsCoordinateReferenceSystem crs
void setPostProcessor(QgsProcessingLayerPostProcessorInterface *processor)
Sets the layer post-processor.
Contains information about the context in which a processing algorithm is executed.
QgsProcessingContext::LayerDetails & layerToLoadOnCompletionDetails(const QString &layer)
Returns a reference to the details for a given layer which is loaded on completion of the algorithm o...
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
bool willLoadLayerOnCompletion(const QString &layer) const
Returns true if the given layer (by ID or datasource) will be loaded into the current project upon co...
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
An interface for layer post-processing handlers for execution following a processing algorithm operat...
virtual void postProcessLayer(QgsMapLayer *layer, QgsProcessingContext &context, QgsProcessingFeedback *feedback)=0
Post-processes the specified layer, following successful execution of a processing algorithm.
Processing feedback object for multi-step operations.
A rectangular map extent parameter for processing algorithms.
A map layer parameter for processing algorithms.
A numeric parameter for processing algorithms.
A vector tile layer destination parameter, for specifying the destination path for a vector tile laye...
A rectangle specified with double values.
Defines a matrix of tiles for a single zoom level: it is defined by its size (width *.
QPointF mapToTileCoordinates(const QgsPointXY &mapPoint) const
Returns row/column coordinates (floating point number) from the given point in map coordinates.
QgsTileRange tileRangeFromExtent(const QgsRectangle &mExtent) const
Returns tile range that fully covers the given extent.
A range of tiles in a tile matrix.
int endColumn() const
Returns index of the last column in the range.
int endRow() const
Returns index of the last row in the range.
int startRow() const
Returns index of the first row in the range.
int startColumn() const
Returns index of the first column in the range.
Implements a map layer that is dedicated to rendering of vector tiles.
int sourceMinZoom() const
Returns minimum zoom level at which source has any valid tiles (negative = unconstrained).
int sourceMaxZoom() const
Returns maximum zoom level at which source has any valid tiles (negative = unconstrained).
QgsDataProvider * dataProvider() override
Returns the layer's data provider, it may be nullptr.
QgsVectorTileMatrixSet & tileMatrixSet()
Returns the vector tile matrix set.
static QList< QgsVectorTileRawData > blockingFetchTileRawData(const QgsVectorTileDataProvider *provider, const QgsTileMatrixSet &tileMatrixSet, const QPointF &viewCenter, const QgsTileRange &range, int zoomLevel, QgsFeedback *feedback=nullptr, Qgis::RendererUsage usage=Qgis::RendererUsage::Unknown)
Returns raw tile data for the specified range of tiles. Blocks the caller until all tiles are fetched...
Keeps track of raw tile data from one or more sources that need to be decoded.
static bool encodeGzip(const QByteArray &bytesIn, QByteArray &bytesOut)
Encodes gzip byte stream, returns true on success.