31 SetStylePostProcessor( QDomDocument &doc )
40 tileLayer->importNamedStyle( mDocument, errorMsg );
41 tileLayer->triggerRepaint();
47 QDomDocument mDocument;
50QString QgsDownloadVectorTilesAlgorithm::name()
const
52 return QStringLiteral(
"downloadvectortiles" );
55QString QgsDownloadVectorTilesAlgorithm::displayName()
const
57 return QObject::tr(
"Download vector tiles" );
60QStringList QgsDownloadVectorTilesAlgorithm::tags()
const
62 return QObject::tr(
"vector,split,field,unique" ).split(
',' );
65QString QgsDownloadVectorTilesAlgorithm::group()
const
67 return QObject::tr(
"Vector tiles" );
70QString QgsDownloadVectorTilesAlgorithm::groupId()
const
72 return QStringLiteral(
"vectortiles" );
75QString QgsDownloadVectorTilesAlgorithm::shortHelpString()
const
77 return QObject::tr(
"Downloads vector tiles of the input vector tile layer and saves them in the local vector tile file." );
80QgsDownloadVectorTilesAlgorithm *QgsDownloadVectorTilesAlgorithm::createInstance()
const
82 return new QgsDownloadVectorTilesAlgorithm();
85void QgsDownloadVectorTilesAlgorithm::initAlgorithm(
const QVariantMap & )
96 QgsMapLayer *layer = parameterAsLayer( parameters, QStringLiteral(
"INPUT" ), context );
101 mProvider.reset( qgis::down_cast< const QgsVectorTileDataProvider * >( vtLayer->
dataProvider() )->clone() );
104 mLayerName = vtLayer->
name();
106 mExtent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context, layer->
crs() );
108 mMaxZoom = parameterAsInt( parameters, QStringLiteral(
"MAX_ZOOM" ), context );
111 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() ) );
114 mTileLimit =
static_cast< long long >( parameterAsInt( parameters, QStringLiteral(
"TILE_LIMIT" ), context ) );
116 mStyleDocument = QDomDocument( QStringLiteral(
"qgis" ) );
119 if ( !errorMsg.isEmpty() )
121 feedback->
pushWarning( QObject::tr(
"Failed to get layer style: %1" ).arg( errorMsg ) );
129 const QString outputFile = parameterAsOutputLayer( parameters, QStringLiteral(
"OUTPUT" ), context );
132 long long tileCount = 0;
133 QMap<int, QgsTileRange> tileRanges;
134 for (
int i = 0; i <= mMaxZoom; i++ )
138 tileRanges.insert( i, tileRange );
141 if ( tileCount > mTileLimit )
143 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 ) );
146 std::unique_ptr<QgsMbTiles> writer = std::make_unique<QgsMbTiles>( outputFile );
147 if ( !writer->create() )
151 writer->setMetadataValue(
"format",
"pbf" );
152 writer->setMetadataValue(
"name", mLayerName );
153 writer->setMetadataValue(
"minzoom", QString::number( mSourceMinZoom ) );
154 writer->setMetadataValue(
"maxzoom", QString::number( mMaxZoom ) );
155 writer->setMetadataValue(
"crs", mTileMatrixSet.rootMatrix().crs().authid() );
159 ct.setBallparkTransformsAreAppropriate(
true );
160 QgsRectangle wgsExtent = ct.transformBoundingBox( mExtent );
161 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.
double xMinimum() const
Returns the x minimum value (left side of rectangle).
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
double xMaximum() const
Returns the x maximum value (right side of rectangle).
double yMaximum() const
Returns the y maximum value (top side of rectangle).
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.