QGIS API Documentation 3.99.0-Master (d270888f95f)
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( QgsRasterLayer *rasterLayer, QComboBox *zoomedInResamplingComboBox, QComboBox *zoomedOutResamplingComboBox, QDoubleSpinBox *maximumOversamplingSpinBox, QCheckBox *cbEarlyResampling )
42{
43 mRasterLayer = rasterLayer;
44 mZoomedInResamplingComboBox = zoomedInResamplingComboBox;
45 mZoomedOutResamplingComboBox = zoomedOutResamplingComboBox;
46 mMaximumOversamplingSpinBox = maximumOversamplingSpinBox;
47 mCbEarlyResampling = cbEarlyResampling;
48
49 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
50 {
51 combo->addItem( QObject::tr( "Nearest Neighbour" ), static_cast<int>( Qgis::RasterResamplingMethod::Nearest ) );
52 combo->addItem( QObject::tr( "Bilinear (2x2 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Bilinear ) );
53 combo->addItem( QObject::tr( "Cubic (4x4 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Cubic ) );
54 }
55
56 if ( mCbEarlyResampling->isChecked() )
57 {
58 addExtraEarlyResamplingMethodsToCombos();
59 }
60
61 QObject::connect( mCbEarlyResampling, &QCheckBox::toggled, this, [this]( bool state ) {
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 );
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>(
142 mZoomedInResamplingComboBox->itemData( mZoomedInResamplingComboBox->currentIndex() ).toInt()
143 );
144 const Qgis::RasterResamplingMethod zoomedOutMethod = static_cast<Qgis::RasterResamplingMethod>(
145 mZoomedOutResamplingComboBox->itemData( mZoomedOutResamplingComboBox->currentIndex() ).toInt()
146 );
147
148 mRasterLayer->setResamplingStage( mCbEarlyResampling->isChecked() ? Qgis::RasterResamplingStage::Provider : Qgis::RasterResamplingStage::ResampleFilter );
149 QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
150 if ( provider )
151 {
152 provider->setZoomedInResamplingMethod( zoomedInMethod );
153 provider->setZoomedOutResamplingMethod( zoomedOutMethod );
154
155 provider->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
156 }
157
158 QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
159 if ( resampleFilter )
160 {
161 std::unique_ptr<QgsRasterResampler> zoomedInResampler;
162
163 // NOLINTBEGIN(bugprone-branch-clone)
164 switch ( zoomedInMethod )
165 {
167 break;
168
170 zoomedInResampler = std::make_unique<QgsBilinearRasterResampler>();
171 break;
172
174 zoomedInResampler = std::make_unique<QgsCubicRasterResampler>();
175 break;
176
182
183 // not supported as late resampler methods
184 break;
185 }
186 // NOLINTEND(bugprone-branch-clone)
187
188 resampleFilter->setZoomedInResampler( zoomedInResampler.release() );
189
190 //raster resampling
191 std::unique_ptr<QgsRasterResampler> zoomedOutResampler;
192
193 // NOLINTBEGIN(bugprone-branch-clone)
194 switch ( zoomedOutMethod )
195 {
197 break;
198
200 zoomedOutResampler = std::make_unique<QgsBilinearRasterResampler>();
201 break;
202
204 zoomedOutResampler = std::make_unique<QgsCubicRasterResampler>();
205 break;
206
207
213 // not supported as late resampler methods
214 break;
215 }
216 // NOLINTEND(bugprone-branch-clone)
217
218 resampleFilter->setZoomedOutResampler( zoomedOutResampler.release() );
219
220 resampleFilter->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
221 }
222}
223
224void QgsResamplingUtils::addExtraEarlyResamplingMethodsToCombos()
225{
226 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::CubicSpline ) ) != -1 )
227 return; // already present
228
229 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
230 {
231 combo->addItem( QObject::tr( "Cubic B-Spline (4x4 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::CubicSpline ) );
232 combo->addItem( QObject::tr( "Lanczos (6x6 Kernel)" ), static_cast<int>( Qgis::RasterResamplingMethod::Lanczos ) );
233 combo->addItem( QObject::tr( "Average" ), static_cast<int>( Qgis::RasterResamplingMethod::Average ) );
234 combo->addItem( QObject::tr( "Mode" ), static_cast<int>( Qgis::RasterResamplingMethod::Mode ) );
235 combo->addItem( QObject::tr( "Gauss" ), static_cast<int>( Qgis::RasterResamplingMethod::Gauss ) );
236 }
237}
238
239void QgsResamplingUtils::removeExtraEarlyResamplingMethodsFromCombos()
240{
241 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( Qgis::RasterResamplingMethod::CubicSpline ) ) == -1 )
242 return; // already removed
243
244 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
245 {
246 for ( const Qgis::RasterResamplingMethod method :
247 {
253 } )
254 {
255 combo->removeItem( combo->findData( static_cast<int>( method ) ) );
256 }
257 }
258}
259
@ Provider
Resampling occurs in Provider.
Definition qgis.h:1530
@ ResampleFilter
Resampling occurs in ResamplingFilter.
Definition qgis.h:1529
@ ProviderHintCanPerformProviderResampling
Provider can perform resampling (to be opposed to post rendering resampling).
Definition qgis.h:4963
RasterResamplingMethod
Resampling method for raster provider-level resampling.
Definition qgis.h:1542
@ Lanczos
Lanczos windowed sinc interpolation (6x6 kernel).
Definition qgis.h:1547
@ Nearest
Nearest-neighbour resampling.
Definition qgis.h:1543
@ Mode
Mode (selects the value which appears most often of all the sampled points).
Definition qgis.h:1549
@ Bilinear
Bilinear (2x2 kernel) resampling.
Definition qgis.h:1544
@ Average
Average resampling.
Definition qgis.h:1548
@ CubicSpline
Cubic B-Spline Approximation (4x4 kernel).
Definition qgis.h:1546
@ Cubic
Cubic Convolution Approximation (4x4 kernel) resampling.
Definition qgis.h:1545
@ Gauss
Gauss blurring.
Definition qgis.h:1550
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.