QGIS API Documentation 3.41.0-Master (57ec4277f5e)
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, QComboBox *zoomedInResamplingComboBox, QComboBox *zoomedOutResamplingComboBox, QDoubleSpinBox *maximumOversamplingSpinBox, QCheckBox *cbEarlyResampling )
37{
38 mRasterLayer = rasterLayer;
39 mZoomedInResamplingComboBox = zoomedInResamplingComboBox;
40 mZoomedOutResamplingComboBox = zoomedOutResamplingComboBox;
41 mMaximumOversamplingSpinBox = maximumOversamplingSpinBox;
42 mCbEarlyResampling = cbEarlyResampling;
43
44 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
45 {
46 combo->addItem( QObject::tr( "Nearest Neighbour" ), static_cast<int>( Qgis::RasterResamplingMethod::Nearest ) );
47 combo->addItem( QObject::tr( "Bilinear (2x2 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Bilinear ) );
48 combo->addItem( QObject::tr( "Cubic (4x4 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Cubic ) );
49 }
50
51 if ( mCbEarlyResampling->isChecked() )
52 {
53 addExtraEarlyResamplingMethodsToCombos();
54 }
55
56 QObject::connect( mCbEarlyResampling, &QCheckBox::toggled, this, [=]( bool state ) {
57 if ( state )
58 addExtraEarlyResamplingMethodsToCombos();
59 else
60 removeExtraEarlyResamplingMethodsFromCombos();
61 } );
62}
63
64void QgsResamplingUtils::refreshWidgetsFromLayer()
65{
66 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
67 mCbEarlyResampling->setVisible(
69 );
70 mCbEarlyResampling->setChecked( mRasterLayer->resamplingStage() == Qgis::RasterResamplingStage::Provider );
71
72 switch ( mRasterLayer->resamplingStage() )
73 {
75 removeExtraEarlyResamplingMethodsFromCombos();
76 break;
78 addExtraEarlyResamplingMethodsToCombos();
79 break;
80 }
81
82 if ( provider && mRasterLayer->resamplingStage() == Qgis::RasterResamplingStage::Provider )
83 {
84 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( provider->zoomedInResamplingMethod() ) ) );
85 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( provider->zoomedOutResamplingMethod() ) ) );
86
87 mMaximumOversamplingSpinBox->setValue( provider->maxOversampling() );
88 }
89 else
90 {
91 const QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
92 //set combo boxes to current resampling types
93 if ( resampleFilter )
94 {
95 const QgsRasterResampler *zoomedInResampler = resampleFilter->zoomedInResampler();
96 if ( zoomedInResampler )
97 {
98 if ( zoomedInResampler->type() == QLatin1String( "bilinear" ) )
99 {
100 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Bilinear ) ) );
101 }
102 else if ( zoomedInResampler->type() == QLatin1String( "cubic" ) )
103 {
104 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Cubic ) ) );
105 }
106 }
107 else
108 {
109 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Nearest ) ) );
110 }
111
112 const QgsRasterResampler *zoomedOutResampler = resampleFilter->zoomedOutResampler();
113 if ( zoomedOutResampler )
114 {
115 if ( zoomedOutResampler->type() == QLatin1String( "bilinear" ) )
116 {
117 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Bilinear ) ) );
118 }
119 else if ( zoomedOutResampler->type() == QLatin1String( "cubic" ) )
120 {
121 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Cubic ) ) );
122 }
123 }
124 else
125 {
126 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Nearest ) ) );
127 }
128 mMaximumOversamplingSpinBox->setValue( resampleFilter->maxOversampling() );
129 }
130 }
131}
132
133
134void QgsResamplingUtils::refreshLayerFromWidgets()
135{
136 const Qgis::RasterResamplingMethod zoomedInMethod = static_cast<Qgis::RasterResamplingMethod>(
137 mZoomedInResamplingComboBox->itemData( mZoomedInResamplingComboBox->currentIndex() ).toInt()
138 );
139 const Qgis::RasterResamplingMethod zoomedOutMethod = static_cast<Qgis::RasterResamplingMethod>(
140 mZoomedOutResamplingComboBox->itemData( mZoomedOutResamplingComboBox->currentIndex() ).toInt()
141 );
142
143 mRasterLayer->setResamplingStage( mCbEarlyResampling->isChecked() ? Qgis::RasterResamplingStage::Provider : Qgis::RasterResamplingStage::ResampleFilter );
144 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
145 if ( provider )
146 {
147 provider->setZoomedInResamplingMethod( zoomedInMethod );
148 provider->setZoomedOutResamplingMethod( zoomedOutMethod );
149
150 provider->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
151 }
152
153 QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
154 if ( resampleFilter )
155 {
156 std::unique_ptr<QgsRasterResampler> zoomedInResampler;
157
158 // NOLINTBEGIN(bugprone-branch-clone)
159 switch ( zoomedInMethod )
160 {
162 break;
163
165 zoomedInResampler = std::make_unique<QgsBilinearRasterResampler>();
166 break;
167
169 zoomedInResampler = std::make_unique<QgsCubicRasterResampler>();
170 break;
171
177
178 // not supported as late resampler methods
179 break;
180 }
181 // NOLINTEND(bugprone-branch-clone)
182
183 resampleFilter->setZoomedInResampler( zoomedInResampler.release() );
184
185 //raster resampling
186 std::unique_ptr<QgsRasterResampler> zoomedOutResampler;
187
188 // NOLINTBEGIN(bugprone-branch-clone)
189 switch ( zoomedOutMethod )
190 {
192 break;
193
195 zoomedOutResampler = std::make_unique<QgsBilinearRasterResampler>();
196 break;
197
199 zoomedOutResampler = std::make_unique<QgsCubicRasterResampler>();
200 break;
201
202
208 // not supported as late resampler methods
209 break;
210 }
211 // NOLINTEND(bugprone-branch-clone)
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>( Qgis::RasterResamplingMethod::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>( Qgis::RasterResamplingMethod::CubicSpline ) );
227 combo->addItem( QObject::tr( "Lanczos (6x6 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Lanczos ) );
228 combo->addItem( QObject::tr( "Average" ), static_cast<int>( Qgis::RasterResamplingMethod::Average ) );
229 combo->addItem( QObject::tr( "Mode" ), static_cast<int>( Qgis::RasterResamplingMethod::Mode ) );
230 combo->addItem( QObject::tr( "Gauss" ), static_cast<int>( Qgis::RasterResamplingMethod::Gauss ) );
231 }
232}
233
234void QgsResamplingUtils::removeExtraEarlyResamplingMethodsFromCombos()
235{
236 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::CubicSpline ) ) == -1 )
237 return; // already removed
238
239 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
240 {
241 for ( const Qgis::RasterResamplingMethod 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)
RasterResamplingMethod
Resampling method for raster provider-level resampling.
Definition qgis.h:1394
@ 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.
@ Average
Average resampling.
@ CubicSpline
Cubic B-Spline Approximation (4x4 kernel)
@ Cubic
Cubic Convolution Approximation (4x4 kernel) resampling.
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.