Skip to content
Permalink
Browse files

[FEATURE]: zonal statistics plugin

  • Loading branch information
mhugent committed Jun 10, 2011
1 parent 1b15528 commit fabc66aa1d4a63e135153bbefc5491a9c896aae0
@@ -13,6 +13,7 @@ ADD_SUBDIRECTORY(point_displacement_renderer)
ADD_SUBDIRECTORY(spatialquery)
ADD_SUBDIRECTORY(sqlanywhere)
ADD_SUBDIRECTORY(roadgraph)
ADD_SUBDIRECTORY(zonal_statistics)

IF (WITH_SPATIALITE)
ADD_SUBDIRECTORY(offline_editing)
@@ -0,0 +1,57 @@
########################################################
# Files

SET (ZONAL_STATISTICS_SRCS
qgszonalstatisticsplugin.cpp
qgszonalstatisticsdialog.cpp
)

SET (ZONAL_STATISTICS_UIS
qgszonalstatisticsdialogbase.ui
)

SET (ZONAL_STATISTICS_MOC_HDRS
qgszonalstatisticsdialog.h
qgszonalstatisticsplugin.h
)

########################################################
# Build

QT4_WRAP_UI (ZONAL_STATISTICS_UIS_H ${ZONAL_STATISTICS_UIS})

QT4_WRAP_CPP (ZONAL_STATISTICS_MOC_SRCS ${ZONAL_STATISTICS_MOC_HDRS})

QT4_ADD_RESOURCES(ZONAL_STATISTICS_RCC_SRCS ${ZONAL_STATISTICS_RCCS})

ADD_LIBRARY (zonalstatisticsplugin MODULE
${ZONAL_STATISTICS_SRCS}
${ZONAL_STATISTICS_MOC_SRCS}
${ZONAL_STATISTICS_RCC_SRCS}
${ZONAL_STATISTICS_UIS_H})

INCLUDE_DIRECTORIES(
${CMAKE_CURRENT_BINARY_DIR}
${GDAL_INCLUDE_DIR}
../../core
../../core/raster
../../gui
../../analysis/vector
..
.
)

TARGET_LINK_LIBRARIES(zonalstatisticsplugin
qgis_analysis
qgis_core
qgis_gui
)


########################################################
# Install

