30int tile2tms(
const int y,
const int zoom )
32 double n = std::pow( 2, zoom );
33 return (
int ) std::floor( n - y - 1 );
36int lon2tileX(
const double lon,
const int z )
38 return (
int ) ( std::floor( ( lon + 180.0 ) / 360.0 * ( 1 << z ) ) );
41int lat2tileY(
const double lat,
const int z )
43 double latRad = lat * M_PI / 180.0;
44 return (
int ) ( std::floor( ( 1.0 - std::asinh( std::tan( latRad ) ) / M_PI ) / 2.0 * ( 1 << z ) ) );
47double tileX2lon(
const int x,
const int z )
49 return x / ( double ) ( 1 << z ) * 360.0 - 180;
52double tileY2lat(
const int y,
const int z )
54 double n = M_PI - 2.0 * M_PI * y / ( double ) ( 1 << z );
55 return 180.0 / M_PI * std::atan( 0.5 * ( std::exp( n ) - std::exp( -n ) ) );
58void extent2TileXY(
QgsRectangle extent,
const int zoom,
int &xMin,
int &yMin,
int &xMax,
int &yMax )
60 xMin = lon2tileX( extent.
xMinimum(), zoom );
61 yMin = lat2tileY( extent.
yMinimum(), zoom );
62 xMax = lon2tileX( extent.
xMaximum(), zoom );
63 yMax = lat2tileY( extent.
xMaximum(), zoom );
66QList<MetaTile> getMetatiles(
const QgsRectangle extent,
const int zoom,
const int tileSize )
68 int minX = lon2tileX( extent.
xMinimum(), zoom );
69 int minY = lat2tileY( extent.
yMaximum(), zoom );
70 int maxX = lon2tileX( extent.
xMaximum(), zoom );
71 int maxY = lat2tileY( extent.
yMinimum(), zoom );
75 QMap<QString, MetaTile> tiles;
76 for (
int x = minX; x <= maxX; x++ )
79 for (
int y = minY; y <= maxY; y++ )
81 QString key = QStringLiteral(
"%1:%2" ).arg( (
int ) ( i / tileSize ) ).arg( (
int ) ( j / tileSize ) );
82 MetaTile tile = tiles.value( key, MetaTile() );
83 tile.addTile( i % tileSize, j % tileSize, Tile( x, y, zoom ) );
84 tiles.insert( key, tile );
89 return tiles.values();
94QString QgsXyzTilesBaseAlgorithm::group()
const
96 return QObject::tr(
"Raster tools" );
99QString QgsXyzTilesBaseAlgorithm::groupId()
const
101 return QStringLiteral(
"rastertools" );
109void QgsXyzTilesBaseAlgorithm::createCommonParameters()
115 addParameter(
new QgsProcessingParameterColor( QStringLiteral(
"BACKGROUND_COLOR" ), QObject::tr(
"Background color" ), QColor( Qt::transparent ),
true,
true ) );
117 addParameter(
new QgsProcessingParameterEnum( QStringLiteral(
"TILE_FORMAT" ), QObject::tr(
"Tile format" ), QStringList() << QStringLiteral(
"PNG" ) << QStringLiteral(
"JPG" ),
false, 0 ) );
124 Q_UNUSED( feedback );
129 QSet<QString> visibleLayers;
132 if ( layer->isVisible() )
134 visibleLayers << layer->layer()->id();
141 if ( visibleLayers.contains( layer->id() ) )
144 clonedLayer->moveToThread(
nullptr );
145 mLayers << clonedLayer;
149 QgsRectangle extent = parameterAsExtent( parameters, QStringLiteral(
"EXTENT" ), context );
154 mExtent = ct.transformBoundingBox( extent );
158 feedback->
reportError( QObject::tr(
"Could not transform the extent into the project CRS" ),
true );
162 mMinZoom = parameterAsInt( parameters, QStringLiteral(
"ZOOM_MIN" ), context );
163 mMaxZoom = parameterAsInt( parameters, QStringLiteral(
"ZOOM_MAX" ), context );
164 mDpi = parameterAsInt( parameters, QStringLiteral(
"DPI" ), context );
165 mBackgroundColor = parameterAsColor( parameters, QStringLiteral(
"BACKGROUND_COLOR" ), context );
166 mAntialias = parameterAsBool( parameters, QStringLiteral(
"ANTIALIAS" ), context );
167 mTileFormat = parameterAsEnum( parameters, QStringLiteral(
"TILE_FORMAT" ), context ) ? QStringLiteral(
"JPG" ) : QStringLiteral(
"PNG" );
168 mJpgQuality = mTileFormat == QLatin1String(
"JPG" ) ? parameterAsInt( parameters, QStringLiteral(
"QUALITY" ), context ) : -1;
169 mMetaTileSize = parameterAsInt( parameters, QStringLiteral(
"METATILESIZE" ), context );
172 mFeedback = feedback;
180 mWgs84Extent = mSrc2Wgs.transformBoundingBox( mExtent );
184 feedback->
reportError( QObject::tr(
"Could not transform the extent into WGS84" ),
true );
188 if ( parameters.contains( QStringLiteral(
"TILE_WIDTH" ) ) )
190 mTileWidth = parameterAsInt( parameters, QStringLiteral(
"TILE_WIDTH" ), context );
193 if ( parameters.contains( QStringLiteral(
"TILE_HEIGHT" ) ) )
195 mTileHeight = parameterAsInt( parameters, QStringLiteral(
"TILE_HEIGHT" ), context );
198 if ( mTileFormat != QLatin1String(
"PNG" ) && mBackgroundColor.alpha() != 255 )
200 feedback->
pushWarning( QObject::tr(
"Background color setting ignored, the JPG format only supports fully opaque colors" ) );
210 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
215 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( layer->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( layer->name(), QString(), QString() ) );
216 mLayers.removeAll( layer );
223void QgsXyzTilesBaseAlgorithm::startJobs()
225 while ( mRendererJobs.size() < mThreadsNumber && !mMetaTiles.empty() )
227 MetaTile metaTile = mMetaTiles.takeFirst();
232 settings.
setExtent( mWgs2Mercator.transformBoundingBox( metaTile.extent() ) );
244 if ( mTileFormat == QLatin1String(
"PNG" ) || mBackgroundColor.alpha() == 255 )
248 QSize size( mTileWidth * metaTile.rows, mTileHeight * metaTile.cols );
260 mRendererJobs.insert( job, metaTile );
268QString QgsXyzTilesDirectoryAlgorithm::name()
const
270 return QStringLiteral(
"tilesxyzdirectory" );
273QString QgsXyzTilesDirectoryAlgorithm::displayName()
const
275 return QObject::tr(
"Generate XYZ tiles (Directory)" );
278QStringList QgsXyzTilesDirectoryAlgorithm::tags()
const
280 return QObject::tr(
"tiles,xyz,tms,directory" ).split(
',' );
283QString QgsXyzTilesDirectoryAlgorithm::shortHelpString()
const
285 return QObject::tr(
"Generates XYZ tiles of map canvas content and saves them as individual images in a directory." );
288QgsXyzTilesDirectoryAlgorithm *QgsXyzTilesDirectoryAlgorithm::createInstance()
const
290 return new QgsXyzTilesDirectoryAlgorithm();
293void QgsXyzTilesDirectoryAlgorithm::initAlgorithm(
const QVariantMap & )
295 createCommonParameters();
298 addParameter(
new QgsProcessingParameterBoolean( QStringLiteral(
"TMS_CONVENTION" ), QObject::tr(
"Use inverted tile Y axis (TMS convention)" ),
false ) );
300 std::unique_ptr<QgsProcessingParameterString> titleParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"HTML_TITLE" ), QObject::tr(
"Leaflet HTML output title" ), QVariant(),
false,
true );
302 addParameter( titleParam.release() );
303 std::unique_ptr<QgsProcessingParameterString> attributionParam = std::make_unique<QgsProcessingParameterString>( QStringLiteral(
"HTML_ATTRIBUTION" ), QObject::tr(
"Leaflet HTML output attribution" ), QVariant(),
false,
true );
305 addParameter( attributionParam.release() );
306 std::unique_ptr<QgsProcessingParameterBoolean> osmParam = std::make_unique<QgsProcessingParameterBoolean>( QStringLiteral(
"HTML_OSM" ), QObject::tr(
"Include OpenStreetMap basemap in Leaflet HTML output" ),
false );
308 addParameter( osmParam.release() );
311 addParameter(
new QgsProcessingParameterFileDestination( QStringLiteral(
"OUTPUT_HTML" ), QObject::tr(
"Output html (Leaflet)" ), QObject::tr(
"HTML files (*.html)" ), QVariant(),
true ) );
316 const bool tms = parameterAsBoolean( parameters, QStringLiteral(
"TMS_CONVENTION" ), context );
317 const QString title = parameterAsString( parameters, QStringLiteral(
"HTML_TITLE" ), context );
318 const QString attribution = parameterAsString( parameters, QStringLiteral(
"HTML_ATTRIBUTION" ), context );
319 const bool useOsm = parameterAsBoolean( parameters, QStringLiteral(
"HTML_OSM" ), context );
320 QString outputDir = parameterAsString( parameters, QStringLiteral(
"OUTPUT_DIRECTORY" ), context );
321 const QString outputHtml = parameterAsString( parameters, QStringLiteral(
"OUTPUT_HTML" ), context );
323 mOutputDir = outputDir;
327 for (
int z = mMinZoom; z <= mMaxZoom; z++ )
332 mMetaTiles += getMetatiles( mWgs84Extent, z, mMetaTileSize );
333 feedback->
pushWarning( QObject::tr(
"%1 tiles will be created for zoom level %2" ).arg( mMetaTiles.size() - mTotalTiles ).arg( z ) );
334 mTotalTiles = mMetaTiles.size();
336 feedback->
pushWarning( QObject::tr(
"A total of %1 tiles will be created" ).arg( mTotalTiles ) );
338 checkLayersUsagePolicy( feedback );
340 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
342 layer->moveToThread( QThread::currentThread() );
351 qDeleteAll( mLayers );
355 results.insert( QStringLiteral(
"OUTPUT_DIRECTORY" ), outputDir );
357 if ( !outputHtml.isEmpty() )
359 QString osm = QStringLiteral(
360 "var osm_layer = L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png',"
361 "{minZoom: %1, maxZoom: %2, attribution: '© <a href=\"https://www.openstreetmap.org/copyright\">OpenStreetMap</a> contributors'}).addTo(map);"
366 QString addOsm = useOsm ? osm : QString();
367 QString tmsConvention = tms ? QStringLiteral(
"true" ) : QStringLiteral(
"false" );
368 QString attr = attribution.isEmpty() ? QStringLiteral(
"Created by QGIS" ) : attribution;
369 QString tileSource = QStringLiteral(
"'file:///%1/{z}/{x}/{y}.%2'" )
370 .arg( outputDir.replace(
"\\",
"/" ).toHtmlEscaped() )
371 .arg( mTileFormat.toLower() );
373 QString html = QStringLiteral(
374 "<!DOCTYPE html><html><head><title>%1</title><meta charset=\"utf-8\"/>"
375 "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">"
376 "<link rel=\"stylesheet\" href=\"https://unpkg.com/[email protected]/dist/leaflet.css\""
377 "integrity=\"sha384-sHL9NAb7lN7rfvG5lfHpm643Xkcjzp4jFvuavGOndn6pjVqS6ny56CAt3nsEVT4H\""
380 "integrity=\"sha384-cxOPjt7s7Iz04uaHJceBmS+qpjv2JkIHNVcuOrM+YHwZOmJGBXI00mdUXEq65HTH\""
381 "crossorigin=\"\"></script>"
382 "<style type=\"text/css\">body {margin: 0;padding: 0;} html, body, #map{width: 100%;height: 100%;}</style></head>"
383 "<body><div id=\"map\"></div><script>"
384 "var map = L.map('map', {attributionControl: false}).setView([%2, %3], %4);"
385 "L.control.attribution({prefix: false}).addTo(map);"
387 "var tilesource_layer = L.tileLayer(%6, {minZoom: %7, maxZoom: %8, tms: %9, attribution: '%10'}).addTo(map);"
388 "</script></body></html>"
390 .arg( title.isEmpty() ? QStringLiteral(
"Leaflet preview" ) : title )
391 .arg( mWgs84Extent.center().y() )
392 .arg( mWgs84Extent.center().x() )
393 .arg( ( mMaxZoom + mMinZoom ) / 2 )
398 .arg( tmsConvention )
401 QFile htmlFile( outputHtml );
402 if ( !htmlFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
406 QTextStream fout( &htmlFile );
407#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
408 fout.setCodec(
"UTF-8" );
412 results.insert( QStringLiteral(
"OUTPUT_HTML" ), outputHtml );
420 MetaTile metaTile = mRendererJobs.value( job );
423 QMap<QPair<int, int>, Tile>::const_iterator it = metaTile.tiles.constBegin();
424 while ( it != metaTile.tiles.constEnd() )
426 QPair<int, int> tm = it.key();
427 Tile tile = it.value();
428 QImage tileImage = img.copy( mTileWidth * tm.first, mTileHeight * tm.second, mTileWidth, mTileHeight );
429 QDir tileDir( QStringLiteral(
"%1/%2/%3" ).arg( mOutputDir ).arg( tile.z ).arg( tile.x ) );
430 tileDir.mkpath( tileDir.absolutePath() );
434 y = tile2tms( y, tile.z );
436 tileImage.save( QStringLiteral(
"%1/%2.%3" ).arg( tileDir.absolutePath() ).arg( y ).arg( mTileFormat.toLower() ), mTileFormat.toStdString().c_str(), mJpgQuality );
440 mRendererJobs.remove( job );
443 mFeedback->setProgress( 100.0 * ( mProcessedTiles++ ) / mTotalTiles );
445 if ( mFeedback->isCanceled() )
447 while ( mRendererJobs.size() > 0 )
451 mRendererJobs.remove( j );
454 mRendererJobs.clear();
462 if ( mMetaTiles.size() > 0 )
466 else if ( mMetaTiles.size() == 0 && mRendererJobs.size() == 0 )
477QString QgsXyzTilesMbtilesAlgorithm::name()
const
479 return QStringLiteral(
"tilesxyzmbtiles" );
482QString QgsXyzTilesMbtilesAlgorithm::displayName()
const
484 return QObject::tr(
"Generate XYZ tiles (MBTiles)" );
487QStringList QgsXyzTilesMbtilesAlgorithm::tags()
const
489 return QObject::tr(
"tiles,xyz,tms,mbtiles" ).split(
',' );
492QString QgsXyzTilesMbtilesAlgorithm::shortHelpString()
const
494 return QObject::tr(
"Generates XYZ tiles of map canvas content and saves them as an MBTiles file." );
497QgsXyzTilesMbtilesAlgorithm *QgsXyzTilesMbtilesAlgorithm::createInstance()
const
499 return new QgsXyzTilesMbtilesAlgorithm();
502void QgsXyzTilesMbtilesAlgorithm::initAlgorithm(
const QVariantMap & )
504 createCommonParameters();
510 const QString outputFile = parameterAsString( parameters, QStringLiteral(
"OUTPUT_FILE" ), context );
512 mMbtilesWriter = std::make_unique<QgsMbTiles>( outputFile );
513 if ( !mMbtilesWriter->create() )
517 mMbtilesWriter->setMetadataValue(
"format", mTileFormat.toLower() );
518 mMbtilesWriter->setMetadataValue(
"name", QFileInfo( outputFile ).baseName() );
519 mMbtilesWriter->setMetadataValue(
"description", QFileInfo( outputFile ).baseName() );
520 mMbtilesWriter->setMetadataValue(
"version", QStringLiteral(
"1.1" ) );
521 mMbtilesWriter->setMetadataValue(
"type", QStringLiteral(
"overlay" ) );
522 mMbtilesWriter->setMetadataValue(
"minzoom", QString::number( mMinZoom ) );
523 mMbtilesWriter->setMetadataValue(
"maxzoom", QString::number( mMaxZoom ) );
524 QString boundsStr = QString(
"%1,%2,%3,%4" )
525 .arg( mWgs84Extent.xMinimum() )
526 .arg( mWgs84Extent.yMinimum() )
527 .arg( mWgs84Extent.xMaximum() )
528 .arg( mWgs84Extent.yMaximum() );
529 mMbtilesWriter->setMetadataValue(
"bounds", boundsStr );
532 for (
int z = mMinZoom; z <= mMaxZoom; z++ )
537 mMetaTiles += getMetatiles( mWgs84Extent, z, mMetaTileSize );
538 feedback->
pushInfo( QObject::tr(
"%1 tiles will be created for zoom level %2" ).arg( mMetaTiles.size() - mTotalTiles ).arg( z ) );
539 mTotalTiles = mMetaTiles.size();
541 feedback->
pushInfo( QObject::tr(
"A total of %1 tiles will be created" ).arg( mTotalTiles ) );
543 checkLayersUsagePolicy( feedback );
545 for (
QgsMapLayer *layer : std::as_const( mLayers ) )
547 layer->moveToThread( QThread::currentThread() );
556 qDeleteAll( mLayers );
560 results.insert( QStringLiteral(
"OUTPUT_FILE" ), outputFile );
566 MetaTile metaTile = mRendererJobs.value( job );
569 QMap<QPair<int, int>, Tile>::const_iterator it = metaTile.tiles.constBegin();
570 while ( it != metaTile.tiles.constEnd() )
572 QPair<int, int> tm = it.key();
573 Tile tile = it.value();
574 QImage tileImage = img.copy( mTileWidth * tm.first, mTileHeight * tm.second, mTileWidth, mTileHeight );
576 QBuffer buffer( &ba );
577 buffer.open( QIODevice::WriteOnly );
578 tileImage.save( &buffer, mTileFormat.toStdString().c_str(), mJpgQuality );
579 mMbtilesWriter->setTileData( tile.z, tile.x, tile2tms( tile.y, tile.z ), ba );
583 mRendererJobs.remove( job );
586 mFeedback->setProgress( 100.0 * ( mProcessedTiles++ ) / mTotalTiles );
588 if ( mFeedback->isCanceled() )
590 while ( mRendererJobs.size() > 0 )
594 mRendererJobs.remove( j );
597 mRendererJobs.clear();
605 if ( mMetaTiles.size() > 0 )
609 else if ( mMetaTiles.size() == 0 && mRendererJobs.size() == 0 )
@ UsePartialCandidates
Whether to use also label candidates that are partially outside of the map view.
QFlags< ProcessingAlgorithmFlag > ProcessingAlgorithmFlags
Flags indicating how and when an algorithm operates and should be exposed to users.
@ RequiresProject
The algorithm requires that a valid QgsProject is available from the processing context in order to e...
@ Advanced
Parameter is an advanced parameter which should be hidden from users by default.
@ Antialiasing
Enable anti-aliasing for map rendering.
This class represents a coordinate reference system (CRS).
Custom exception class for Coordinate Reference System related exceptions.
static QgsExpressionContextScope * mapSettingsScope(const QgsMapSettings &mapSettings)
Creates a new scope which contains variables and functions relating to a QgsMapSettings object.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
bool isCanceled() const
Tells whether the operation has been canceled already.
Stores global configuration for labeling engine.
void setFlag(Qgis::LabelingFlag f, bool enabled=true)
Sets whether a particual flag is enabled.
QList< QgsLayerTreeLayer * > findLayers() const
Find all layer nodes.
Layer tree node points to a map layer.
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.
virtual QgsMapLayer * clone() const =0
Returns a new instance equivalent to this one except for the id which is still unique.
void finished()
emitted when asynchronous rendering is finished (or canceled).
void start()
Start the rendering job and immediately return.
Job implementation that renders everything sequentially in one thread.
QImage renderedImage() override
Gets a preview/resulting image.
void cancel() override
Stop the rendering job - does not return until the job has terminated.
The QgsMapSettings class contains configuration for rendering of the map.
const QgsLabelingEngineSettings & labelingEngineSettings() const
Returns the global configuration of the labeling engine.
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 setExtent(const QgsRectangle &rect, bool magnified=true)
Sets the coordinates of the rectangle which should be rendered.
void setExpressionContext(const QgsExpressionContext &context)
Sets the expression context.
void setLabelingEngineSettings(const QgsLabelingEngineSettings &settings)
Sets the global configuration of the labeling engine.
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.
const QgsExpressionContext & expressionContext() const
Gets the expression context.
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.
int maximumThreads() const
Returns the (optional) number of threads to use when running algorithms.
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 pushWarning(const QString &warning)
Pushes a warning informational message from the algorithm.
virtual void pushFormattedMessage(const QString &html, const QString &text)
Pushes a pre-formatted message from the algorithm.
virtual void reportError(const QString &error, bool fatalError=false)
Reports that the algorithm encountered an error while executing.
A boolean parameter for processing algorithms.
A color parameter for processing algorithms.
An enum based parameter for processing algorithms, allowing for selection from predefined values.
A rectangular map extent parameter for processing algorithms.
A generic file based destination parameter, for specifying the destination path for a file (non-map l...
A folder destination parameter, for specifying the destination path for a folder created by the algor...
A numeric parameter for processing algorithms.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
QgsLayerTree * layerTreeRoot() const
Returns pointer to the root (invisible) node of the project's layer tree.
QgsCoordinateReferenceSystem crs
A rectangle specified with double values.
#define MAXIMUM_OPENSTREETMAP_TILES_FETCH