30 SetStylePostProcessor( QDomDocument &doc )
39 tileLayer->importNamedStyle( mDocument, errorMsg );
40 tileLayer->triggerRepaint();
45 QDomDocument mDocument;
48QString QgsDownloadVectorTilesAlgorithm::name()
const
50 return QStringLiteral(
"downloadvectortiles" );
53QString QgsDownloadVectorTilesAlgorithm::displayName()
const
55 return QObject::tr(
"Download vector tiles" );
58QStringList QgsDownloadVectorTilesAlgorithm::tags()
const
60 return QObject::tr(
"vector,split,field,unique" ).split(
',' );
63QString QgsDownloadVectorTilesAlgorithm::group()
const
65 return QObject::tr(
"Vector tiles" );
68QString QgsDownloadVectorTilesAlgorithm::groupId()
const
70 return QStringLiteral(
"vectortiles" );
73QString QgsDownloadVectorTilesAlgorithm::shortHelpString()
const
75 return QObject::tr(
"This algorithm downloads vector tiles of the input vector tile layer and saves them in the local vector tile file." );
78QString QgsDownloadVectorTilesAlgorithm::shortDescription()
const
80 return QObject::tr(
"Downloads vector tiles of the input vector tile layer and saves them in the local vector tile file." );
83QgsDownloadVectorTilesAlgorithm *QgsDownloadVectorTilesAlgorithm::createInstance()
const
85 return new QgsDownloadVectorTilesAlgorithm();
88void QgsDownloadVectorTilesAlgorithm::initAlgorithm(
const QVariantMap & )
99 QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral(
"INPUT" ), context );
104 mProvider.reset( qgis::down_cast<const QgsVectorTileDataProvider *>( vtLayer->
dataProvider() )->clone() );
107 mLayerName = vtLayer->
name();
109 mExtent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, layer->
crs() );
111 mMaxZoom = parameterAsInt( parameters, QStringLiteral(
"MAX_ZOOM" ), context );
114 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() ) );
117 mTileLimit =
static_cast<long long>( parameterAsInt( parameters, QStringLiteral(
"TILE_LIMIT" ), context ) );
119 mStyleDocument = QDomDocument( QStringLiteral(
"qgis" ) );
122 if ( !errorMsg.isEmpty() )
124 feedback->
pushWarning( QObject::tr(
"Failed to get layer style: %1" ).arg( errorMsg ) );
132 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
135 long long tileCount = 0;
136 QMap<int, QgsTileRange> tileRanges;
137 for (
int i = 0; i <= mMaxZoom; i++ )
141 tileRanges.insert( i, tileRange );
144 if ( tileCount > mTileLimit )
146 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 ) );
149 auto writer = std::make_unique<QgsMbTiles>( outputFile );
150 if ( !writer->create() )
154 writer->setMetadataValue(
"format",
"pbf" );
155 writer->setMetadataValue(
"name", mLayerName );
156 writer->setMetadataValue(
"minzoom", QString::number( mSourceMinZoom ) );
157 writer->setMetadataValue(
"maxzoom", QString::number( mMaxZoom ) );
158 writer->setMetadataValue(
"crs", mTileMatrixSet.rootMatrix().crs().authid() );
162 ct.setBallparkTransformsAreAppropriate(
true );
163 QgsRectangle wgsExtent = ct.transformBoundingBox( mExtent );
164 QString boundsStr = QString(
"%1,%2,%3,%4" )
169 writer->setMetadataValue(
"bounds", boundsStr );
178 std::unique_ptr<QgsVectorTileLoader> loader;
179 QList<QgsVectorTileRawData> rawTiles;
181 QMap<int, QgsTileRange>::const_iterator it = tileRanges.constBegin();
182 while ( it != tileRanges.constEnd() )
187 multiStepFeedback.setCurrentStep( it.key() );
189 QgsTileMatrix tileMatrix = mTileMatrixSet.tileMatrix( it.key() );
190 tileCount =
static_cast<long long>( it.value().endColumn() - it.value().startColumn() + 1 ) * ( it.value().endRow() - it.value().startRow() + 1 );
194 long long tileNumber = 0;
203 const QByteArray data = rawTile.data.first();
205 if ( !data.isEmpty() )
207 QByteArray gzipTileData;
209 int rowTMS = pow( 2, rawTile.id.zoomLevel() ) - rawTile.id.row() - 1;
210 writer->setTileData( rawTile.id.zoomLevel(), rawTile.id.column(), rowTMS, gzipTileData );
213 multiStepFeedback.setProgress( 100.0 * ( tileNumber++ ) / tileCount );
225 results.insert( QStringLiteral(
"OUTPUT" ), 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.