QGIS API Documentation 3.99.0-Master (8e76e220402)
Loading...
Searching...
No Matches
qgsterraintexturegenerator_p.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsterraintexturegenerator_p.cpp
3 --------------------------------------
4 Date : July 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17
18#include "qgs3dmapsettings.h"
20#include "qgseventtracing.h"
22#include "qgsmapsettings.h"
24#include "qgsproject.h"
25
26#include <QString>
27
28#include "moc_qgsterraintexturegenerator_p.cpp"
29
30using namespace Qt::StringLiterals;
31
33
34QgsTerrainTextureGenerator::QgsTerrainTextureGenerator( const Qgs3DMapSettings &map )
35 : mMap( map )
36 , mTextureSize( QSize( mMap.terrainSettings()->mapTileResolution(), mMap.terrainSettings()->mapTileResolution() ) )
37{
38}
39
40int QgsTerrainTextureGenerator::render( const QgsRectangle &extent, QgsChunkNodeId tileId, const QString &debugText )
41{
42 QgsMapSettings mapSettings( baseMapSettings() );
43 mapSettings.setExtent( extent );
44 QSize size = QSize( mTextureSize );
45
46 QgsRectangle clippedExtent = extent;
47 if ( mMap.sceneMode() == Qgis::SceneMode::Local && mMap.terrainGenerator()->type() == QgsTerrainGenerator::Flat )
48 {
49 // The flat terrain generator might have non-square tiles, clipped at the scene's extent.
50 // We need to produce non-square textures for those cases.
51 clippedExtent = extent.intersect( mMap.extent() );
52 mapSettings.setExtent( clippedExtent );
53 }
54 if ( !qgsDoubleNear( clippedExtent.width(), clippedExtent.height() ) )
55 {
56 if ( clippedExtent.height() > clippedExtent.width() )
57 size.setWidth( static_cast< int >( std::round( size.width() * clippedExtent.width() / clippedExtent.height() ) ) );
58 else if ( clippedExtent.height() < clippedExtent.width() )
59 size.setHeight( static_cast< int >( std::round( size.height() * clippedExtent.height() / clippedExtent.width() ) ) );
60 }
61 mapSettings.setOutputSize( size );
62
63 QgsEventTracing::addEvent( QgsEventTracing::AsyncBegin, u"3D"_s, u"Texture"_s, tileId.text() );
64
66 connect( job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
67
68 JobData jobData;
69 jobData.jobId = ++mLastJobId;
70 jobData.tileId = tileId;
71 jobData.job = job;
72 jobData.extent = extent;
73 jobData.debugText = debugText;
74
75 mJobs.insert( job, jobData ); //store job data just before launching the job
76 job->start();
77
78 // QgsDebugMsgLevel( u"added job: %1 .... in queue: %2"_s.arg( jobData.jobId ).arg( jobs.count() ), 2);
79 return jobData.jobId;
80}
81
82void QgsTerrainTextureGenerator::cancelJob( int jobId )
83{
84 for ( const JobData &jd : std::as_const( mJobs ) )
85 {
86 if ( jd.jobId == jobId )
87 {
88 // QgsDebugMsgLevel( u"canceling job %1"_s.arg( jobId ), 2 );
89 disconnect( jd.job, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
90 connect( jd.job, &QgsMapRendererJob::finished, jd.job, &QgsMapRendererSequentialJob::deleteLater );
91 jd.job->cancelWithoutBlocking();
92 mJobs.remove( jd.job );
93 return;
94 }
95 }
96 Q_ASSERT( false && "requested job ID does not exist!" );
97}
98
99void QgsTerrainTextureGenerator::waitForFinished()
100{
101 for ( auto it = mJobs.keyBegin(); it != mJobs.keyEnd(); it++ )
102 disconnect( *it, &QgsMapRendererJob::finished, this, &QgsTerrainTextureGenerator::onRenderingFinished );
103 QVector<QgsMapRendererSequentialJob *> toBeDeleted;
104 for ( auto it = mJobs.constBegin(); it != mJobs.constEnd(); it++ )
105 {
106 QgsMapRendererSequentialJob *mapJob = it.key();
107 mapJob->waitForFinished();
108 JobData jobData = it.value();
109 toBeDeleted.push_back( mapJob );
110
111 QImage img = mapJob->renderedImage();
112
113 if ( mMap.showTerrainTilesInfo() )
114 {
115 // extra tile information for debugging
116 QPainter p( &img );
117 p.setPen( Qt::red );
118 p.setBackgroundMode( Qt::OpaqueMode );
119 QFont font = p.font();
120 font.setPixelSize( std::max( 30, mMap.terrainSettings()->mapTileResolution() / 6 ) );
121 p.setFont( font );
122 p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
123 p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
124 p.end();
125 }
126
127 // pass QImage further
128 emit tileReady( jobData.jobId, img );
129 }
130
131 for ( QgsMapRendererSequentialJob *mapJob : toBeDeleted )
132 {
133 mJobs.remove( mapJob );
134 mapJob->deleteLater();
135 }
136}
137
138void QgsTerrainTextureGenerator::onRenderingFinished()
139{
140 QgsMapRendererSequentialJob *mapJob = static_cast<QgsMapRendererSequentialJob *>( sender() );
141
142 Q_ASSERT( mJobs.contains( mapJob ) );
143 JobData jobData = mJobs.value( mapJob );
144
145 QImage img = mapJob->renderedImage();
146
147 if ( mMap.showTerrainTilesInfo() )
148 {
149 // extra tile information for debugging
150 QPainter p( &img );
151 p.setPen( Qt::red );
152 p.setBackgroundMode( Qt::OpaqueMode );
153 QFont font = p.font();
154 font.setPixelSize( std::max( 30, mMap.terrainSettings()->mapTileResolution() / 6 ) );
155 p.setFont( font );
156 p.drawRect( 0, 0, img.width() - 1, img.height() - 1 );
157 p.drawText( img.rect(), jobData.debugText, QTextOption( Qt::AlignCenter ) );
158 p.end();
159 }
160
161 mapJob->deleteLater();
162 mJobs.remove( mapJob );
163
164 // QgsDebugMsgLevel( u"finished job %1 ... in queue: %2"_s.arg( jobData.jobId).arg( jobs.count() ), 2 );
165
166 QgsEventTracing::addEvent( QgsEventTracing::AsyncEnd, u"3D"_s, u"Texture"_s, jobData.tileId.text() );
167
168 // pass QImage further
169 emit tileReady( jobData.jobId, img );
170}
171
172QgsMapSettings QgsTerrainTextureGenerator::baseMapSettings()
173{
174 QgsMapSettings mapSettings;
175
176 mapSettings.setOutputSize( mTextureSize );
177 mapSettings.setDestinationCrs( mMap.sceneMode() == Qgis::SceneMode::Globe ? mMap.crs().toGeographicCrs() : mMap.crs() );
178 mapSettings.setBackgroundColor( mMap.backgroundColor() );
179 mapSettings.setFlag( Qgis::MapSettingsFlag::DrawLabeling, mMap.showLabels() );
181 mapSettings.setTransformContext( mMap.transformContext() );
182 mapSettings.setPathResolver( mMap.pathResolver() );
183 mapSettings.setRendererUsage( mMap.rendererUsage() );
184
185 QList<QgsMapLayer *> layers;
186 QgsMapThemeCollection *mapThemes = mMap.mapThemeCollection();
187 QString mapThemeName = mMap.terrainMapTheme();
188 if ( mapThemeName.isEmpty() || !mapThemes || !mapThemes->hasMapTheme( mapThemeName ) )
189 {
190 layers = mMap.layers();
191 }
192 else
193 {
194 layers = mapThemes->mapThemeVisibleLayers( mapThemeName );
195 mapSettings.setLayerStyleOverrides( mapThemes->mapThemeStyleOverrides( mapThemeName ) );
196 }
197 layers.erase( std::remove_if( layers.begin(), layers.end(), []( const QgsMapLayer *layer ) { return layer->renderer3D(); } ), layers.end() );
198 mapSettings.setLayers( layers );
199
200 return mapSettings;
201}
202
@ Globe
Scene is represented as a globe using a geocentric CRS.
Definition qgis.h:4252
@ Local
Local scene based on a projected CRS.
Definition qgis.h:4251
@ DrawLabeling
Enable drawing of labels on top of the map.
Definition qgis.h:2779
@ Render3DMap
Render is for a 3D map.
Definition qgis.h:2788
Definition of the world.
Base class for all map layer types.
Definition qgsmaplayer.h:83
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 waitForFinished() override
Block until the job has finished.
Contains configuration for rendering maps.
void setLayers(const QList< QgsMapLayer * > &layers)
Sets the list of layers to render in the map.
void setRendererUsage(Qgis::RendererUsage rendererUsage)
Sets the rendering usage.
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 setTransformContext(const QgsCoordinateTransformContext &context)
Sets the coordinate transform context, which stores various information regarding which datum transfo...
void setPathResolver(const QgsPathResolver &resolver)
Sets the path resolver for conversion between relative and absolute paths during rendering operations...
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.
Container class that allows storage of map themes consisting of visible map layers and layer styles.
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.
A rectangle specified with double values.
QgsRectangle intersect(const QgsRectangle &rect) const
Returns the intersection with the given rectangle.
@ Flat
The whole terrain is flat area.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6935