QGIS API Documentation 3.99.0-Master (752b475928d)
Loading...
Searching...
No Matches
qgsresamplingutils.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsresamplingutils.cpp
3 --------------------
4 begin : 12/06/2020
5 copyright : (C) 2020 Even Rouault
6 email : even.rouault at spatialys.com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgsresamplingutils.h"
19
23#include "qgsrasterlayer.h"
25#include "qgsrasterresampler.h"
26
27#include <QCheckBox>
28#include <QComboBox>
29#include <QDoubleSpinBox>
30#include <QObject>
31
32#include "moc_qgsresamplingutils.cpp"
33
35
36QgsResamplingUtils::QgsResamplingUtils() = default;
37
38void QgsResamplingUtils::initWidgets( QgsRasterLayer *rasterLayer, QComboBox *zoomedInResamplingComboBox, QComboBox *zoomedOutResamplingComboBox, QDoubleSpinBox *maximumOversamplingSpinBox, QCheckBox *cbEarlyResampling )
39{
40 mRasterLayer = rasterLayer;
41 mZoomedInResamplingComboBox = zoomedInResamplingComboBox;
42 mZoomedOutResamplingComboBox = zoomedOutResamplingComboBox;
43 mMaximumOversamplingSpinBox = maximumOversamplingSpinBox;
44 mCbEarlyResampling = cbEarlyResampling;
45
46 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
47 {
48 combo->addItem( QObject::tr( "Nearest Neighbour" ), static_cast<int>( Qgis::RasterResamplingMethod::Nearest ) );
49 combo->addItem( QObject::tr( "Bilinear (2x2 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Bilinear ) );
50 combo->addItem( QObject::tr( "Cubic (4x4 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Cubic ) );
51 }
52
53 if ( mCbEarlyResampling->isChecked() )
54 {
55 addExtraEarlyResamplingMethodsToCombos();
56 }
57
58 QObject::connect( mCbEarlyResampling, &QCheckBox::toggled, this, [this]( bool state ) {
59 if ( state )
60 addExtraEarlyResamplingMethodsToCombos();
61 else
62 removeExtraEarlyResamplingMethodsFromCombos();
63 } );
64}
65
66void QgsResamplingUtils::refreshWidgetsFromLayer()
67{
68 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
69 mCbEarlyResampling->setVisible(
71 );
72 mCbEarlyResampling->setChecked( mRasterLayer->resamplingStage() == Qgis::RasterResamplingStage::Provider );
73
74 switch ( mRasterLayer->resamplingStage() )
75 {
77 removeExtraEarlyResamplingMethodsFromCombos();
78 break;
80 addExtraEarlyResamplingMethodsToCombos();
81 break;
82 }
83
84 if ( provider && mRasterLayer->resamplingStage() == Qgis::RasterResamplingStage::Provider )
85 {
86 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( provider->zoomedInResamplingMethod() ) ) );
87 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( provider->zoomedOutResamplingMethod() ) ) );
88
89 mMaximumOversamplingSpinBox->setValue( provider->maxOversampling() );
90 }
91 else
92 {
93 const QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
94 //set combo boxes to current resampling types
95 if ( resampleFilter )
96 {
97 const QgsRasterResampler *zoomedInResampler = resampleFilter->zoomedInResampler();
98 if ( zoomedInResampler )
99 {
100 if ( zoomedInResampler->type() == QLatin1String( "bilinear" ) )
101 {
102 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Bilinear ) ) );
103 }
104 else if ( zoomedInResampler->type() == QLatin1String( "cubic" ) )
105 {
106 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Cubic ) ) );
107 }
108 }
109 else
110 {
111 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Nearest ) ) );
112 }
113
114 const QgsRasterResampler *zoomedOutResampler = resampleFilter->zoomedOutResampler();
115 if ( zoomedOutResampler )
116 {
117 if ( zoomedOutResampler->type() == QLatin1String( "bilinear" ) )
118 {
119 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Bilinear ) ) );
120 }
121 else if ( zoomedOutResampler->type() == QLatin1String( "cubic" ) )
122 {
123 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Cubic ) ) );
124 }
125 }
126 else
127 {
128 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Nearest ) ) );
129 }
130 mMaximumOversamplingSpinBox->setValue( resampleFilter->maxOversampling() );
131 }
132 }
133}
134
135
136void QgsResamplingUtils::refreshLayerFromWidgets()
137{
138 const Qgis::RasterResamplingMethod zoomedInMethod = static_cast<Qgis::RasterResamplingMethod>(
139 mZoomedInResamplingComboBox->itemData( mZoomedInResamplingComboBox->currentIndex() ).toInt()
140 );
141 const Qgis::RasterResamplingMethod zoomedOutMethod = static_cast<Qgis::RasterResamplingMethod>(
142 mZoomedOutResamplingComboBox->itemData( mZoomedOutResamplingComboBox->currentIndex() ).toInt()
143 );
144
145 mRasterLayer->setResamplingStage( mCbEarlyResampling->isChecked() ? Qgis::RasterResamplingStage::Provider : Qgis::RasterResamplingStage::ResampleFilter );
146 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
147 if ( provider )
148 {
149 provider->setZoomedInResamplingMethod( zoomedInMethod );
150 provider->setZoomedOutResamplingMethod( zoomedOutMethod );
151
152 provider->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
153 }
154
155 QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
156 if ( resampleFilter )
157 {
158 std::unique_ptr<QgsRasterResampler> zoomedInResampler;
159
160 // NOLINTBEGIN(bugprone-branch-clone)
161 switch ( zoomedInMethod )
162 {
164 break;
165
167 zoomedInResampler = std::make_unique<QgsBilinearRasterResampler>();
168 break;
169
171 zoomedInResampler = std::make_unique<QgsCubicRasterResampler>();
172 break;
173
179
180 // not supported as late resampler methods
181 break;
182 }
183 // NOLINTEND(bugprone-branch-clone)
184
185 resampleFilter->setZoomedInResampler( zoomedInResampler.release() );
186
187 //raster resampling
188 std::unique_ptr<QgsRasterResampler> zoomedOutResampler;
189
190 // NOLINTBEGIN(bugprone-branch-clone)
191 switch ( zoomedOutMethod )
192 {
194 break;
195
197 zoomedOutResampler = std::make_unique<QgsBilinearRasterResampler>();
198 break;
199
201 zoomedOutResampler = std::make_unique<QgsCubicRasterResampler>();
202 break;
203
204
210 // not supported as late resampler methods
211 break;
212 }
213 // NOLINTEND(bugprone-branch-clone)
214
215 resampleFilter->setZoomedOutResampler( zoomedOutResampler.release() );
216
217 resampleFilter->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
218 }
219}
220
221void QgsResamplingUtils::addExtraEarlyResamplingMethodsToCombos()
222{
223 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::CubicSpline ) ) != -1 )
224 return; // already present
225
226 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
227 {
228 combo->addItem( QObject::tr( "Cubic B-Spline (4x4 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::CubicSpline ) );
229 combo->addItem( QObject::tr( "Lanczos (6x6 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Lanczos ) );
230 combo->addItem( QObject::tr( "Average" ), static_cast<int>( Qgis::RasterResamplingMethod::Average ) );
231 combo->addItem( QObject::tr( "Mode" ), static_cast<int>( Qgis::RasterResamplingMethod::Mode ) );
232 combo->addItem( QObject::tr( "Gauss" ), static_cast<int>( Qgis::RasterResamplingMethod::Gauss ) );
233 }
234}
235
236void QgsResamplingUtils::removeExtraEarlyResamplingMethodsFromCombos()
237{
238 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::CubicSpline ) ) == -1 )
239 return; // already removed
240
241 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
242 {
243 for ( const Qgis::RasterResamplingMethod method :
244 {
250 } )
251 {
252 combo->removeItem( combo->findData( static_cast<int>( method ) ) );
253 }
254 }
255}
256
@ Provider
Resampling occurs in Provider.
Definition qgis.h:1472
@ ResampleFilter
Resampling occurs in ResamplingFilter.
Definition qgis.h:1471
@ ProviderHintCanPerformProviderResampling
Provider can perform resampling (to be opposed to post rendering resampling).
Definition qgis.h:4891
RasterResamplingMethod
Resampling method for raster provider-level resampling.
Definition qgis.h:1484
@ Lanczos
Lanczos windowed sinc interpolation (6x6 kernel).
Definition qgis.h:1489
@ Nearest
Nearest-neighbour resampling.
Definition qgis.h:1485
@ Mode
Mode (selects the value which appears most often of all the sampled points).
Definition qgis.h:1491
@ Bilinear
Bilinear (2x2 kernel) resampling.
Definition qgis.h:1486
@ Average
Average resampling.
Definition qgis.h:1490
@ CubicSpline
Cubic B-Spline Approximation (4x4 kernel).
Definition qgis.h:1488
@ Cubic
Cubic Convolution Approximation (4x4 kernel) resampling.
Definition qgis.h:1487
@ Gauss
Gauss blurring.
Definition qgis.h:1492
Base class for raster data providers.
Qgis::RasterResamplingMethod zoomedInResamplingMethod() const
Returns resampling method for zoomed-in operations.
virtual bool setZoomedInResamplingMethod(Qgis::RasterResamplingMethod method)
Set resampling method to apply for zoomed-in operations.
virtual bool setZoomedOutResamplingMethod(Qgis::RasterResamplingMethod method)
Set resampling method to apply for zoomed-out operations.
virtual bool setMaxOversampling(double factor)
Sets maximum oversampling factor for zoomed-out operations.
double maxOversampling() const
Returns maximum oversampling factor for zoomed-out operations.
Qgis::RasterResamplingMethod zoomedOutResamplingMethod() const
Returns resampling method for zoomed-out operations.
virtual Qgis::RasterProviderCapabilities providerCapabilities() const
Returns flags containing the supported capabilities of the data provider.
Represents a raster layer.
Resample filter pipe for rasters.
void setZoomedOutResampler(QgsRasterResampler *r)
Sets resampler for zoomed out scales. Takes ownership of the object.
const QgsRasterResampler * zoomedInResampler() const
const QgsRasterResampler * zoomedOutResampler() const
void setZoomedInResampler(QgsRasterResampler *r)
Sets resampler for zoomed in scales. Takes ownership of the object.
Interface for resampling rasters (e.g.
virtual QString type() const =0
Gets a descriptive type identifier for this raster resampler.