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(
"Downloads vector tiles of the input vector tile layer and saves them in the local vector tile file." );
78QgsDownloadVectorTilesAlgorithm *QgsDownloadVectorTilesAlgorithm::createInstance()
const
80 return new QgsDownloadVectorTilesAlgorithm();
83void QgsDownloadVectorTilesAlgorithm::initAlgorithm(
const QVariantMap & )
94 QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral(
"INPUT" ), context );
99 mProvider.reset( qgis::down_cast<const QgsVectorTileDataProvider *>( vtLayer->
dataProvider() )->clone() );
102 mLayerName = vtLayer->
name();
104 mExtent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, layer->
crs() );
106 mMaxZoom = parameterAsInt( parameters, QStringLiteral(
"MAX_ZOOM" ), context );
109 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() ) );
112 mTileLimit =
static_cast<long long>( parameterAsInt( parameters, QStringLiteral(
"TILE_LIMIT" ), context ) );
114 mStyleDocument = QDomDocument( QStringLiteral(
"qgis" ) );
117 if ( !errorMsg.isEmpty() )
119 feedback->
pushWarning( QObject::tr(
"Failed to get layer style: %1" ).arg( errorMsg ) );
127 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
130 long long tileCount = 0;
131 QMap<int, QgsTileRange> tileRanges;
132 for (
int i = 0; i <= mMaxZoom; i++ )
136 tileRanges.insert( i, tileRange );
139 if ( tileCount > mTileLimit )
141 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 ) );
144 std::unique_ptr<QgsMbTiles> writer = std::make_unique<QgsMbTiles>( outputFile );
145 if ( !writer->create() )
149 writer->setMetadataValue(
"format",
"pbf" );
150 writer->setMetadataValue(
"name", mLayerName );
151 writer->setMetadataValue(
"minzoom", QString::number( mSourceMinZoom ) );
152 writer->setMetadataValue(
"maxzoom", QString::number( mMaxZoom ) );
153 writer->setMetadataValue(
"crs", mTileMatrixSet.rootMatrix().crs().authid() );
157 ct.setBallparkTransformsAreAppropriate(
true );
158 QgsRectangle wgsExtent = ct.transformBoundingBox( mExtent );
159 QString boundsStr = QString(
"%1,%2,%3,%4" )
164 writer->setMetadataValue(
"bounds", boundsStr );
173 std::unique_ptr<QgsVectorTileLoader> loader;
174 QList<QgsVectorTileRawData> rawTiles;
176 QMap<int, QgsTileRange>::const_iterator it = tileRanges.constBegin();
177 while ( it != tileRanges.constEnd() )
182 multiStepFeedback.setCurrentStep( it.key() );
184 QgsTileMatrix tileMatrix = mTileMatrixSet.tileMatrix( it.key() );
185 tileCount =
static_cast<long long>( it.value().endColumn() - it.value().startColumn() + 1 ) * ( it.value().endRow() - it.value().startRow() + 1 );
189 long long tileNumber = 0;
198 const QByteArray data = rawTile.data.first();
200 if ( !data.isEmpty() )
202 QByteArray gzipTileData;
204 int rowTMS = pow( 2, rawTile.id.zoomLevel() ) - rawTile.id.row() - 1;
205 writer->setTileData( rawTile.id.zoomLevel(), rawTile.id.column(), rowTMS, gzipTileData );
208 multiStepFeedback.setProgress( 100.0 * ( tileNumber++ ) / tileCount );
220 results.insert( QStringLiteral(
"OUTPUT" ), outputFile );
@ VectorTile
Vector tile layers.
@ Export
Renderer used for printing or exporting to a file.
This class 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.
Range of tiles in a tile matrix to be rendered.
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.
CORE_EXPORT bool encodeGzip(const QByteArray &bytesIn, QByteArray &bytesOut)
Encodes gzip byte stream, returns true on success.