Skip to content

Commit 5e41c43

Browse files
committed
[FEATURE] Allow band selection for zonal stats plugin
1 parent ee7b4d2 commit 5e41c43

4 files changed

+72
-8
lines changed

src/plugins/zonal_statistics/qgszonalstatisticsdialog.cpp

+39-5
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void QgsZonalStatisticsDialog::insertAvailableLayers()
120120
QgsRasterDataProvider* rp = rl->dataProvider();
121121
if ( rp && rp->name() == "gdal" )
122122
{
123-
mRasterLayerComboBox->addItem( rl->name(), QVariant( rl->source() ) );
123+
mRasterLayerComboBox->addItem( rl->name(), QVariant( rl->id() ) );
124124
}
125125
}
126126
else
@@ -138,14 +138,27 @@ void QgsZonalStatisticsDialog::insertAvailableLayers()
138138
}
139139
}
140140

141-
QString QgsZonalStatisticsDialog::rasterFilePath() const
141+
QgsRasterLayer* QgsZonalStatisticsDialog::rasterLayer() const
142142
{
143143
int index = mRasterLayerComboBox->currentIndex();
144144
if ( index == -1 )
145145
{
146-
return "";
146+
return 0;
147147
}
148-
return mRasterLayerComboBox->itemData( index ).toString();
148+
QString id = mRasterLayerComboBox->itemData( index ).toString();
149+
QgsRasterLayer* layer = dynamic_cast<QgsRasterLayer*>( QgsMapLayerRegistry::instance()->mapLayer( id ) );
150+
return layer;
151+
}
152+
153+
QString QgsZonalStatisticsDialog::rasterFilePath() const
154+
{
155+
QgsRasterLayer* layer = rasterLayer();
156+
return layer ? layer->source() : QString();
157+
}
158+
159+
int QgsZonalStatisticsDialog::rasterBand() const
160+
{
161+
return mBandComboBox->currentIndex() + 1;
149162
}
150163

151164
QgsVectorLayer* QgsZonalStatisticsDialog::polygonLayer() const
@@ -171,7 +184,7 @@ QgsZonalStatistics::Statistics QgsZonalStatisticsDialog::selectedStats() const
171184
QListWidgetItem* item = mStatsListWidget->item( i );
172185
if ( item->checkState() == Qt::Checked )
173186
{
174-
stats |= ( QgsZonalStatistics::Statistic )( item->data( Qt::UserRole ).toInt() );
187+
stats |= ( QgsZonalStatistics::Statistic )item->data( Qt::UserRole ).toInt();
175188
}
176189
}
177190
return stats;
@@ -218,3 +231,24 @@ bool QgsZonalStatisticsDialog::prefixIsValid( const QString& prefix ) const
218231
}
219232
return true;
220233
}
234+
235+
void QgsZonalStatisticsDialog::on_mRasterLayerComboBox_currentIndexChanged( int index )
236+
{
237+
Q_UNUSED( index );
238+
239+
QgsRasterLayer* layer = rasterLayer();
240+
if ( !layer )
241+
{
242+
mBandComboBox->setEnabled( false );
243+
return;
244+
}
245+
246+
mBandComboBox->setEnabled( true );
247+
mBandComboBox->clear();
248+
249+
int bandCountInt = layer->bandCount();
250+
for ( int i = 1; i <= bandCountInt; ++i )
251+
{
252+
mBandComboBox->addItem( layer->bandName( i ) );
253+
}
254+
}

src/plugins/zonal_statistics/qgszonalstatisticsdialog.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/***************************************************************************
1+
/***************************************************************************
22
qgszonalstatisticsdialog.h - description
33
-----------------------
44
begin : September 1st, 2009
@@ -23,6 +23,7 @@
2323

2424
class QgisInterface;
2525
class QgsVectorLayer;
26+
class QgsRasterLayer;
2627

2728
class QgsZonalStatisticsDialog: public QDialog, private Ui::QgsZonalStatisticsDialogBase
2829
{
@@ -32,8 +33,10 @@ class QgsZonalStatisticsDialog: public QDialog, private Ui::QgsZonalStatisticsDi
3233
~QgsZonalStatisticsDialog();
3334

3435
QString rasterFilePath() const;
35-
int rasterBand() const {return 1;} //todo: expose that in the GUI
36+
int rasterBand() const;
3637
QgsVectorLayer* polygonLayer() const;
38+
QgsRasterLayer* rasterLayer() const;
39+
3740
QString attributePrefix() const;
3841
QgsZonalStatistics::Statistics selectedStats() const;
3942

@@ -47,6 +50,11 @@ class QgsZonalStatisticsDialog: public QDialog, private Ui::QgsZonalStatisticsDi
4750
bool prefixIsValid( const QString& prefix ) const;
4851

4952
QgisInterface* mIface;
53+
54+
private slots:
55+
56+
void on_mRasterLayerComboBox_currentIndexChanged( int index );
57+
5058
};
5159

5260
#endif // QGSZONALSTATISTICSDIALOG_H

src/plugins/zonal_statistics/qgszonalstatisticsdialogbase.ui

+22
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@
2424
<item>
2525
<widget class="QComboBox" name="mRasterLayerComboBox"/>
2626
</item>
27+
<item>
28+
<layout class="QHBoxLayout" name="horizontalLayout">
29+
<item>
30+
<widget class="QLabel" name="label_2">
31+
<property name="text">
32+
<string>Band</string>
33+
</property>
34+
</widget>
35+
</item>
36+
<item>
37+
<widget class="QComboBox" name="mBandComboBox"/>
38+
</item>
39+
</layout>
40+
</item>
2741
<item>
2842
<widget class="QLabel" name="mVectorLayerLabel">
2943
<property name="text">
@@ -70,6 +84,14 @@
7084
</item>
7185
</layout>
7286
</widget>
87+
<tabstops>
88+
<tabstop>mRasterLayerComboBox</tabstop>
89+
<tabstop>mBandComboBox</tabstop>
90+
<tabstop>mPolygonLayerComboBox</tabstop>
91+
<tabstop>mColumnPrefixLineEdit</tabstop>
92+
<tabstop>mStatsListWidget</tabstop>
93+
<tabstop>buttonBox</tabstop>
94+
</tabstops>
7395
<resources/>
7496
<connections>
7597
<connection>

src/plugins/zonal_statistics/qgszonalstatisticsplugin.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void QgsZonalStatisticsPlugin::run()
7272
return;
7373
}
7474

75-
QgsZonalStatistics zs( vl, rasterFile, d.attributePrefix(), 1, d.selectedStats() ); //atm hardcode first band
75+
QgsZonalStatistics zs( vl, rasterFile, d.attributePrefix(), d.rasterBand(), d.selectedStats() );
7676
QProgressDialog p( tr( "Calculating zonal statistics..." ), tr( "Abort..." ), 0, 0 );
7777
p.setWindowModality( Qt::WindowModal );
7878
zs.calculateStatistics( &p );

0 commit comments

Comments
 (0)