QGIS API Documentation 3.41.0-Master (cea29feecf2)
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>( QgsRasterDataProvider::ResamplingMethod::Nearest ) );
47 combo->addItem( QObject::tr( "Bilinear (2x2 Kernel)" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Bilinear ) );
48 combo->addItem( QObject::tr( "Cubic (4x4 Kernel)" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::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>( QgsRasterDataProvider::ResamplingMethod::Bilinear ) ) );
101 }
102 else if ( zoomedInResampler->type() == QLatin1String( "cubic" ) )
103 {
104 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Cubic ) ) );
105 }
106 }
107 else
108 {
109 mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::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>( QgsRasterDataProvider::ResamplingMethod::Bilinear ) ) );
118 }
119 else if ( zoomedOutResampler->type() == QLatin1String( "cubic" ) )
120 {
121 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Cubic ) ) );
122 }
123 }
124 else
125 {
126 mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Nearest ) ) );
127 }
128 mMaximumOversamplingSpinBox->setValue( resampleFilter->maxOversampling() );
129 }
130 }
131}
132
133
134void QgsResamplingUtils::refreshLayerFromWidgets()
135{
137 mZoomedInResamplingComboBox->itemData( mZoomedInResamplingComboBox->currentIndex() ).toInt()
138 );
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 switch ( zoomedInMethod )
159 {
161 break;
162
164 zoomedInResampler = std::make_unique<QgsBilinearRasterResampler>();
165 break;
166
168 zoomedInResampler = std::make_unique<QgsCubicRasterResampler>();
169 break;
170
176
177 // not supported as late resampler methods
178 break;
179 }
180
181 resampleFilter->setZoomedInResampler( zoomedInResampler.release() );
182
183 //raster resampling
184 std::unique_ptr<QgsRasterResampler> zoomedOutResampler;
185
186 switch ( zoomedOutMethod )
187 {
189 break;
190
192 zoomedOutResampler = std::make_unique<QgsBilinearRasterResampler>();
193 break;
194
196 zoomedOutResampler = std::make_unique<QgsCubicRasterResampler>();
197 break;
198
199
205 // not supported as late resampler methods
206 break;
207 }
208
209 resampleFilter->setZoomedOutResampler( zoomedOutResampler.release() );
210
211 resampleFilter->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
212 }
213}
214
215void QgsResamplingUtils::addExtraEarlyResamplingMethodsToCombos()
216{
217 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::CubicSpline ) ) != -1 )
218 return; // already present
219
220 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
221 {
222 combo->addItem( QObject::tr( "Cubic B-Spline (4x4 Kernel)" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::CubicSpline ) );
223 combo->addItem( QObject::tr( "Lanczos (6x6 Kernel)" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Lanczos ) );
224 combo->addItem( QObject::tr( "Average" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Average ) );
225 combo->addItem( QObject::tr( "Mode" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Mode ) );
226 combo->addItem( QObject::tr( "Gauss" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Gauss ) );
227 }
228}
229
230void QgsResamplingUtils::removeExtraEarlyResamplingMethodsFromCombos()
231{
232 if ( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::CubicSpline ) ) == -1 )
233 return; // already removed
234
235 for ( QComboBox *combo : { mZoomedInResamplingComboBox, mZoomedOutResamplingComboBox } )
236 {
237 for ( const QgsRasterDataProvider::ResamplingMethod method :
238 {
244 } )
245 {
246 combo->removeItem( combo->findData( static_cast<int>( method ) ) );
247 }
248 }
249}
250
@ 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.