QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgsalgorithmrasterize.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsalgorithmrasterize.cpp - QgsRasterizeAlgorithm
3
4 ---------------------
5 Original implementation in Python:
6
7 begin : 2016-10-05
8 copyright : (C) 2016 by OPENGIS.ch
10
11 C++ port:
12
13 begin : 20.11.2019
14 copyright : (C) 2019 by Alessandro Pasotti
15 email : elpaso at itopen dot it
16 ***************************************************************************
17 * *
18 * This program is free software; you can redistribute it and/or modify *
19 * it under the terms of the GNU General Public License as published by *
20 * the Free Software Foundation; either version 2 of the License, or *
21 * (at your option) any later version. *
22 * *
23 ***************************************************************************/
24
27#include "qgsprovidermetadata.h"
28#include "qgsmaplayerutils.h"
30#include "qgsrasterfilewriter.h"
32#include "gdal.h"
33#include "qgsgdalutils.h"
34#include "qgslayertree.h"
35
36#include <QtConcurrent>
37
39
40QString QgsRasterizeAlgorithm::name() const
41{
42 return QStringLiteral( "rasterize" );
43}
44
45QString QgsRasterizeAlgorithm::displayName() const
46{
47 return QObject::tr( "Convert map to raster" );
48}
49
50QStringList QgsRasterizeAlgorithm::tags() const
51{
52 return QObject::tr( "layer,raster,convert,file,map themes,tiles,render" ).split( ',' );
53}
54
55Qgis::ProcessingAlgorithmFlags QgsRasterizeAlgorithm::flags() const
56{
58}
59
60QString QgsRasterizeAlgorithm::group() const
61{
62 return QObject::tr( "Raster tools" );
63}
64
65QString QgsRasterizeAlgorithm::groupId() const
66{
67 return QStringLiteral( "rastertools" );
68}
69
70void QgsRasterizeAlgorithm::initAlgorithm( const QVariantMap & )
71{
72 addParameter( new QgsProcessingParameterExtent(
73 QStringLiteral( "EXTENT" ),
74 QObject::tr( "Minimum extent to render" )
75 ) );
76 addParameter( new QgsProcessingParameterNumber(
77 QStringLiteral( "EXTENT_BUFFER" ),
78 QObject::tr( "Buffer around tiles in map units" ),
80 0,
81 true,
82 0
83 ) );
84 addParameter( new QgsProcessingParameterNumber(
85 QStringLiteral( "TILE_SIZE" ),
86 QObject::tr( "Tile size" ),
88 1024,
89 false,
90 64
91 ) );
92 addParameter( new QgsProcessingParameterNumber(
93 QStringLiteral( "MAP_UNITS_PER_PIXEL" ),
94 QObject::tr( "Map units per pixel" ),
96 100,
97 true,
98 0
99 ) );
100 addParameter( new QgsProcessingParameterBoolean(
101 QStringLiteral( "MAKE_BACKGROUND_TRANSPARENT" ),
102 QObject::tr( "Make background transparent" ),
103 false
104 ) );
105
106 addParameter( new QgsProcessingParameterMapTheme(
107 QStringLiteral( "MAP_THEME" ),
108 QObject::tr( "Map theme to render" ),
109 QVariant(), true
110 ) );
111
112 addParameter( new QgsProcessingParameterMultipleLayers(
113 QStringLiteral( "LAYERS" ),
114 QObject::tr( "Layers to render" ),
116 QVariant(),
117 true
118 ) );
120 QStringLiteral( "OUTPUT" ),
121 QObject::tr( "Output layer" )
122 ) );
123}
124
125QString QgsRasterizeAlgorithm::shortDescription() const
126{
127 return QObject::tr( "Renders the map canvas to a raster file." );
128}
129
130QString QgsRasterizeAlgorithm::shortHelpString() const
131{
132 return QObject::tr( "This algorithm rasterizes map canvas content.\n\n"
133 "A map theme can be selected to render a predetermined set of layers with a defined style for each layer. "
134 "Alternatively, a set of layers can be selected if no map theme is set. "
135 "If neither map theme nor layer is set, all the visible layers in the set extent will be rendered.\n\n"
136 "The minimum extent entered will internally be extended to a multiple of the tile size." );
137}
138
139QgsRasterizeAlgorithm *QgsRasterizeAlgorithm::createInstance() const
140{
141 return new QgsRasterizeAlgorithm();
142}
143
144
145QVariantMap QgsRasterizeAlgorithm::processAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
146{
147 // Note: MAP_THEME and LAYERS are handled and cloned in prepareAlgorithm
148 const QgsRectangle extent { parameterAsExtent( parameters, QStringLiteral( "EXTENT" ), context, context.project()->crs() ) };
149 const int tileSize { parameterAsInt( parameters, QStringLiteral( "TILE_SIZE" ), context ) };
150 const bool transparent { parameterAsBool( parameters, QStringLiteral( "MAKE_BACKGROUND_TRANSPARENT" ), context ) };
151 const double mapUnitsPerPixel { parameterAsDouble( parameters, QStringLiteral( "MAP_UNITS_PER_PIXEL" ), context ) };
152 const double extentBuffer { parameterAsDouble( parameters, QStringLiteral( "EXTENT_BUFFER" ), context ) };
153 const QString outputLayerFileName { parameterAsOutputLayer( parameters, QStringLiteral( "OUTPUT" ), context ) };
154
155 int xTileCount { static_cast<int>( ceil( extent.width() / mapUnitsPerPixel / tileSize ) ) };
156 int yTileCount { static_cast<int>( ceil( extent.height() / mapUnitsPerPixel / tileSize ) ) };
157 int width { xTileCount * tileSize };
158 int height { yTileCount * tileSize };
159 int nBands { transparent ? 4 : 3 };
160
161 int64_t totalTiles = 0;
162 for ( auto &layer : std::as_const( mMapLayers ) )
163 {
164 if ( QgsMapLayerUtils::isOpenStreetMapLayer( layer.get() ) )
165 {
166 if ( QgsRasterLayer *rasterLayer = qobject_cast<QgsRasterLayer *>( ( layer.get() ) ) )
167 {
168 const QList<double> resolutions = rasterLayer->dataProvider()->nativeResolutions();
169 if ( resolutions.isEmpty() )
170 {
171 continue;
172 }
173
174 if ( totalTiles == 0 )
175 {
176 const QgsCoordinateTransform ct( context.project()->crs(), rasterLayer->crs(), context.transformContext() );
177 QgsRectangle extentLayer;
178 try
179 {
180 extentLayer = ct.transform( extent );
181 }
182 catch ( QgsCsException & )
183 {
184 totalTiles = -1;
185 continue;
186 }
187
188 const double mapUnitsPerPixelLayer = extentLayer.width() / width;
189 int i;
190 for ( i = 0; i < resolutions.size() && resolutions.at( i ) < mapUnitsPerPixelLayer; i++ )
191 {
192 }
193
194 if ( i == resolutions.size() || ( i > 0 && resolutions.at( i ) - mapUnitsPerPixelLayer > mapUnitsPerPixelLayer - resolutions.at( i - 1 ) ) )
195 {
196 i--;
197 }
198
199 const int nbTilesWidth = std::ceil( extentLayer.width() / resolutions.at( i ) / 256 );
200 const int nbTilesHeight = std::ceil( extentLayer.height() / resolutions.at( i ) / 256 );
201 totalTiles = static_cast<int64_t>( nbTilesWidth ) * nbTilesHeight;
202 }
203 feedback->pushInfo( QStringLiteral( "%1" ).arg( totalTiles ) );
204
205 if ( totalTiles > MAXIMUM_OPENSTREETMAP_TILES_FETCH )
206 {
207 // Prevent bulk downloading of tiles from openstreetmap.org as per OSMF tile usage policy
208 feedback->pushFormattedMessage( QObject::tr( "Layer %1 will be skipped as the algorithm leads to bulk downloading behavior which is prohibited by the %2OpenStreetMap Foundation tile usage policy%3" ).arg( rasterLayer->name(), QStringLiteral( "<a href=\"https://operations.osmfoundation.org/policies/tiles/\">" ), QStringLiteral( "</a>" ) ), QObject::tr( "Layer %1 will be skipped as the algorithm leads to bulk downloading behavior which is prohibited by the %2OpenStreetMap Foundation tile usage policy%3" ).arg( rasterLayer->name(), QString(), QString() ) );
209
210 layer->deleteLater();
211 std::vector<std::unique_ptr<QgsMapLayer>>::iterator position = std::find( mMapLayers.begin(), mMapLayers.end(), layer );
212 if ( position != mMapLayers.end() )
213 {
214 mMapLayers.erase( position );
215 }
216 }
217 }
218 }
219 }
220
221 const QString driverName { QgsRasterFileWriter::driverForExtension( QFileInfo( outputLayerFileName ).suffix() ) };
222 if ( driverName.isEmpty() )
223 {
224 throw QgsProcessingException( QObject::tr( "Invalid output raster format" ) );
225 }
226
227 GDALDriverH hOutputFileDriver = GDALGetDriverByName( driverName.toLocal8Bit().constData() );
228 if ( !hOutputFileDriver )
229 {
230 throw QgsProcessingException( QObject::tr( "Error creating GDAL driver" ) );
231 }
232
233 gdal::dataset_unique_ptr hOutputDataset( GDALCreate( hOutputFileDriver, outputLayerFileName.toUtf8().constData(), width, height, nBands, GDALDataType::GDT_Byte, nullptr ) );
234 if ( !hOutputDataset )
235 {
236 throw QgsProcessingException( QObject::tr( "Error creating GDAL output layer" ) );
237 }
238
239 GDALSetProjection( hOutputDataset.get(), context.project()->crs().toWkt( Qgis::CrsWktVariant::PreferredGdal ).toLatin1().constData() );
240 double geoTransform[6];
241 geoTransform[0] = extent.xMinimum();
242 geoTransform[1] = mapUnitsPerPixel;
243 geoTransform[2] = 0;
244 geoTransform[3] = extent.yMaximum();
245 geoTransform[4] = 0;
246 geoTransform[5] = -mapUnitsPerPixel;
247 GDALSetGeoTransform( hOutputDataset.get(), geoTransform );
248
249 int red = context.project()->readNumEntry( QStringLiteral( "Gui" ), "/CanvasColorRedPart", 255 );
250 int green = context.project()->readNumEntry( QStringLiteral( "Gui" ), "/CanvasColorGreenPart", 255 );
251 int blue = context.project()->readNumEntry( QStringLiteral( "Gui" ), "/CanvasColorBluePart", 255 );
252
253 QColor bgColor;
254 if ( transparent )
255 {
256 bgColor = QColor( red, green, blue, 0 );
257 }
258 else
259 {
260 bgColor = QColor( red, green, blue );
261 }
262
263 QgsMapSettings mapSettings;
264 mapSettings.setOutputImageFormat( QImage::Format_ARGB32 );
265 mapSettings.setDestinationCrs( context.project()->crs() );
266 mapSettings.setFlag( Qgis::MapSettingsFlag::Antialiasing, true );
270 mapSettings.setTransformContext( context.transformContext() );
271 mapSettings.setExtentBuffer( extentBuffer );
272 mapSettings.setBackgroundColor( bgColor );
273
274 // Set layers cloned in prepareAlgorithm
275 QList<QgsMapLayer *> layers;
276 for ( const auto &lptr : mMapLayers )
277 {
278 layers.push_back( lptr.get() );
279 }
280 mapSettings.setLayers( layers );
281 mapSettings.setLayerStyleOverrides( mMapThemeStyleOverrides );
282
283 // Start rendering
284 const double extentRatio { mapUnitsPerPixel * tileSize };
285 const int numTiles { xTileCount * yTileCount };
286
287 // Custom deleter for CPL allocation
288 struct CPLDelete
289 {
290 void operator()( uint8_t *ptr ) const
291 {
292 CPLFree( ptr );
293 }
294 };
295
296 QAtomicInt rendered = 0;
297 QMutex rasterWriteLocker;
298
299 const auto renderJob = [&]( const int x, const int y, QgsMapSettings mapSettings ) {
300 QImage image { tileSize, tileSize, QImage::Format::Format_ARGB32 };
301 mapSettings.setOutputDpi( image.logicalDpiX() );
302 mapSettings.setOutputSize( image.size() );
303 QPainter painter { &image };
304 if ( feedback->isCanceled() )
305 {
306 return;
307 }
308 image.fill( transparent ? bgColor.rgba() : bgColor.rgb() );
309 mapSettings.setExtent( QgsRectangle(
310 extent.xMinimum() + x * extentRatio,
311 extent.yMaximum() - ( y + 1 ) * extentRatio,
312 extent.xMinimum() + ( x + 1 ) * extentRatio,
313 extent.yMaximum() - y * extentRatio
314 ) );
315 QgsMapRendererCustomPainterJob job( mapSettings, &painter );
316 job.start();
317 job.waitForFinished();
318
319 gdal::dataset_unique_ptr hIntermediateDataset( QgsGdalUtils::imageToMemoryDataset( image ) );
320 if ( !hIntermediateDataset )
321 {
322 throw QgsProcessingException( QObject::tr( "Error reading tiles from the temporary image" ) );
323 }
324
325 const int xOffset { x * tileSize };
326 const int yOffset { y * tileSize };
327
328 std::unique_ptr<uint8_t, CPLDelete> buffer( static_cast<uint8_t *>( CPLMalloc( sizeof( uint8_t ) * static_cast<size_t>( tileSize * tileSize * nBands ) ) ) );
329 CPLErr err = GDALDatasetRasterIO( hIntermediateDataset.get(), GF_Read, 0, 0, tileSize, tileSize, buffer.get(), tileSize, tileSize, GDT_Byte, nBands, nullptr, 0, 0, 0 );
330 if ( err != CE_None )
331 {
332 throw QgsProcessingException( QObject::tr( "Error reading intermediate raster" ) );
333 }
334
335 {
336 QMutexLocker locker( &rasterWriteLocker );
337 err = GDALDatasetRasterIO( hOutputDataset.get(), GF_Write, xOffset, yOffset, tileSize, tileSize, buffer.get(), tileSize, tileSize, GDT_Byte, nBands, nullptr, 0, 0, 0 );
338 rendered++;
339 feedback->setProgress( static_cast<double>( rendered ) / numTiles * 100.0 );
340 }
341 if ( err != CE_None )
342 {
343 throw QgsProcessingException( QObject::tr( "Error writing output raster" ) );
344 }
345 };
346
347 feedback->setProgress( 0 );
348
349 std::vector<QFuture<void>> futures;
350
351 for ( int x = 0; x < xTileCount; ++x )
352 {
353 for ( int y = 0; y < yTileCount; ++y )
354 {
355 if ( feedback->isCanceled() )
356 {
357 return {};
358 }
359 futures.push_back( QtConcurrent::run( renderJob, x, y, mapSettings ) );
360 }
361 }
362
363 for ( auto &f : futures )
364 {
365 f.waitForFinished();
366 }
367
368 return { { QStringLiteral( "OUTPUT" ), outputLayerFileName } };
369}
370
371
372bool QgsRasterizeAlgorithm::prepareAlgorithm( const QVariantMap &parameters, QgsProcessingContext &context, QgsProcessingFeedback *feedback )
373{
374 Q_UNUSED( feedback )
375 // Retrieve and clone layers
376 const QString mapTheme { parameterAsString( parameters, QStringLiteral( "MAP_THEME" ), context ) };
377 const QList<QgsMapLayer *> mapLayers { parameterAsLayerList( parameters, QStringLiteral( "LAYERS" ), context ) };
378 if ( !mapTheme.isEmpty() && context.project()->mapThemeCollection()->hasMapTheme( mapTheme ) )
379 {
380 const auto constLayers { context.project()->mapThemeCollection()->mapThemeVisibleLayers( mapTheme ) };
381 for ( const QgsMapLayer *ml : constLayers )
382 {
383 mMapLayers.push_back( std::unique_ptr<QgsMapLayer>( ml->clone() ) );
384 }
385 mMapThemeStyleOverrides = context.project()->mapThemeCollection()->mapThemeStyleOverrides( mapTheme );
386 }
387 else if ( !mapLayers.isEmpty() )
388 {
389 for ( const QgsMapLayer *ml : std::as_const( mapLayers ) )
390 {
391 mMapLayers.push_back( std::unique_ptr<QgsMapLayer>( ml->clone() ) );
392 }
393 }
394 // Still no layers? Get them all from the project
395 if ( mMapLayers.size() == 0 )
396 {
397 QList<QgsMapLayer *> layers;
398 QgsLayerTree *root = context.project()->layerTreeRoot();
399 for ( QgsLayerTreeLayer *nodeLayer : root->findLayers() )
400 {
401 QgsMapLayer *layer = nodeLayer->layer();
402 if ( nodeLayer->isVisible() && root->layerOrder().contains( layer ) )
403 layers << layer;
404 }
405
406 for ( const QgsMapLayer *ml : std::as_const( layers ) )
407 {
408 mMapLayers.push_back( std::unique_ptr<QgsMapLayer>( ml->clone() ) );
409 }
410 }
411 return mMapLayers.size() > 0;
412}
413
414
@ MapLayer
Any map layer type (raster, vector, mesh, point cloud, annotation or plugin layer)
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
Definition qgis.h:3410
@ PreferredGdal
Preferred format for conversion of CRS to WKT for use with the GDAL library.
@ RequiresProject
The algorithm requires that a valid QgsProject is available from the processing context in order to e...
@ RenderMapTile
Draw map such that there are no problems between adjacent tiles.
@ Antialiasing
Enable anti-aliasing for map rendering.
@ UseAdvancedEffects
Enable layer opacity and blending effects.
@ HighQualityImageTransforms
Enable high quality image transformations, which results in better appearance of scaled or rotated ra...
QString toWkt(Qgis::CrsWktVariant variant=Qgis::CrsWktVariant::Wkt1Gdal, bool multiline=false, int indentationWidth=4) const
Returns a WKT representation of this CRS.
Class for doing transforms between two map coordinate systems.
Custom exception class for Coordinate Reference System related exceptions.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
void setProgress(double progress)
Sets the current progress for the feedback object.
Definition qgsfeedback.h:61
static gdal::dataset_unique_ptr imageToMemoryDataset(const QImage &image)
Converts an image to a GDAL memory dataset by borrowing image data.
Layer tree node points to a map layer.
Namespace with helper functions for layer tree operations.
QList< QgsMapLayer * > layerOrder() const
The order in which layers will be rendered on the canvas.
static bool isOpenStreetMapLayer(QgsMapLayer *layer)
Returns true if the layer is served by OpenStreetMap server.
Base class for all map layer types.
Definition qgsmaplayer.h:76
Job implementation that renders everything sequentially using a custom painter.
The QgsMapSettings class contains configuration for rendering of the map.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers to render in the map.
void setOutputDpi(double dpi)
Sets the dpi (dots per inch) used for conversion between real world units (e.g.
void setOutputImageFormat(QImage::Format format)
sets format of internal QImage
void setLayerStyleOverrides(const QMap< QString, QString > &overrides)
Sets the map of map layer style overrides (key: layer ID, value: style name) where a different style ...
void setExtent(const QgsRectangle &rect, bool magnified=true)
Sets the coordinates of the rectangle which should be rendered.
void setExtentBuffer(double buffer)
Sets the buffer in map units to use around the visible extent for rendering symbols whose correspondi...
void setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
void setOutputSize(QSize size)
Sets the size of the resulting map image, in pixels.
void setBackgroundColor(const QColor &color)
Sets the background color of the map.
void setFlag(Qgis::MapSettingsFlag flag, bool on=true)
Enable or disable a particular flag (other flags are not affected)
void setDestinationCrs(const QgsCoordinateReferenceSystem &crs)
Sets the destination crs (coordinate reference system) for the map render.
bool hasMapTheme(const QString &name) const
Returns whether a map theme with a matching name exists.
QList< QgsMapLayer * > mapThemeVisibleLayers(const QString &name) const
Returns the list of layers that are visible for the specified map theme.
QMap< QString, QString > mapThemeStyleOverrides(const QString &name)
Gets layer style overrides (for QgsMapSettings) of the visible layers for given map theme.
virtual Qgis::ProcessingAlgorithmFlags flags() const
Returns the flags indicating how and when the algorithm operates and should be exposed to users.
Contains information about the context in which a processing algorithm is executed.
QgsCoordinateTransformContext transformContext() const
Returns the coordinate transform context.
QgsProject * project() const
Returns the project in which the algorithm is being executed.
Custom exception class for processing related exceptions.
Base class for providing feedback from a processing algorithm.
virtual void pushInfo(const QString &info)
Pushes a general informational message from the algorithm.
virtual void pushFormattedMessage(const QString &html, const QString &text)
Pushes a pre-formatted message from the algorithm.
A boolean parameter for processing algorithms.
A rectangular map extent parameter for processing algorithms.
A map theme parameter for processing algorithms, allowing users to select an existing map theme from ...
A parameter for processing algorithms which accepts multiple map layers.
A numeric parameter for processing algorithms.
A raster layer destination parameter, for specifying the destination path for a raster layer created ...
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
Reads an integer from the specified scope and key.
QgsMapThemeCollection * mapThemeCollection
Definition qgsproject.h:115
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
QgsCoordinateReferenceSystem crs
Definition qgsproject.h:112
static QString driverForExtension(const QString &extension)
Returns the GDAL driver name for a specified file extension.
Represents a raster layer.
A rectangle specified with double values.
std::unique_ptr< std::remove_pointer< GDALDatasetH >::type, GDALDatasetCloser > dataset_unique_ptr
Scoped GDAL dataset.
#define MAXIMUM_OPENSTREETMAP_TILES_FETCH