Skip to content

Commit a29d6d2

Browse files
committed
[raster calc] use active layer to setup dialog to harmonize
the behavior with that of processing
1 parent 50a103f commit a29d6d2

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

src/app/qgisapp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -5320,7 +5320,7 @@ void QgisApp::newGeoPackageLayer()
53205320

53215321
void QgisApp::showRasterCalculator()
53225322
{
5323-
QgsRasterCalcDialog d( this );
5323+
QgsRasterCalcDialog d( dynamic_cast<QgsRasterLayer *>( activeLayer() ), this );
53245324
if ( d.exec() == QDialog::Accepted )
53255325
{
53265326
//invoke analysis library

src/app/qgsrastercalcdialog.cpp

+21-17
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include <QFileDialog>
3030
#include <QFontDatabase>
3131

32-
QgsRasterCalcDialog::QgsRasterCalcDialog( QWidget *parent, Qt::WindowFlags f ): QDialog( parent, f )
32+
QgsRasterCalcDialog::QgsRasterCalcDialog( QgsRasterLayer *rasterLayer, QWidget *parent, Qt::WindowFlags f ): QDialog( parent, f )
3333
{
3434
setupUi( this );
3535
QgsGui::instance()->enableAutoGeometryRestore( this );
@@ -66,16 +66,16 @@ QgsRasterCalcDialog::QgsRasterCalcDialog( QWidget *parent, Qt::WindowFlags f ):
6666
connect( mOrButton, &QPushButton::clicked, this, &QgsRasterCalcDialog::mOrButton_clicked );
6767
connect( mButtonBox, &QDialogButtonBox::helpRequested, this, &QgsRasterCalcDialog::showHelp );
6868

69+
if ( rasterLayer && rasterLayer->dataProvider() && rasterLayer->dataProvider()->name() == QLatin1String( "gdal" ) )
70+
{
71+
setExtentSize( rasterLayer->width(), rasterLayer->height(), rasterLayer->extent() );
72+
mCrsSelector->setCrs( rasterLayer->crs() );
73+
}
74+
6975
//add supported output formats
7076
insertAvailableOutputFormats();
7177
insertAvailableRasterBands();
7278

73-
if ( !mAvailableRasterBands.isEmpty() )
74-
{
75-
//grab default crs from first raster
76-
mCrsSelector->setCrs( mAvailableRasterBands.at( 0 ).raster->crs() );
77-
}
78-
7979
mExpressionTextEdit->setCurrentFont( QFontDatabase::systemFont( QFontDatabase::FixedFont ) );
8080
}
8181

@@ -148,27 +148,31 @@ QVector<QgsRasterCalculatorEntry> QgsRasterCalcDialog::rasterEntries() const
148148
return entries;
149149
}
150150

151+
void QgsRasterCalcDialog::setExtentSize( int width, int height, QgsRectangle bbox )
152+
{
153+
mNColumnsSpinBox->setValue( width );
154+
mNRowsSpinBox->setValue( height );
155+
mXMinSpinBox->setValue( bbox.xMinimum() );
156+
mXMaxSpinBox->setValue( bbox.xMaximum() );
157+
mYMinSpinBox->setValue( bbox.yMinimum() );
158+
mYMaxSpinBox->setValue( bbox.yMaximum() );
159+
mExtentSizeSet = true;
160+
}
161+
151162
void QgsRasterCalcDialog::insertAvailableRasterBands()
152163
{
153164
const QMap<QString, QgsMapLayer *> &layers = QgsProject::instance()->mapLayers();
154165
QMap<QString, QgsMapLayer *>::const_iterator layerIt = layers.constBegin();
155166

156-
bool firstLayer = true;
157167
for ( ; layerIt != layers.constEnd(); ++layerIt )
158168
{
159169
QgsRasterLayer *rlayer = dynamic_cast<QgsRasterLayer *>( layerIt.value() );
160170
if ( rlayer && rlayer->dataProvider() && rlayer->dataProvider()->name() == QLatin1String( "gdal" ) )
161171
{
162-
if ( firstLayer ) //set bounding box / resolution of output to the values of the first possible input layer
172+
if ( !mExtentSizeSet ) //set bounding box / resolution of output to the values of the first possible input layer
163173
{
164-
mNColumnsSpinBox->setValue( rlayer->width() );
165-
mNRowsSpinBox->setValue( rlayer->height() );
166-
QgsRectangle bbox = rlayer->extent();
167-
mXMinSpinBox->setValue( bbox.xMinimum() );
168-
mXMaxSpinBox->setValue( bbox.xMaximum() );
169-
mYMinSpinBox->setValue( bbox.yMinimum() );
170-
mYMaxSpinBox->setValue( bbox.yMaximum() );
171-
firstLayer = false;
174+
setExtentSize( rlayer->width(), rlayer->height(), rlayer->extent() );
175+
mCrsSelector->setCrs( rlayer->crs() );
172176
}
173177
//get number of bands
174178
for ( int i = 0; i < rlayer->bandCount(); ++i )

src/app/qgsrastercalcdialog.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class APP_EXPORT QgsRasterCalcDialog: public QDialog, private Ui::QgsRasterCalcD
2828
{
2929
Q_OBJECT
3030
public:
31-
QgsRasterCalcDialog( QWidget *parent = nullptr, Qt::WindowFlags f = nullptr );
31+
QgsRasterCalcDialog( QgsRasterLayer *rasterLayer = nullptr, QWidget *parent = nullptr, Qt::WindowFlags f = nullptr );
3232

3333
QString formulaString() const;
3434
QString outputFile() const;
@@ -83,7 +83,10 @@ class APP_EXPORT QgsRasterCalcDialog: public QDialog, private Ui::QgsRasterCalcD
8383
void mOrButton_clicked();
8484

8585
private:
86-
//insert available GDAL drivers that support the create() option
86+
//! Sets the extent and size of the output
87+
void setExtentSize( int width, int height, QgsRectangle bbox );
88+
89+
// Insert available GDAL drivers that support the create() option
8790
void insertAvailableOutputFormats();
8891
//! Accesses the available raster layers/bands from the layer registry
8992
void insertAvailableRasterBands();
@@ -99,6 +102,8 @@ class APP_EXPORT QgsRasterCalcDialog: public QDialog, private Ui::QgsRasterCalcD
99102
QMap<QString, QString> mDriverExtensionMap;
100103

101104
QList<QgsRasterCalculatorEntry> mAvailableRasterBands;
105+
106+
bool mExtentSizeSet = false;
102107
};
103108

104109
#endif // QGSRASTERCALCDIALOG_H

0 commit comments

Comments
 (0)