QGIS API Documentation 3.41.0-Master (3440c17df1d)
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 "qgsrasterlayer.h"
20#include "qgsrasterresampler.h"
22#include "qgsresamplingutils.h"
23#include "moc_qgsresamplingutils.cpp"
26
27#include <QObject>
28#include <QComboBox>
29#include <QDoubleSpinBox>
30#include <QCheckBox>
31
33
34QgsResamplingUtils::QgsResamplingUtils() = default;
35
36void QgsResamplingUtils::initWidgets( QgsRasterLayer *rasterLayer,
37 QComboBox *zoomedInResamplingComboBox,
38 QComboBox *zoomedOutResamplingComboBox,
39 QDoubleSpinBox *maximumOversamplingSpinBox,
40 QCheckBox *cbEarlyResampling )
41{
42 mRasterLayer = rasterLayer;
43 mZoomedInResamplingComboBox = zoomedInResamplingComboBox;
44 mZoomedOutResamplingComboBox = zoomedOutResamplingComboBox;
45 mMaximumOversamplingSpinBox = maximumOversamplingSpinBox;
46 mCbEarlyResampling = cbEarlyResampling;
47
48 for ( QComboBox *combo : {mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
49 {
50 combo->addItem( QObject::tr( "Nearest Neighbour" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Nearest ) );
51 combo->addItem( QObject::tr( "Bilinear (2x2 Kernel)" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Bilinear ) );
52 combo->addItem( QObject::tr( "Cubic (4x4 Kernel)" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Cubic ) );
53 }
54
55 if ( mCbEarlyResampling->isChecked() )
56 {
57 addExtraEarlyResamplingMethodsToCombos();
58 }
59
60 QObject::connect( mCbEarlyResampling, &QCheckBox::toggled, this, [ = ]( bool state )
61 {
62 if ( state )
63 addExtraEarlyResamplingMethodsToCombos();
64 else
65 removeExtraEarlyResamplingMethodsFromCombos();
66 } );
67}
68
69void QgsResamplingUtils::refreshWidgetsFromLayer()
70{
71 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
72 mCbEarlyResampling->setVisible(
74 mCbEarlyResampling->setChecked( mRasterLayer->resamplingStage() == Qgis::RasterResamplingStage::Provider );
75
76 switch ( mRasterLayer->resamplingStage() )
77 {
79 removeExtraEarlyResamplingMethodsFromCombos();
80 break;
82 addExtraEarlyResamplingMethodsToCombos();
83 break;
84 }
85
86 if ( provider && mRasterLayer->resamplingStage() == Qgis::RasterResamplingStage::Provider )
87 {
88 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( provider->zoomedInResamplingMethod() ) ) );
89 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( provider->zoomedOutResamplingMethod() ) ) );
90
91 mMaximumOversamplingSpinBox->setValue( provider->maxOversampling() );
92 }
93 else
94 {
95 const QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
96 //set combo boxes to current resampling types
97 if ( resampleFilter )
98 {
99 const QgsRasterResampler *zoomedInResampler = resampleFilter->zoomedInResampler();
100 if ( zoomedInResampler )
101 {
102 if ( zoomedInResampler->type() == QLatin1String( "bilinear" ) )
103 {
104 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Bilinear ) ) );
105 }
106 else if ( zoomedInResampler->type() == QLatin1String( "cubic" ) )
107 {
108 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Cubic ) ) );
109 }
110 }
111 else
112 {
113 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Nearest ) ) );
114 }
115
116 const QgsRasterResampler *zoomedOutResampler = resampleFilter->zoomedOutResampler();
117 if ( zoomedOutResampler )
118 {
119 if ( zoomedOutResampler->type() == QLatin1String( "bilinear" ) )
120 {
121 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Bilinear ) ) );
122 }
123 else if ( zoomedOutResampler->type() == QLatin1String( "cubic" ) )
124 {
125 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Cubic ) ) );
126 }
127 }
128 else
129 {
130 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Nearest ) ) );
131 }
132 mMaximumOversamplingSpinBox->setValue( resampleFilter->maxOversampling() );
133 }
134 }
135}
136
137
138void QgsResamplingUtils::refreshLayerFromWidgets()
139{
140 const QgsRasterDataProvider::ResamplingMethod zoomedInMethod =
142 mZoomedInResamplingComboBox->itemData( mZoomedInResamplingComboBox->currentIndex() ).toInt() );
143 const QgsRasterDataProvider::ResamplingMethod zoomedOutMethod =
145 mZoomedOutResamplingComboBox->itemData( mZoomedOutResamplingComboBox->currentIndex() ).toInt() );
146
147 mRasterLayer->setResamplingStage( mCbEarlyResampling->isChecked() ? Qgis::RasterResamplingStage::Provider : Qgis::RasterResamplingStage::ResampleFilter );
148 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
149 if ( provider )
150 {
151 provider->setZoomedInResamplingMethod( zoomedInMethod );
152 provider->setZoomedOutResamplingMethod( zoomedOutMethod );
153
154 provider->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
155 }
156
157 QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
158 if ( resampleFilter )
159 {
160 std::unique_ptr< QgsRasterResampler > zoomedInResampler;
161
162 switch ( zoomedInMethod )
163 {
165 break;
166
168 zoomedInResampler = std::make_unique< QgsBilinearRasterResampler >();
169 break;
170
172 zoomedInResampler = std::make_unique< QgsCubicRasterResampler >();
173 break;
174
180
181 // not supported as late resampler methods
182 break;
183 }
184
185 resampleFilter->setZoomedInResampler( zoomedInResampler.release() );
186
187 //raster resampling
188 std::unique_ptr< QgsRasterResampler > zoomedOutResampler;
189
190 switch ( zoomedOutMethod )
191 {
193 break;
194
196 zoomedOutResampler = std::make_unique< QgsBilinearRasterResampler >();
197 break;
198
200 zoomedOutResampler = std::make_unique< QgsCubicRasterResampler >();
201 break;
202
203
209 // not supported as late resampler methods
210 break;
211 }
212
213 resampleFilter->setZoomedOutResampler( zoomedOutResampler.release() );
214
215 resampleFilter->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
216 }
217}
218
219void QgsResamplingUtils::addExtraEarlyResamplingMethodsToCombos()
220{
221 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::CubicSpline ) ) != -1 )
222 return; // already present
223
224 for ( QComboBox *combo : {mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
225 {
226 combo->addItem( QObject::tr( "Cubic B-Spline (4x4 Kernel)" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::CubicSpline ) );
227 combo->addItem( QObject::tr( "Lanczos (6x6 Kernel)" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Lanczos ) );
228 combo->addItem( QObject::tr( "Average" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Average ) );
229 combo->addItem( QObject::tr( "Mode" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Mode ) );
230 combo->addItem( QObject::tr( "Gauss" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Gauss ) );
231 }
232}
233
234void QgsResamplingUtils::removeExtraEarlyResamplingMethodsFromCombos()
235{
236 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::CubicSpline ) ) == -1 )
237 return; // already removed
238
239 for ( QComboBox *combo : {mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
240 {
241 for ( const QgsRasterDataProvider::ResamplingMethod method :
242 {
248 } )
249 {
250 combo->removeItem( combo->findData( static_cast< int >( method ) ) );
251 }
252 }
253}
254
@ Provider
Resampling occurs in Provider.
@ ResampleFilter
Resampling occurs in ResamplingFilter.
@ ProviderHintCanPerformProviderResampling
Provider can perform resampling (to be opposed to post rendering resampling)
Base class for raster data providers.
ResamplingMethod zoomedOutResamplingMethod() const
Returns resampling method 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.
virtual Qgis::RasterProviderCapabilities providerCapabilities() const
Returns flags containing the supported capabilities of the data provider.
virtual bool setZoomedInResamplingMethod(ResamplingMethod method)
Set resampling method to apply for zoomed-in operations.
ResamplingMethod zoomedInResamplingMethod() const
Returns resampling method for zoomed-in operations.
ResamplingMethod
Resampling method for provider-level resampling.
@ Lanczos
Lanczos windowed sinc interpolation (6x6 kernel)
@ Nearest
Nearest-neighbour resampling.
@ Mode
Mode (selects the value which appears most often of all the sampled points)
@ Bilinear
Bilinear (2x2 kernel) resampling.
@ CubicSpline
Cubic B-Spline Approximation (4x4 kernel)
@ Cubic
Cubic Convolution Approximation (4x4 kernel) resampling.
virtual bool setZoomedOutResamplingMethod(ResamplingMethod method)
Set resampling method to apply for zoomed-out operations.
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.