INSTALL(TARGETS zonalstatisticsplugin
RUNTIME DESTINATION ${QGIS_PLUGIN_DIR}
LIBRARY DESTINATION ${QGIS_PLUGIN_DIR}
)
@@ -0,0 +1,132 @@
/***************************************************************************
qgszonalstatisticsdialog.h - description
-----------------------
begin : September 1st, 2009
copyright : (C) 2009 by Marco Hugentobler
email : marco dot hugentobler at karto dot baug dot ethz dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgszonalstatisticsdialog.h"
#include "qgsmaplayerregistry.h"
#include "qgsrasterlayer.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"

QgsZonalStatisticsDialog::QgsZonalStatisticsDialog(QgisInterface* iface): QDialog(), mIface(iface)
{
setupUi(this);
insertAvailableLayers();
mColumnPrefixLineEdit->setText(proposeAttributePrefix());
}

QgsZonalStatisticsDialog::QgsZonalStatisticsDialog(): QDialog(0), mIface(0)
{
setupUi(this);
}

QgsZonalStatisticsDialog::~QgsZonalStatisticsDialog()
{

}

void QgsZonalStatisticsDialog::insertAvailableLayers()
{
//insert available raster layers
//enter available layers into the combo box
QMap<QString, QgsMapLayer*> mapLayers = QgsMapLayerRegistry::instance()->mapLayers();
QMap<QString, QgsMapLayer*>::iterator layer_it = mapLayers.begin();

for ( ; layer_it != mapLayers.end(); ++layer_it )
{
QgsRasterLayer* rl = dynamic_cast<QgsRasterLayer*>( layer_it.value() );
if ( rl )
{
mRasterLayerComboBox->addItem( rl->name(), QVariant( rl->source() ) );
}
else
{
QgsVectorLayer* vl = dynamic_cast<QgsVectorLayer*>( layer_it.value() );
if(vl && vl->geometryType() == QGis::Polygon)
{
mPolygonLayerComboBox->addItem( vl->name(), QVariant( vl->getLayerID() ) );
}
}
}
}

QString QgsZonalStatisticsDialog::rasterFilePath() const
{
int index = mRasterLayerComboBox->currentIndex();
if ( index == -1 )
{
return "";
}
return mRasterLayerComboBox->itemData( index ).toString();
}

QgsVectorLayer* QgsZonalStatisticsDialog::polygonLayer() const
{
int index = mPolygonLayerComboBox->currentIndex();
if(index == -1)
{
return 0;
}
return dynamic_cast<QgsVectorLayer*>(QgsMapLayerRegistry::instance()->mapLayer(mPolygonLayerComboBox->itemData( index ).toString()));
}

QString QgsZonalStatisticsDialog::attributePrefix() const
{
return mColumnPrefixLineEdit->text();
}

QString QgsZonalStatisticsDialog::proposeAttributePrefix() const
{
if(!polygonLayer())
{
return "";
}

QString proposedPrefix = "";
while(!prefixIsValid(proposedPrefix))
{
proposedPrefix.prepend("_");
}
return proposedPrefix;
}

bool QgsZonalStatisticsDialog::prefixIsValid(const QString& prefix) const
{
QgsVectorLayer* vl = polygonLayer();
if(!vl)
{
return false;
}
QgsVectorDataProvider* dp = vl->dataProvider();
if(!dp)
{
return false;
}

QgsFieldMap providerFieldMap = dp->fields();
QgsFieldMap::const_iterator it = providerFieldMap.constBegin();
QString currentFieldName;

for(; it != providerFieldMap.constEnd(); ++it)
{
currentFieldName = it.value().name();
if(currentFieldName == (prefix + "mean") || currentFieldName == (prefix + "sum") || currentFieldName == (prefix + "count") )
{
return false;
}
}
return true;
}
@@ -0,0 +1,50 @@
/***************************************************************************
qgszonalstatisticsdialog.h - description
-----------------------
begin : September 1st, 2009
copyright : (C) 2009 by Marco Hugentobler
email : marco dot hugentobler at karto dot baug dot ethz dot ch
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSZONALSTATISTICSDIALOG_H
#define QGSZONALSTATISTICSDIALOG_H

#include "ui_qgszonalstatisticsdialogbase.h"

class QgisInterface;
class QgsVectorLayer;

class QgsZonalStatisticsDialog: public QDialog, private Ui::QgsZonalStatisticsDialogBase
{
Q_OBJECT
public:
QgsZonalStatisticsDialog(QgisInterface* iface);
~QgsZonalStatisticsDialog();

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

private:
QgsZonalStatisticsDialog();
/**Fills the available raster and polygon layers into the combo boxes*/
void insertAvailableLayers();
/**Propose a valid prefix for the attributes*/
QString proposeAttributePrefix() const;
/**Check if a prefix can be used for the count, sum and mean attribute*/
bool prefixIsValid(const QString& prefix) const;

QgisInterface* mIface;
};

#endif // QGSZONALSTATISTICSDIALOG_H
@@ -0,0 +1,93 @@
<ui version="4.0" >
<class>QgsZonalStatisticsDialogBase</class>
<widget class="QDialog" name="QgsZonalStatisticsDialogBase" >
<property name="geometry" >
<rect>
<x>0</x>
<y>0</y>
<width>366</width>
<height>242</height>
</rect>
</property>
<property name="windowTitle" >
<string>Dialog</string>
</property>
<layout class="QGridLayout" name="gridLayout" >
<item row="0" column="0" >
<widget class="QLabel" name="mRasterLayerLabel" >
<property name="text" >
<string>Raster layer:</string>
</property>
</widget>
</item>
<item row="1" column="0" >
<widget class="QComboBox" name="mRasterLayerComboBox" />
</item>
<item row="2" column="0" >
<widget class="QLabel" name="mVectorLayerLabel" >
<property name="text" >
<string>Polygon layer containing the zones:</string>
</property>
</widget>
</item>
<item row="3" column="0" >
<widget class="QComboBox" name="mPolygonLayerComboBox" />
</item>
<item row="4" column="0" >
<widget class="QLabel" name="mColumnPrefixLabel" >
<property name="text" >
<string>Output column prefix:</string>
</property>
</widget>
</item>
<item row="5" column="0" >
<widget class="QLineEdit" name="mColumnPrefixLineEdit" />
</item>
<item row="6" column="0" >
<widget class="QDialogButtonBox" name="buttonBox" >
<property name="orientation" >
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons" >
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>QgsZonalStatisticsDialogBase</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel" >
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel" >
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>QgsZonalStatisticsDialogBase</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel" >
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel" >
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

0 comments on commit fabc66a

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