Skip to content

Commit cc7e63b

Browse files
committedJun 10, 2011
[FEATURE]: zonal statistics plugin
1 parent 80595cd commit cc7e63b

File tree

7 files changed

+487
-0
lines changed

7 files changed

+487
-0
lines changed
 

‎src/plugins/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ ADD_SUBDIRECTORY(point_displacement_renderer)
1313
ADD_SUBDIRECTORY(spatialquery)
1414
ADD_SUBDIRECTORY(sqlanywhere)
1515
ADD_SUBDIRECTORY(roadgraph)
16+
ADD_SUBDIRECTORY(zonal_statistics)
1617

1718
IF (WITH_SPATIALITE)
1819
ADD_SUBDIRECTORY(offline_editing)
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
########################################################
2+
# Files
3+
4+
SET (ZONAL_STATISTICS_SRCS
5+
qgszonalstatisticsplugin.cpp
6+
qgszonalstatisticsdialog.cpp
7+
)
8+
9+
SET (ZONAL_STATISTICS_UIS
10+
qgszonalstatisticsdialogbase.ui
11+
)
12+
13+
SET (ZONAL_STATISTICS_MOC_HDRS
14+
qgszonalstatisticsdialog.h
15+
qgszonalstatisticsplugin.h
16+
)
17+
18+
########################################################
19+
# Build
20+
21+
QT4_WRAP_UI (ZONAL_STATISTICS_UIS_H ${ZONAL_STATISTICS_UIS})
22+
23+
QT4_WRAP_CPP (ZONAL_STATISTICS_MOC_SRCS ${ZONAL_STATISTICS_MOC_HDRS})
24+
25+
QT4_ADD_RESOURCES(ZONAL_STATISTICS_RCC_SRCS ${ZONAL_STATISTICS_RCCS})
26+
27+
ADD_LIBRARY (zonalstatisticsplugin MODULE
28+
${ZONAL_STATISTICS_SRCS}
29+
${ZONAL_STATISTICS_MOC_SRCS}
30+
${ZONAL_STATISTICS_RCC_SRCS}
31+
${ZONAL_STATISTICS_UIS_H})
32+
33+
INCLUDE_DIRECTORIES(
34+
${CMAKE_CURRENT_BINARY_DIR}
35+
${GDAL_INCLUDE_DIR}
36+
../../core
37+
../../core/raster
38+
../../gui
39+
../../analysis/vector
40+
..
41+
.
42+
)
43+
44+
TARGET_LINK_LIBRARIES(zonalstatisticsplugin
45+
qgis_analysis
46+
qgis_core
47+
qgis_gui
48+
)
49+
50+
51+
########################################################
52+
# Install
53+
54+
INSTALL(TARGETS zonalstatisticsplugin
55+
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
56+
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR}
57+
)
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/***************************************************************************
2+
qgszonalstatisticsdialog.h - description
3+
-----------------------
4+
begin : September 1st, 2009
5+
copyright : (C) 2009 by Marco Hugentobler
6+
email : marco dot hugentobler at karto dot baug dot ethz dot ch
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 "qgszonalstatisticsdialog.h"
19+
#include "qgsmaplayerregistry.h"
20+
#include "qgsrasterlayer.h"
21+
#include "qgsvectordataprovider.h"
22+
#include "qgsvectorlayer.h"
23+
24+
QgsZonalStatisticsDialog::QgsZonalStatisticsDialog(QgisInterface* iface): QDialog(), mIface(iface)
25+
{
26+
setupUi(this);
27+
insertAvailableLayers();
28+
mColumnPrefixLineEdit->setText(proposeAttributePrefix());
29+
}
30+
31+
QgsZonalStatisticsDialog::QgsZonalStatisticsDialog(): QDialog(0), mIface(0)
32+
{
33+
setupUi(this);
34+
}
35+
36+
QgsZonalStatisticsDialog::~QgsZonalStatisticsDialog()
37+
{
38+
39+
}
40+
41+
void QgsZonalStatisticsDialog::insertAvailableLayers()
42+
{
43+
//insert available raster layers
44+
//enter available layers into the combo box
45+
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
46+
QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin();
47+
48+
for ( ; layer_it != mapLayers.end(); ++layer_it )
49+
{
50+
QgsRasterLayer* rl = dynamic_cast<QgsRasterLayer*>( layer_it.value() );
51+
if ( rl )
52+
{
53+
mRasterLayerComboBox->addItem( rl->name(), QVariant( rl->source() ) );
54+
}
55+
else
56+
{
57+
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer_it.value() );
58+
if(vl && vl->geometryType() == QGis::Polygon)
59+
{
60+
mPolygonLayerComboBox->addItem( vl->name(), QVariant( vl->getLayerID() ) );
61+
}
62+
}
63+
}
64+
}
65+
66+
QString QgsZonalStatisticsDialog::rasterFilePath() const
67+
{
68+
int index = mRasterLayerComboBox->currentIndex();
69+
if ( index == -1 )
70+
{
71+
return "";
72+
}
73+
return mRasterLayerComboBox->itemData( index ).toString();
74+
}
75+
76+
QgsVectorLayer* QgsZonalStatisticsDialog::polygonLayer() const
77+
{
78+
int index = mPolygonLayerComboBox->currentIndex();
79+
if(index == -1)
80+
{
81+
return 0;
82+
}
83+
return dynamic_cast<QgsVectorLayer*>(QgsMapLayerRegistry::instance()->mapLayer(mPolygonLayerComboBox->itemData( index ).toString()));
84+
}
85+
86+
QString QgsZonalStatisticsDialog::attributePrefix() const
87+
{
88+
return mColumnPrefixLineEdit->text();
89+
}
90+
91+
QString QgsZonalStatisticsDialog::proposeAttributePrefix() const
92+
{
93+
if(!polygonLayer())
94+
{
95+
return "";
96+
}
97+
98+
QString proposedPrefix = "";
99+
while(!prefixIsValid(proposedPrefix))
100+
{
101+
proposedPrefix.prepend("_");
102+
}
103+
return proposedPrefix;
104+
}
105+
106+
bool QgsZonalStatisticsDialog::prefixIsValid(const QString& prefix) const
107+
{
108+
QgsVectorLayer* vl = polygonLayer();
109+
if(!vl)
110+
{
111+
return false;
112+
}
113+
QgsVectorDataProvider* dp = vl->dataProvider();
114+
if(!dp)
115+
{
116+
return false;
117+
}
118+
119+
QgsFieldMap providerFieldMap = dp->fields();
120+
QgsFieldMap::const_iterator it = providerFieldMap.constBegin();
121+
QString currentFieldName;
122+
123+
for(; it != providerFieldMap.constEnd(); ++it)
124+
{
125+
currentFieldName = it.value().name();
126+
if(currentFieldName == (prefix + "mean") || currentFieldName == (prefix + "sum") || currentFieldName == (prefix + "count") )
127+
{
128+
return false;
129+
}
130+
}
131+
return true;
132+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/***************************************************************************
2+
qgszonalstatisticsdialog.h - description
3+
-----------------------
4+
begin : September 1st, 2009
5+
copyright : (C) 2009 by Marco Hugentobler
6+
email : marco dot hugentobler at karto dot baug dot ethz dot ch
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+
#ifndef QGSZONALSTATISTICSDIALOG_H
19+
#define QGSZONALSTATISTICSDIALOG_H
20+
21+
#include "ui_qgszonalstatisticsdialogbase.h"
22+
23+
class QgisInterface;
24+
class QgsVectorLayer;
25+
26+
class QgsZonalStatisticsDialog: public QDialog, private Ui::QgsZonalStatisticsDialogBase
27+
{
28+
Q_OBJECT
29+
public:
30+
QgsZonalStatisticsDialog(QgisInterface* iface);
31+
~QgsZonalStatisticsDialog();
32+
33+
QString rasterFilePath() const;
34+
int rasterBand() const {return 1;} //todo: expose that in the GUI
35+
QgsVectorLayer* polygonLayer() const;
36+
QString attributePrefix() const;
37+
38+
private:
39+
QgsZonalStatisticsDialog();
40+
/**Fills the available raster and polygon layers into the combo boxes*/
41+
void insertAvailableLayers();
42+
/**Propose a valid prefix for the attributes*/
43+
QString proposeAttributePrefix() const;
44+
/**Check if a prefix can be used for the count, sum and mean attribute*/
45+
bool prefixIsValid(const QString& prefix) const;
46+
47+
QgisInterface* mIface;
48+
};
49+
50+
#endif // QGSZONALSTATISTICSDIALOG_H
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<ui version="4.0" >
2+
<class>QgsZonalStatisticsDialogBase</class>
3+
<widget class="QDialog" name="QgsZonalStatisticsDialogBase" >
4+
<property name="geometry" >
5+
<rect>
6+
<x>0</x>
7+
<y>0</y>
8+
<width>366</width>
9+
<height>242</height>
10+
</rect>
11+
</property>
12+
<property name="windowTitle" >
13+
<string>Dialog</string>
14+
</property>
15+
<layout class="QGridLayout" name="gridLayout" >
16+
<item row="0" column="0" >
17+
<widget class="QLabel" name="mRasterLayerLabel" >
18+
<property name="text" >
19+
<string>Raster layer:</string>
20+
</property>
21+
</widget>
22+
</item>
23+
<item row="1" column="0" >
24+
<widget class="QComboBox" name="mRasterLayerComboBox" />
25+
</item>
26+
<item row="2" column="0" >
27+
<widget class="QLabel" name="mVectorLayerLabel" >
28+
<property name="text" >
29+
<string>Polygon layer containing the zones:</string>
30+
</property>
31+
</widget>
32+
</item>
33+
<item row="3" column="0" >
34+
<widget class="QComboBox" name="mPolygonLayerComboBox" />
35+
</item>
36+
<item row="4" column="0" >
37+
<widget class="QLabel" name="mColumnPrefixLabel" >
38+
<property name="text" >
39+
<string>Output column prefix:</string>
40+
</property>
41+
</widget>
42+
</item>
43+
<item row="5" column="0" >
44+
<widget class="QLineEdit" name="mColumnPrefixLineEdit" />
45+
</item>
46+
<item row="6" column="0" >
47+
<widget class="QDialogButtonBox" name="buttonBox" >
48+
<property name="orientation" >
49+
<enum>Qt::Horizontal</enum>
50+
</property>
51+
<property name="standardButtons" >
52+
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
53+
</property>
54+
</widget>
55+
</item>
56+
</layout>
57+
</widget>
58+
<resources/>
59+
<connections>
60+
<connection>
61+
<sender>buttonBox</sender>
62+
<signal>accepted()</signal>
63+
<receiver>QgsZonalStatisticsDialogBase</receiver>
64+
<slot>accept()</slot>
65+
<hints>
66+
<hint type="sourcelabel" >
67+
<x>248</x>
68+
<y>254</y>
69+
</hint>
70+
<hint type="destinationlabel" >
71+
<x>157</x>
72+
<y>274</y>
73+
</hint>
74+
</hints>
75+
</connection>
76+
<connection>
77+
<sender>buttonBox</sender>
78+
<signal>rejected()</signal>
79+
<receiver>QgsZonalStatisticsDialogBase</receiver>
80+
<slot>reject()</slot>
81+
<hints>
82+
<hint type="sourcelabel" >
83+
<x>316</x>
84+
<y>260</y>
85+
</hint>
86+
<hint type="destinationlabel" >
87+
<x>286</x>
88+
<y>274</y>
89+
</hint>
90+
</hints>
91+
</connection>
92+
</connections>
93+
</ui>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/***************************************************************************
2+
qgszonalstatisticsplugin.cpp - description
3+
----------------------------
4+
begin : August 29th, 2009
5+
copyright : (C) 2009 by Marco Hugentobler
6+
email : marco dot hugentobler at karto dot baug dot ethz dot ch
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 "qgszonalstatisticsplugin.h"
19+
#include "qgisinterface.h"
20+
#include "qgszonalstatistics.h"
21+
#include "qgszonalstatisticsdialog.h"
22+
#include "qgsvectorlayer.h"
23+
#include <QAction>
24+
#include <QProgressDialog>
25+
26+
static const QString name_ = QObject::tr( "Zonal statistics plugin" );
27+
static const QString description_ = QObject::tr( "A plugin to calculate count, sum, mean of rasters for each polygon of a vector layer" );
28+
static const QString version_ = QObject::tr( "Version 0.1" );
29+
30+
QgsZonalStatisticsPlugin::QgsZonalStatisticsPlugin( QgisInterface* iface ): mIface(iface), mAction(0)
31+
{
32+
33+
}
34+
35+
QgsZonalStatisticsPlugin::~QgsZonalStatisticsPlugin()
36+
{
37+
38+
}
39+
40+
void QgsZonalStatisticsPlugin::initGui()
41+
{
42+
mAction = new QAction( tr( "&Zonal statistics..." ), 0 );
43+
QObject::connect( mAction, SIGNAL( triggered() ), this, SLOT( run() ) );
44+
mIface->addToolBarIcon( mAction );
45+
mIface->addPluginToMenu( tr( "&Zonal statistics..." ), mAction );
46+
}
47+
48+
void QgsZonalStatisticsPlugin::unload()
49+
{
50+
51+
}
52+
53+
void QgsZonalStatisticsPlugin::run()
54+
{
55+
QgsZonalStatisticsDialog d(mIface);
56+
if(d.exec() == QDialog::Rejected)
57+
{
58+
return;
59+
}
60+
61+
QString rasterFile = d.rasterFilePath();
62+
QgsVectorLayer* vl = d.polygonLayer();
63+
if(!vl)
64+
{
65+
return;
66+
}
67+
68+
QgsZonalStatistics zs(vl, rasterFile, d.attributePrefix(), 1); //atm hardcode first band
69+
QProgressDialog p(tr("Calculating zonal statistics..."), tr("Abort..."), 0, 0);
70+
p.setWindowModality(Qt::WindowModal);
71+
zs.calculateStatistics(&p);
72+
}
73+
74+
//global methods for the plugin manager
75+
QGISEXTERN QgisPlugin* classFactory( QgisInterface * ifacePointer )
76+
{
77+
return new QgsZonalStatisticsPlugin( ifacePointer );
78+
}
79+
80+
QGISEXTERN QString name()
81+
{
82+
return name_;
83+
}
84+
85+
QGISEXTERN QString description()
86+
{
87+
return description_;
88+
}
89+
90+
QGISEXTERN QString version()
91+
{
92+
return version_;
93+
}
94+
95+
QGISEXTERN int type()
96+
{
97+
return QgisPlugin::UI;
98+
}
99+
100+
QGISEXTERN void unload( QgisPlugin* pluginPointer )
101+
{
102+
delete pluginPointer;
103+
}
104+
105+
106+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/***************************************************************************
2+
qgszonalstatisticsplugin.h - description
3+
-----------------------
4+
begin : August 29th, 2009
5+
copyright : (C) 2009 by Marco Hugentobler
6+
email : marco dot hugentobler at karto dot baug dot ethz dot ch
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+
#ifndef QGSZONALSTATISTICSPLUGIN_H
19+
#define QGSZONALSTATISTICSPLUGIN_H
20+
21+
#include "qgisplugin.h"
22+
#include <QObject>
23+
24+
class QgsInterface;
25+
class QAction;
26+
27+
class QgsZonalStatisticsPlugin: public QObject, public QgisPlugin
28+
{
29+
Q_OBJECT
30+
public:
31+
QgsZonalStatisticsPlugin( QgisInterface* iface );
32+
~QgsZonalStatisticsPlugin();
33+
34+
/**initialize connection to GUI*/
35+
void initGui();
36+
/**Unload the plugin and cleanup the GUI*/
37+
void unload();
38+
39+
private slots:
40+
/**Select input file, output file, format and analysis method*/
41+
void run();
42+
43+
private:
44+
QgisInterface* mIface;
45+
QAction* mAction;
46+
};
47+
48+
#endif // QGSZONALSTATISTICSPLUGIN_H

0 commit comments

Comments
 (0)
Please sign in to comment.