QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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#include <QString>
32
33#include "moc_qgsresamplingutils.cpp"
34
35using namespace Qt::StringLiterals;
36
38
39QgsResamplingUtils::QgsResamplingUtils() = default;
40
41void QgsResamplingUtils::initWidgets(
42 QgsRasterLayer *rasterLayer, QComboBox *zoomedInResamplingComboBox, QComboBox *zoomedOutResamplingComboBox, QDoubleSpinBox *maximumOversamplingSpinBox, QCheckBox *cbEarlyResampling
43)
44{
45 mRasterLayer = rasterLayer;
46 mZoomedInResamplingComboBox = zoomedInResamplingComboBox;
47 mZoomedOutResamplingComboBox = zoomedOutResamplingComboBox;
48 mMaximumOversamplingSpinBox = maximumOversamplingSpinBox;
49 mCbEarlyResampling = cbEarlyResampling;
50
51 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
52 {
53 combo->addItem( QObject::tr( "Nearest Neighbour" ), static_cast<int>( Qgis::RasterResamplingMethod::Nearest ) );
54 combo->addItem( QObject::tr( "Bilinear (2x2 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Bilinear ) );
55 combo->addItem( QObject::tr( "Cubic (4x4 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Cubic ) );
56 }
57
58 if ( mCbEarlyResampling->isChecked() )
59 {
60 addExtraEarlyResamplingMethodsToCombos();
61 }
62
63 QObject::connect( mCbEarlyResampling, &QCheckBox::toggled, this, [this]( bool state ) {
64 if ( state )
65 addExtraEarlyResamplingMethodsToCombos();
66 else
67 removeExtraEarlyResamplingMethodsFromCombos();
68 } );
69}
70
71void QgsResamplingUtils::refreshWidgetsFromLayer()
72{
73 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
74 mCbEarlyResampling->setVisible( provider && ( provider->providerCapabilities() & Qgis::RasterProviderCapability::ProviderHintCanPerformProviderResampling ) );
75 mCbEarlyResampling->setChecked( mRasterLayer->resamplingStage() == Qgis::RasterResamplingStage::Provider );
76
77 switch ( mRasterLayer->resamplingStage() )
78 {
80 removeExtraEarlyResamplingMethodsFromCombos();
81 break;
83 addExtraEarlyResamplingMethodsToCombos();
84 break;
85 }
86
87 if ( provider && mRasterLayer->resamplingStage() == Qgis::RasterResamplingStage::Provider )
88 {
89 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( provider->zoomedInResamplingMethod() ) ) );
90 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( provider->zoomedOutResamplingMethod() ) ) );
91
92 mMaximumOversamplingSpinBox->setValue( provider->maxOversampling() );
93 }
94 else
95 {
96 const QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
97 //set combo boxes to current resampling types
98 if ( resampleFilter )
99 {
100 const QgsRasterResampler *zoomedInResampler = resampleFilter->zoomedInResampler();
101 if ( zoomedInResampler )
102 {
103 if ( zoomedInResampler->type() == "bilinear"_L1 )
104 {
105 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Bilinear ) ) );
106 }
107 else if ( zoomedInResampler->type() == "cubic"_L1 )
108 {
109 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Cubic ) ) );
110 }
111 }
112 else
113 {
114 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Nearest ) ) );
115 }
116
117 const QgsRasterResampler *zoomedOutResampler = resampleFilter->zoomedOutResampler();
118 if ( zoomedOutResampler )
119 {
120 if ( zoomedOutResampler->type() == "bilinear"_L1 )
121 {
122 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Bilinear ) ) );
123 }
124 else if ( zoomedOutResampler->type() == "cubic"_L1 )
125 {
126 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Cubic ) ) );
127 }
128 }
129 else
130 {
131 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::Nearest ) ) );
132 }
133 mMaximumOversamplingSpinBox->setValue( resampleFilter->maxOversampling() );
134 }
135 }
136}
137
138
139void QgsResamplingUtils::refreshLayerFromWidgets()
140{
141 const Qgis::RasterResamplingMethod zoomedInMethod = static_cast<Qgis::RasterResamplingMethod>( mZoomedInResamplingComboBox->itemData( mZoomedInResamplingComboBox->currentIndex() ).toInt() );
142 const Qgis::RasterResamplingMethod zoomedOutMethod = static_cast<Qgis::RasterResamplingMethod>( mZoomedOutResamplingComboBox->itemData( mZoomedOutResamplingComboBox->currentIndex() ).toInt() );
143
144 mRasterLayer->setResamplingStage( mCbEarlyResampling->isChecked() ? Qgis::RasterResamplingStage::Provider : Qgis::RasterResamplingStage::ResampleFilter );
145 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
146 if ( provider )
147 {
148 provider->setZoomedInResamplingMethod( zoomedInMethod );
149 provider->setZoomedOutResamplingMethod( zoomedOutMethod );
150
151 provider->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
152 }
153
154 QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
155 if ( resampleFilter )
156 {
157 std::unique_ptr<QgsRasterResampler> zoomedInResampler;
158
159 // NOLINTBEGIN(bugprone-branch-clone)
160 switch ( zoomedInMethod )
161 {
163 break;
164
166 zoomedInResampler = std::make_unique<QgsBilinearRasterResampler>();
167 break;
168
170 zoomedInResampler = std::make_unique<QgsCubicRasterResampler>();
171 break;
172
178
179 // not supported as late resampler methods
180 break;
181 }
182 // NOLINTEND(bugprone-branch-clone)
183
184 resampleFilter->setZoomedInResampler( zoomedInResampler.release() );
185
186 //raster resampling
187 std::unique_ptr<QgsRasterResampler> zoomedOutResampler;
188
189 // NOLINTBEGIN(bugprone-branch-clone)
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 // NOLINTEND(bugprone-branch-clone)
213
214 resampleFilter->setZoomedOutResampler( zoomedOutResampler.release() );
215
216 resampleFilter->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
217 }
218}
219
220void QgsResamplingUtils::addExtraEarlyResamplingMethodsToCombos()
221{
222 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::CubicSpline ) ) != -1 )
223 return; // already present
224
225 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
226 {
227 combo->addItem( QObject::tr( "Cubic B-Spline (4x4 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::CubicSpline ) );
228 combo->addItem( QObject::tr( "Lanczos (6x6 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Lanczos ) );
229 combo->addItem( QObject::tr( "Average" ), static_cast<int>( Qgis::RasterResamplingMethod::Average ) );
230 combo->addItem( QObject::tr( "Mode" ), static_cast<int>( Qgis::RasterResamplingMethod::Mode ) );
231 combo->addItem( QObject::tr( "Gauss" ), static_cast<int>( Qgis::RasterResamplingMethod::Gauss ) );
232 }
233}
234
235void QgsResamplingUtils::removeExtraEarlyResamplingMethodsFromCombos()
236{
237 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::CubicSpline ) ) == -1 )
238 return; // already removed
239
240 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
241 {
242 for ( const Qgis::RasterResamplingMethod method :
244 {
245 combo->removeItem( combo->findData( static_cast<int>( method ) ) );
246 }
247 }
248}
249
@ Provider
Resampling occurs in Provider.
Definition qgis.h:1550
@ ResampleFilter
Resampling occurs in ResamplingFilter.
Definition qgis.h:1549
@ ProviderHintCanPerformProviderResampling
Provider can perform resampling (to be opposed to post rendering resampling).
Definition qgis.h:5048
RasterResamplingMethod
Resampling method for raster provider-level resampling.
Definition qgis.h:1562
@ Lanczos
Lanczos windowed sinc interpolation (6x6 kernel).
Definition qgis.h:1567
@ Nearest
Nearest-neighbour resampling.
Definition qgis.h:1563
@ Mode
Mode (selects the value which appears most often of all the sampled points).
Definition qgis.h:1569
@ Bilinear
Bilinear (2x2 kernel) resampling.
Definition qgis.h:1564
@ Average
Average resampling.
Definition qgis.h:1568
@ CubicSpline
Cubic B-Spline Approximation (4x4 kernel).
Definition qgis.h:1566
@ Cubic
Cubic Convolution Approximation (4x4 kernel) resampling.
Definition qgis.h:1565
@ Gauss
Gauss blurring.
Definition qgis.h:1570
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.