30
30
#define TO8F (x ) QFile::encodeName( x ).constData()
31
31
#endif
32
32
33
- QgsZonalStatistics::QgsZonalStatistics ( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand )
33
+ QgsZonalStatistics::QgsZonalStatistics ( QgsVectorLayer* polygonLayer, const QString& rasterFile, const QString& attributePrefix, int rasterBand , Statistics stats )
34
34
: mRasterFilePath( rasterFile )
35
35
, mRasterBand( rasterBand )
36
36
, mPolygonLayer( polygonLayer )
37
37
, mAttributePrefix( attributePrefix )
38
38
, mInputNodataValue( -1 )
39
+ , mStatistics( stats )
39
40
{
40
41
41
42
}
@@ -44,6 +45,7 @@ QgsZonalStatistics::QgsZonalStatistics()
44
45
: mRasterBand( 0 )
45
46
, mPolygonLayer( 0 )
46
47
, mInputNodataValue( -1 )
48
+ , mStatistics( QgsZonalStatistics::All )
47
49
{
48
50
49
51
}
@@ -110,26 +112,41 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog* p )
110
112
QgsRectangle rasterBBox ( geoTransform[0 ], geoTransform[3 ] - ( nCellsYGDAL * cellsizeY ),
111
113
geoTransform[0 ] + ( nCellsXGDAL * cellsizeX ), geoTransform[3 ] );
112
114
113
- // add the new count, sum, mean fields to the provider
115
+ // add the new fields to the provider
114
116
QList<QgsField> newFieldList;
115
- QString countFieldName = getUniqueFieldName ( mAttributePrefix + " count" );
116
- QString sumFieldName = getUniqueFieldName ( mAttributePrefix + " sum" );
117
- QString meanFieldName = getUniqueFieldName ( mAttributePrefix + " mean" );
118
- QgsField countField ( countFieldName, QVariant::Double, " double precision" );
119
- QgsField sumField ( sumFieldName, QVariant::Double, " double precision" );
120
- QgsField meanField ( meanFieldName, QVariant::Double, " double precision" );
121
- newFieldList.push_back ( countField );
122
- newFieldList.push_back ( sumField );
123
- newFieldList.push_back ( meanField );
117
+ QString countFieldName;
118
+ if ( mStatistics & QgsZonalStatistics::Count )
119
+ {
120
+ countFieldName = getUniqueFieldName ( mAttributePrefix + " count" );
121
+ QgsField countField ( countFieldName, QVariant::Double, " double precision" );
122
+ newFieldList.push_back ( countField );
123
+ }
124
+ QString sumFieldName;
125
+ if ( mStatistics & QgsZonalStatistics::Sum )
126
+ {
127
+ sumFieldName = getUniqueFieldName ( mAttributePrefix + " sum" );
128
+ QgsField sumField ( sumFieldName, QVariant::Double, " double precision" );
129
+ newFieldList.push_back ( sumField );
130
+ }
131
+ QString meanFieldName;
132
+ if ( mStatistics & QgsZonalStatistics::Mean )
133
+ {
134
+ meanFieldName = getUniqueFieldName ( mAttributePrefix + " mean" );
135
+ QgsField meanField ( meanFieldName, QVariant::Double, " double precision" );
136
+ newFieldList.push_back ( meanField );
137
+ }
124
138
vectorProvider->addAttributes ( newFieldList );
125
139
126
140
// index of the new fields
127
- int countIndex = vectorProvider->fieldNameIndex ( countFieldName );
128
- int sumIndex = vectorProvider->fieldNameIndex ( sumFieldName );
129
- int meanIndex = vectorProvider->fieldNameIndex ( meanFieldName );
141
+ int countIndex = mStatistics & QgsZonalStatistics::Count ? vectorProvider->fieldNameIndex ( countFieldName ) : - 1 ;
142
+ int sumIndex = mStatistics & QgsZonalStatistics::Sum ? vectorProvider->fieldNameIndex ( sumFieldName ) : - 1 ;
143
+ int meanIndex = mStatistics & QgsZonalStatistics::Mean ? vectorProvider->fieldNameIndex ( meanFieldName ) : - 1 ;
130
144
131
- if ( countIndex == -1 || sumIndex == -1 || meanIndex == -1 )
145
+ if (( mStatistics & QgsZonalStatistics::Count && countIndex == -1 )
146
+ || ( mStatistics & QgsZonalStatistics::Sum && sumIndex == -1 )
147
+ || ( mStatistics & QgsZonalStatistics::Mean && meanIndex == -1 ) )
132
148
{
149
+ // failed to create a required field
133
150
return 8 ;
134
151
}
135
152
@@ -204,22 +221,24 @@ int QgsZonalStatistics::calculateStatistics( QProgressDialog* p )
204
221
rasterBBox, sum, count );
205
222
}
206
223
207
-
208
- if ( count == 0 )
224
+ if ( mStatistics & QgsZonalStatistics::Mean && count > 0 )
209
225
{
210
- mean = 0 ;
226
+ mean = sum / count ;
211
227
}
212
228
else
213
229
{
214
- mean = sum / count ;
230
+ mean = 0 ;
215
231
}
216
232
217
233
// write the statistics value to the vector data provider
218
234
QgsChangedAttributesMap changeMap;
219
235
QgsAttributeMap changeAttributeMap;
220
- changeAttributeMap.insert ( countIndex, QVariant ( count ) );
221
- changeAttributeMap.insert ( sumIndex, QVariant ( sum ) );
222
- changeAttributeMap.insert ( meanIndex, QVariant ( mean ) );
236
+ if ( mStatistics & QgsZonalStatistics::Count )
237
+ changeAttributeMap.insert ( countIndex, QVariant ( count ) );
238
+ if ( mStatistics & QgsZonalStatistics::Sum )
239
+ changeAttributeMap.insert ( sumIndex, QVariant ( sum ) );
240
+ if ( mStatistics & QgsZonalStatistics::Mean )
241
+ changeAttributeMap.insert ( meanIndex, QVariant ( mean ) );
223
242
changeMap.insert ( f.id (), changeAttributeMap );
224
243
vectorProvider->changeAttributeValues ( changeMap );
225
244
0 commit comments