Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FEATURE] Allow band selection for zonal stats plugin
  • Loading branch information
nyalldawson committed Apr 20, 2015
1 parent ee7b4d2 commit 5e41c43
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 8 deletions.
44 changes: 39 additions & 5 deletions src/plugins/zonal_statistics/qgszonalstatisticsdialog.cpp
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 ) );
}
}
12 changes: 10 additions & 2 deletions src/plugins/zonal_statistics/qgszonalstatisticsdialog.h
@@ -1,4 +1,4 @@
/***************************************************************************
/***************************************************************************
qgszonalstatisticsdialog.h - description
-----------------------
begin : September 1st, 2009
Expand All @@ -23,6 +23,7 @@

class QgisInterface;
class QgsVectorLayer;
class QgsRasterLayer;

class QgsZonalStatisticsDialog: public QDialog, private Ui::QgsZonalStatisticsDialogBase
{
Expand All @@ -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;

Expand All @@ -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
22 changes: 22 additions & 0 deletions src/plugins/zonal_statistics/qgszonalstatisticsdialogbase.ui
Expand Up @@ -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">
Expand Down Expand Up @@ -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>
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/zonal_statistics/qgszonalstatisticsplugin.cpp
Expand Up @@ -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 );
Expand Down

0 comments on commit 5e41c43

Please sign in to comment.