Skip to content

Commit

Permalink
[processing] restore zonal statistics (qgis) alg (#4784)
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Jun 27, 2017
1 parent 182fe3c commit a6b14a0
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion doc/api_break.dox
Expand Up @@ -2530,7 +2530,7 @@ Triangulation {#qgis_api_break_3_0_Triangulation}
QgsZonalStatistics {#qgis_api_break_3_0_QgsZonalStatistics}
------------------
- QgsZonalStatistics() сonstructor now accepts pointer to the QgsRasterLayer instance instead of path to the raster file

- The calculateStatistics argument is now a QgsFeedback object

QGIS 2.6 {#qgis_api_break_2_6}
========
Expand Down
4 changes: 3 additions & 1 deletion python/analysis/vector/qgszonalstatistics.sip
Expand Up @@ -10,6 +10,8 @@





class QgsZonalStatistics
{
%Docstring
Expand Down Expand Up @@ -48,7 +50,7 @@ class QgsZonalStatistics
Constructor for QgsZonalStatistics.
%End

int calculateStatistics( QProgressDialog *p );
int calculateStatistics( QgsFeedback *feedback );
%Docstring
Starts the calculation
:return: 0 in case of success*
Expand Down
7 changes: 4 additions & 3 deletions python/plugins/processing/algs/qgis/QGISAlgorithmProvider.py
Expand Up @@ -71,6 +71,7 @@
from .SpatialiteExecuteSQL import SpatialiteExecuteSQL
from .SymmetricalDifference import SymmetricalDifference
from .VectorSplit import VectorSplit
from .ZonalStatisticsQgis import ZonalStatisticsQgis

# from .ExtractByLocation import ExtractByLocation
# from .PointsInPolygon import PointsInPolygon
Expand Down Expand Up @@ -152,7 +153,6 @@
# from .Relief import Relief
# from .IdwInterpolation import IdwInterpolation
# from .TinInterpolation import TinInterpolation
# from .ZonalStatisticsQgis import ZonalStatisticsQgis
# from .RemoveNullGeometry import RemoveNullGeometry
# from .ExtendLines import ExtendLines
# from .ExtractSpecificNodes import ExtractSpecificNodes
Expand Down Expand Up @@ -227,7 +227,7 @@ def getAlgs(self):
# OffsetLine(), Translate(),
# SingleSidedBuffer(), PointsAlongGeometry(),
# Slope(), Ruggedness(), Hillshade(),
# Relief(), ZonalStatisticsQgis(),
# Relief(),
# IdwInterpolation(), TinInterpolation(),
# RemoveNullGeometry(),
# ExtendLines(), ExtractSpecificNodes(),
Expand Down Expand Up @@ -271,7 +271,8 @@ def getAlgs(self):
Smooth(),
SpatialiteExecuteSQL(),
SymmetricalDifference(),
VectorSplit()
VectorSplit(),
ZonalStatisticsQgis()
]

if hasPlotly:
Expand Down
69 changes: 33 additions & 36 deletions python/plugins/processing/algs/qgis/ZonalStatisticsQgis.py
Expand Up @@ -30,16 +30,17 @@
from qgis.PyQt.QtGui import QIcon

from qgis.analysis import QgsZonalStatistics
from qgis.core import QgsProcessingUtils, QgsFeatureSink
from qgis.core import (QgsFeatureSink,
QgsProcessingUtils,
QgsProcessingParameterDefinition,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterRasterLayer,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterEnum,
QgsProcessingOutputVectorLayer)

from processing.algs.qgis.QgisAlgorithm import QgisAlgorithm
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterRaster
from processing.core.parameters import ParameterString
from processing.core.parameters import ParameterNumber
from processing.core.parameters import ParameterSelection
from processing.core.outputs import OutputVector
from processing.tools import dataobjects

pluginPath = os.path.split(os.path.split(os.path.dirname(__file__))[0])[0]

Expand All @@ -51,7 +52,6 @@ class ZonalStatisticsQgis(QgisAlgorithm):
INPUT_VECTOR = 'INPUT_VECTOR'
COLUMN_PREFIX = 'COLUMN_PREFIX'
STATISTICS = 'STATS'
OUTPUT_LAYER = 'OUTPUT_LAYER'

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'zonalstats.png'))
Expand All @@ -75,24 +75,23 @@ def __init__(self):
self.tr('All'): QgsZonalStatistics.All
}

self.addParameter(ParameterRaster(self.INPUT_RASTER,
self.tr('Raster layer')))
self.addParameter(ParameterNumber(self.RASTER_BAND,
self.tr('Raster band'),
1, 999, 1))
self.addParameter(ParameterVector(self.INPUT_VECTOR,
self.tr('Vector layer containing zones'),
[dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(ParameterString(self.COLUMN_PREFIX,
self.tr('Output column prefix'), '_'))
self.addParameter(ParameterSelection(self.STATISTICS,
self.tr('Statistics to calculate'),
list(self.STATS.keys()),
multiple=True))
self.addOutput(OutputVector(self.OUTPUT_LAYER,
self.tr('Zonal statistics'),
True,
datatype=[dataobjects.TYPE_VECTOR_POLYGON]))
self.addParameter(QgsProcessingParameterRasterLayer(self.INPUT_RASTER,
self.tr('Raster layer')))
self.addParameter(QgsProcessingParameterNumber(self.RASTER_BAND,
self.tr('Raster band'),
minValue=1, maxValue=999, defaultValue=1))
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT_VECTOR,
self.tr('Vector layer containing zones'),
[QgsProcessingParameterDefinition.TypeVectorPolygon]))
self.addParameter(QgsProcessingParameterString(self.COLUMN_PREFIX,
self.tr('Output column prefix'), '_'))
self.addParameter(QgsProcessingParameterEnum(self.STATISTICS,
self.tr('Statistics to calculate'),
list(self.STATS.keys()),
allowMultiple=True))
self.addOutput(QgsProcessingOutputVectorLayer(self.INPUT_VECTOR,
self.tr('Zonal statistics'),
QgsProcessingParameterDefinition.TypeVectorPolygon))

def name(self):
return 'zonalstatisticsqgis'
Expand All @@ -101,14 +100,12 @@ def displayName(self):
return self.tr('Zonal Statistics (QGIS)')

def processAlgorithm(self, parameters, context, feedback):
rasterPath = self.getParameterValue(self.INPUT_RASTER)
vectorPath = self.getParameterValue(self.INPUT_VECTOR)
bandNumber = self.getParameterValue(self.RASTER_BAND)
columnPrefix = self.getParameterValue(self.COLUMN_PREFIX)
st = self.getParameterValue(self.STATISTICS)
bandNumber = self.parameterAsInt(parameters, self.RASTER_BAND, context)
columnPrefix = self.parameterAsString(parameters, self.COLUMN_PREFIX, context)
st = self.parameterAsEnums(parameters, self.STATISTICS, context)

vectorLayer = QgsProcessingUtils.mapLayerFromString(vectorPath, context)
rasterLayer = QgsProcessingUtils.mapLayerFromString(rasterPath, context)
vectorLayer = self.parameterAsVectorLayer(parameters, self.INPUT_VECTOR, context)
rasterLayer = self.parameterAsRasterLayer(parameters, self.INPUT_RASTER, context)

keys = list(self.STATS.keys())
selectedStats = 0
Expand All @@ -120,6 +117,6 @@ def processAlgorithm(self, parameters, context, feedback):
columnPrefix,
bandNumber,
selectedStats)
zs.calculateStatistics(None)
zs.calculateStatistics(feedback)

self.setOutputValue(self.OUTPUT_LAYER, vectorPath)
return {self.INPUT_VECTOR: vectorLayer}
25 changes: 11 additions & 14 deletions src/analysis/vector/qgszonalstatistics.cpp
Expand Up @@ -17,16 +17,17 @@

#include "qgszonalstatistics.h"
#include "qgsfeatureiterator.h"
#include "qgsfeedback.h"
#include "qgsgeometry.h"
#include "qgsvectordataprovider.h"
#include "qgsvectorlayer.h"
#include "qgsrasterdataprovider.h"
#include "qgsrasterlayer.h"
#include "qgsrasterblock.h"
#include "qmath.h"
#include "qgslogger.h"

#include <QProgressDialog>
#include "qmath.h"

#include <QFile>

QgsZonalStatistics::QgsZonalStatistics( QgsVectorLayer *polygonLayer, QgsRasterLayer *rasterLayer, const QString &attributePrefix, int rasterBand, QgsZonalStatistics::Statistics stats )
Expand All @@ -37,7 +38,7 @@ QgsZonalStatistics::QgsZonalStatistics( QgsVectorLayer *polygonLayer, QgsRasterL
, mStatistics( stats )
{}

int QgsZonalStatistics::calculateStatistics( QProgressDialog *p )
int QgsZonalStatistics::calculateStatistics( QgsFeedback *feedback )
{
if ( !mPolygonLayer || mPolygonLayer->geometryType() != QgsWkbTypes::PolygonGeometry )
{
Expand Down Expand Up @@ -191,10 +192,6 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog *p )

//progress dialog
long featureCount = vectorProvider->featureCount();
if ( p )
{
p->setMaximum( featureCount );
}

//iterate over each polygon
QgsFeatureRequest request;
Expand All @@ -213,14 +210,14 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog *p )
QgsChangedAttributesMap changeMap;
while ( fi.nextFeature( f ) )
{
if ( p )
if ( feedback && feedback->isCanceled() )
{
p->setValue( featureCounter );
break;
}

if ( p && p->wasCanceled() )
if ( feedback )
{
break;
feedback->setProgress( 100.0 * static_cast< double >( featureCounter ) / featureCount );
}

if ( !f.hasGeometry() )
Expand Down Expand Up @@ -333,14 +330,14 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog *p )

vectorProvider->changeAttributeValues( changeMap );

if ( p )
if ( feedback )
{
p->setValue( featureCount );
feedback->setProgress( 100 );
}

mPolygonLayer->updateFields();

if ( p && p->wasCanceled() )
if ( feedback && feedback->isCanceled() )
{
return 9;
}
Expand Down
5 changes: 4 additions & 1 deletion src/analysis/vector/qgszonalstatistics.h
Expand Up @@ -20,9 +20,12 @@

#include <QString>
#include <QMap>

#include <limits>
#include <cfloat>

#include "qgis_analysis.h"
#include "qgsfeedback.h"

class QgsGeometry;
class QgsVectorLayer;
Expand Down Expand Up @@ -67,7 +70,7 @@ class ANALYSIS_EXPORT QgsZonalStatistics

/** Starts the calculation
\returns 0 in case of success*/
int calculateStatistics( QProgressDialog *p );
int calculateStatistics( QgsFeedback *feedback );

private:
QgsZonalStatistics() = default;
Expand Down

0 comments on commit a6b14a0

Please sign in to comment.