Skip to content
Permalink
Browse files

[FEATURE] Allow band selection for zonal stats plugin

  • Loading branch information
nyalldawson committed Apr 20, 2015
1 parent ee7b4d2 commit 5e41c432421ab867054a16c397d8c2252f2fc0aa
@@ -120,7 +120,7 @@ void QgsZonalStatisticsDialog::insertAvailableLayers()
QgsRasterDataProvider* rp = rl->dataProvider();
if ( rp && rp->name() == "gdal" )
{
mRasterLayerComboBox->addItem( rl->name(), QVariant( rl->source() ) );
mRasterLayerComboBox->addItem( rl->name(), QVariant( rl->id() ) );
}
}
else
@@ -138,14 +138,27 @@ void QgsZonalStatisticsDialog::insertAvailableLayers()
}
}

QString QgsZonalStatisticsDialog::rasterFilePath() const
QgsRasterLayer* QgsZonalStatisticsDialog::rasterLayer() const
{
int index = mRasterLayerComboBox->currentIndex();
if ( index == -1 )
{
return "";
return 0;
}
return mRasterLayerComboBox->itemData( index ).toString();
QString id = mRasterLayerComboBox->itemData( index ).toString();
QgsRasterLayer* layer = dynamic_cast<QgsRasterLayer*>( QgsMapLayerRegistry::instance()->mapLayer( id ) );
return layer;
}

QString QgsZonalStatisticsDialog::rasterFilePath() const
{
QgsRasterLayer* layer = rasterLayer();
return layer ? layer->source() : QString();
}

int QgsZonalStatisticsDialog::rasterBand() const
{
return mBandComboBox->currentIndex() + 1;
}

QgsVectorLayer* QgsZonalStatisticsDialog::polygonLayer() const
@@ -171,7 +184,7 @@ QgsZonalStatistics::Statistics QgsZonalStatisticsDialog::selectedStats() const
QListWidgetItem* item = mStatsListWidget->item( i );
if ( item->checkState() == Qt::Checked )
{
stats |= ( QgsZonalStatistics::Statistic )( item->data( Qt::UserRole ).toInt() );
stats |= ( QgsZonalStatistics::Statistic )item->data( Qt::UserRole ).toInt();
}
}
return stats;
@@ -218,3 +231,24 @@ bool QgsZonalStatisticsDialog::prefixIsValid( const QString& prefix ) const
}
return true;
}

void QgsZonalStatisticsDialog::on_mRasterLayerComboBox_currentIndexChanged( int index )
{
Q_UNUSED( index );

QgsRasterLayer* layer = rasterLayer();
if ( !layer )
{
mBandComboBox->setEnabled( false );
return;
}

mBandComboBox->setEnabled( true );
mBandComboBox->clear();

int bandCountInt = layer->bandCount();
for ( int i = 1; i <= bandCountInt; ++i )
{
mBandComboBox->addItem( layer->bandName( i ) );
}
}
@@ -1,4 +1,4 @@
/***************************************************************************
/***************************************************************************
qgszonalstatisticsdialog.h - description
-----------------------
begin : September 1st, 2009
@@ -23,6 +23,7 @@

class QgisInterface;
class QgsVectorLayer;
class QgsRasterLayer;

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

QString rasterFilePath() const;
int rasterBand() const {return 1;} //todo: expose that in the GUI
int rasterBand() const;
QgsVectorLayer* polygonLayer() const;
QgsRasterLayer* rasterLayer() const;

QString attributePrefix() const;
QgsZonalStatistics::Statistics selectedStats() const;

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

QgisInterface* mIface;

private slots:

void on_mRasterLayerComboBox_currentIndexChanged( int index );

};

#endif // QGSZONALSTATISTICSDIALOG_H
@@ -24,6 +24,20 @@
<item>
<widget class="QComboBox" name="mRasterLayerComboBox"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Band</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="mBandComboBox"/>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="mVectorLayerLabel">
<property name="text">
@@ -70,6 +84,14 @@
</item>
</layout>
</widget>
<tabstops>
<tabstop>mRasterLayerComboBox</tabstop>
<tabstop>mBandComboBox</tabstop>
<tabstop>mPolygonLayerComboBox</tabstop>
<tabstop>mColumnPrefixLineEdit</tabstop>
<tabstop>mStatsListWidget</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>
<connections>
<connection>
@@ -72,7 +72,7 @@ void QgsZonalStatisticsPlugin::run()
return;
}

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

0 comments on commit 5e41c43

Please sign in to comment.
You can’t perform that action at this time.