QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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"
19 #include "qgsrasterdataprovider.h"
20 #include "qgsrasterresampler.h"
22 #include "qgsresamplingutils.h"
25 
26 #include <QObject>
27 #include <QComboBox>
28 #include <QDoubleSpinBox>
29 #include <QCheckBox>
30 
31 QgsResamplingUtils::QgsResamplingUtils() = default;
32 
33 void QgsResamplingUtils::initWidgets( QgsRasterLayer *rasterLayer,
34  QComboBox *zoomedInResamplingComboBox,
35  QComboBox *zoomedOutResamplingComboBox,
36  QDoubleSpinBox *maximumOversamplingSpinBox,
37  QCheckBox *cbEarlyResampling )
38 {
39  mRasterLayer = rasterLayer;
40  mZoomedInResamplingComboBox = zoomedInResamplingComboBox;
41  mZoomedOutResamplingComboBox = zoomedOutResamplingComboBox;
42  mMaximumOversamplingSpinBox = maximumOversamplingSpinBox;
43  mCbEarlyResampling = cbEarlyResampling;
44 
45  mZoomedInResamplingComboBox->addItem( QObject::tr( "Nearest neighbour" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Nearest ) );
46  mZoomedInResamplingComboBox->addItem( QObject::tr( "Bilinear" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Bilinear ) );
47  mZoomedInResamplingComboBox->addItem( QObject::tr( "Cubic" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Cubic ) );
48 
49  mZoomedOutResamplingComboBox->addItem( QObject::tr( "Nearest neighbour" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Nearest ) );
50  mZoomedOutResamplingComboBox->addItem( QObject::tr( "Bilinear" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Bilinear ) );
51  mZoomedOutResamplingComboBox->addItem( QObject::tr( "Cubic" ), static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Cubic ) );
52 }
53 
54 void QgsResamplingUtils::refreshWidgetsFromLayer()
55 {
56  QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
57  mCbEarlyResampling->setVisible(
59  mCbEarlyResampling->setChecked( mRasterLayer->resamplingStage() == QgsRasterPipe::ResamplingStage::Provider );
60 
61  if ( provider && mRasterLayer->resamplingStage() == QgsRasterPipe::ResamplingStage::Provider )
62  {
63  mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( provider->zoomedInResamplingMethod() ) ) );
64  mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( provider->zoomedOutResamplingMethod() ) ) );
65 
66  mMaximumOversamplingSpinBox->setValue( provider->maxOversampling() );
67  }
68  else
69  {
70  const QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
71  //set combo boxes to current resampling types
72  if ( resampleFilter )
73  {
74  const QgsRasterResampler *zoomedInResampler = resampleFilter->zoomedInResampler();
75  if ( zoomedInResampler )
76  {
77  if ( zoomedInResampler->type() == QLatin1String( "bilinear" ) )
78  {
79  mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Bilinear ) ) );
80  }
81  else if ( zoomedInResampler->type() == QLatin1String( "cubic" ) )
82  {
83  mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Cubic ) ) );
84  }
85  }
86  else
87  {
88  mZoomedInResamplingComboBox->setCurrentIndex( mZoomedInResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Nearest ) ) );
89  }
90 
91  const QgsRasterResampler *zoomedOutResampler = resampleFilter->zoomedOutResampler();
92  if ( zoomedOutResampler )
93  {
94  if ( zoomedOutResampler->type() == QLatin1String( "bilinear" ) )
95  {
96  mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Bilinear ) ) );
97  }
98  else if ( zoomedOutResampler->type() == QLatin1String( "cubic" ) )
99  {
100  mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Cubic ) ) );
101  }
102  }
103  else
104  {
105  mZoomedOutResamplingComboBox->setCurrentIndex( mZoomedOutResamplingComboBox->findData( static_cast<int>( QgsRasterDataProvider::ResamplingMethod::Nearest ) ) );
106  }
107  mMaximumOversamplingSpinBox->setValue( resampleFilter->maxOversampling() );
108  }
109  }
110 }
111 
112 
113 void QgsResamplingUtils::refreshLayerFromWidgets()
114 {
117  mZoomedInResamplingComboBox->itemData( mZoomedInResamplingComboBox->currentIndex() ).toInt() );
120  mZoomedOutResamplingComboBox->itemData( mZoomedOutResamplingComboBox->currentIndex() ).toInt() );
121 
122  mRasterLayer->setResamplingStage( mCbEarlyResampling->isChecked() ? QgsRasterPipe::ResamplingStage::Provider : QgsRasterPipe::ResamplingStage::ResampleFilter );
123  QgsRasterDataProvider *provider = mRasterLayer->dataProvider();
124  if ( provider )
125  {
126  provider->setZoomedInResamplingMethod( zoomedInMethod );
127  provider->setZoomedOutResamplingMethod( zoomedOutMethod );
128 
129  provider->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
130  }
131 
132  QgsRasterResampleFilter *resampleFilter = mRasterLayer->resampleFilter();
133  if ( resampleFilter )
134  {
135  QgsRasterResampler *zoomedInResampler = nullptr;
136 
137  switch ( zoomedInMethod )
138  {
140  break;
141 
143  zoomedInResampler = new QgsBilinearRasterResampler();
144  break;
145 
147  zoomedInResampler = new QgsCubicRasterResampler();
148  break;
149  }
150 
151  resampleFilter->setZoomedInResampler( zoomedInResampler );
152 
153  //raster resampling
154  QgsRasterResampler *zoomedOutResampler = nullptr;
155 
156  switch ( zoomedOutMethod )
157  {
159  break;
160 
162  zoomedOutResampler = new QgsBilinearRasterResampler();
163  break;
164 
166  zoomedOutResampler = new QgsCubicRasterResampler();
167  break;
168  }
169 
170  resampleFilter->setZoomedOutResampler( zoomedOutResampler );
171 
172  resampleFilter->setMaxOversampling( mMaximumOversamplingSpinBox->value() );
173  }
174 }
QgsRasterDataProvider::zoomedInResamplingMethod
ResamplingMethod zoomedInResamplingMethod() const
Returns resampling method for zoomed-in operations.
Definition: qgsrasterdataprovider.h:633
qgsrasterlayer.h
qgsbilinearrasterresampler.h
QgsRasterPipe::ResamplingStage::ResampleFilter
@ ResampleFilter
Resampling occurs in ResamplingFilter.
QgsRasterDataProvider::ResamplingMethod::Nearest
@ Nearest
Nearest-neighbour resamplikng.
QgsRasterDataProvider::ResamplingMethod::Cubic
@ Cubic
Bicubic resamplikng.
QgsRasterDataProvider::ProviderHintCanPerformProviderResampling
@ ProviderHintCanPerformProviderResampling
Provider can perform resampling (to be opposed to post rendering resampling) (since QGIS 3....
Definition: qgsrasterdataprovider.h:104
QgsRasterResampler
Interface for resampling rasters (e.g.
Definition: qgsrasterresampler.h:34
qgsrasterresampler.h
qgscubicrasterresampler.h
QgsRasterDataProvider::ResamplingMethod::Bilinear
@ Bilinear
Bilinear resamplikng.
QgsBilinearRasterResampler
Bilinear Raster Resampler.
Definition: qgsbilinearrasterresampler.h:34
QgsRasterDataProvider::setZoomedInResamplingMethod
virtual bool setZoomedInResamplingMethod(ResamplingMethod method)
Set resampling method to apply for zoomed-in operations.
Definition: qgsrasterdataprovider.h:627
QgsRasterDataProvider::ResamplingMethod
ResamplingMethod
Resampling method for provider-level resampling.
Definition: qgsrasterdataprovider.h:615
QgsRasterDataProvider::providerCapabilities
virtual QgsRasterDataProvider::ProviderCapabilities providerCapabilities() const
Returns flags containing the supported capabilities of the data provider.
Definition: qgsrasterdataprovider.cpp:245
QgsCubicRasterResampler
Cubic Raster Resampler.
Definition: qgscubicrasterresampler.h:33
QgsRasterResampleFilter::setZoomedInResampler
void setZoomedInResampler(QgsRasterResampler *r)
Sets resampler for zoomed in scales. Takes ownership of the object.
Definition: qgsrasterresamplefilter.cpp:111
QgsRasterLayer
Represents a raster layer.
Definition: qgsrasterlayer.h:71
qgsrasterresamplefilter.h
QgsRasterDataProvider::setMaxOversampling
virtual bool setMaxOversampling(double factor)
Sets maximum oversampling factor for zoomed-out operations.
Definition: qgsrasterdataprovider.h:655
QgsRasterDataProvider::maxOversampling
double maxOversampling() const
Returns maximum oversampling factor for zoomed-out operations.
Definition: qgsrasterdataprovider.h:661
QgsRasterResampleFilter::maxOversampling
double maxOversampling() const
Definition: qgsrasterresamplefilter.h:56
QgsRasterResampler::type
virtual QString type() const =0
Gets a descriptive type identifier for this raster resampler.
QgsRasterResampleFilter
Resample filter pipe for rasters.
Definition: qgsrasterresamplefilter.h:33
QgsRasterResampleFilter::zoomedInResampler
const QgsRasterResampler * zoomedInResampler() const
Definition: qgsrasterresamplefilter.h:49
QgsRasterDataProvider::setZoomedOutResamplingMethod
virtual bool setZoomedOutResamplingMethod(ResamplingMethod method)
Set resampling method to apply for zoomed-out operations.
Definition: qgsrasterdataprovider.h:641
qgsresamplingutils.h
QgsRasterResampleFilter::zoomedOutResampler
const QgsRasterResampler * zoomedOutResampler() const
Definition: qgsrasterresamplefilter.h:53
QgsRasterResampleFilter::setMaxOversampling
void setMaxOversampling(double os)
Definition: qgsrasterresamplefilter.h:55
QgsRasterDataProvider::zoomedOutResamplingMethod
ResamplingMethod zoomedOutResamplingMethod() const
Returns resampling method for zoomed-out operations.
Definition: qgsrasterdataprovider.h:647
QgsRasterDataProvider
Base class for raster data providers.
Definition: qgsrasterdataprovider.h:89
QgsRasterResampleFilter::setZoomedOutResampler
void setZoomedOutResampler(QgsRasterResampler *r)
Sets resampler for zoomed out scales. Takes ownership of the object.
Definition: qgsrasterresamplefilter.cpp:116
QgsRasterPipe::ResamplingStage::Provider
@ Provider
Resampling occurs in Provider.
qgsrasterdataprovider.h