@@ -40,10 +40,12 @@ class TestQgsZonalStatistics : public QObject
40
40
41
41
void testStatistics ();
42
42
void testReprojection ();
43
+ void testNoData ();
43
44
44
45
private:
45
46
QgsVectorLayer *mVectorLayer = nullptr ;
46
47
QgsRasterLayer *mRasterLayer = nullptr ;
48
+ QString mTempPath ;
47
49
};
48
50
49
51
void TestQgsZonalStatistics::initTestCase ()
@@ -54,19 +56,19 @@ void TestQgsZonalStatistics::initTestCase()
54
56
55
57
QString myDataPath ( TEST_DATA_DIR ); // defined in CmakeLists.txt
56
58
QString myTestDataPath = myDataPath + " /zonalstatistics/" ;
57
- QString myTempPath = QDir::tempPath () + ' /' ;
59
+ mTempPath = QDir::tempPath () + ' /' ;
58
60
59
61
// copy test data to temp directory
60
62
QDir testDir ( myTestDataPath );
61
63
QStringList files = testDir.entryList ( QDir::Files | QDir::NoDotAndDotDot );
62
64
for ( int i = 0 ; i < files.size (); ++i )
63
65
{
64
- QFile::remove ( myTempPath + files.at ( i ) );
65
- QVERIFY ( QFile::copy ( myTestDataPath + files.at ( i ), myTempPath + files.at ( i ) ) );
66
+ QFile::remove ( mTempPath + files.at ( i ) );
67
+ QVERIFY ( QFile::copy ( myTestDataPath + files.at ( i ), mTempPath + files.at ( i ) ) );
66
68
}
67
69
68
- mVectorLayer = new QgsVectorLayer ( myTempPath + " polys.shp" , QStringLiteral ( " poly" ), QStringLiteral ( " ogr" ) );
69
- mRasterLayer = new QgsRasterLayer ( myTempPath + " edge_problem.asc" , QStringLiteral ( " raster" ), QStringLiteral ( " gdal" ) );
70
+ mVectorLayer = new QgsVectorLayer ( mTempPath + " polys.shp" , QStringLiteral ( " poly" ), QStringLiteral ( " ogr" ) );
71
+ mRasterLayer = new QgsRasterLayer ( mTempPath + " edge_problem.asc" , QStringLiteral ( " raster" ), QStringLiteral ( " gdal" ) );
70
72
QgsProject::instance ()->addMapLayers (
71
73
QList<QgsMapLayer *>() << mVectorLayer << mRasterLayer );
72
74
}
@@ -246,5 +248,59 @@ void TestQgsZonalStatistics::testReprojection()
246
248
QCOMPARE ( f.attribute ( " variance" ).toDouble (), 0.13888888888889 );
247
249
}
248
250
251
+ void TestQgsZonalStatistics::testNoData ()
252
+ {
253
+ QString myDataPath ( TEST_DATA_DIR ); // defined in CmakeLists.txt
254
+ QString myTestDataPath = myDataPath + " /zonalstatistics/" ;
255
+
256
+ // test that zonal stats respects no data and user set no data values
257
+ std::unique_ptr< QgsRasterLayer > rasterLayer = qgis::make_unique< QgsRasterLayer >( myTestDataPath + " raster.tif" , QStringLiteral ( " raster" ), QStringLiteral ( " gdal" ) );
258
+ std::unique_ptr< QgsVectorLayer > vectorLayer = qgis::make_unique< QgsVectorLayer >( mTempPath + " polys2.shp" , QStringLiteral ( " poly" ), QStringLiteral ( " ogr" ) );
259
+
260
+ QgsZonalStatistics zs ( vectorLayer.get (), rasterLayer.get (), QStringLiteral ( " n" ), 1 , QgsZonalStatistics::All );
261
+ zs.calculateStatistics ( nullptr );
262
+
263
+ QgsFeature f;
264
+ QgsFeatureRequest request;
265
+ QgsFeatureIterator it = vectorLayer->getFeatures ( request );
266
+ bool fetched = it.nextFeature ( f );
267
+ QVERIFY ( fetched );
268
+ QCOMPARE ( f.attribute ( " ncount" ).toDouble (), 16.0 );
269
+ QCOMPARE ( f.attribute ( " nsum" ).toDouble (), 13428.0 );
270
+
271
+ fetched = it.nextFeature ( f );
272
+ QVERIFY ( fetched );
273
+ QCOMPARE ( f.attribute ( " ncount" ).toDouble (), 103.0 );
274
+ QCOMPARE ( f.attribute ( " nsum" ).toDouble (), 90536.0 );
275
+
276
+ fetched = it.nextFeature ( f );
277
+ QVERIFY ( fetched );
278
+ QCOMPARE ( f.attribute ( " ncount" ).toDouble (), 0.0 );
279
+ QCOMPARE ( f.attribute ( " nsum" ).toDouble (), 0.0 );
280
+
281
+ // with user no data
282
+ rasterLayer->dataProvider ()->setUserNoDataValue ( 1 , QgsRasterRangeList () << QgsRasterRange ( 842 , 852 )
283
+ << QgsRasterRange ( 877 , 891 ) );
284
+
285
+ zs = QgsZonalStatistics ( vectorLayer.get (), rasterLayer.get (), QStringLiteral ( " un" ), 1 , QgsZonalStatistics::All );
286
+ zs.calculateStatistics ( nullptr );
287
+
288
+ it = vectorLayer->getFeatures ( request );
289
+ fetched = it.nextFeature ( f );
290
+ QVERIFY ( fetched );
291
+ QCOMPARE ( f.attribute ( " uncount" ).toDouble (), 8.0 );
292
+ QCOMPARE ( f.attribute ( " unsum" ).toDouble (), 6652.0 );
293
+
294
+ fetched = it.nextFeature ( f );
295
+ QVERIFY ( fetched );
296
+ QCOMPARE ( f.attribute ( " uncount" ).toDouble (), 52.0 );
297
+ QCOMPARE ( f.attribute ( " unsum" ).toDouble (), 45374.0 );
298
+
299
+ fetched = it.nextFeature ( f );
300
+ QVERIFY ( fetched );
301
+ QCOMPARE ( f.attribute ( " uncount" ).toDouble (), 0.0 );
302
+ QCOMPARE ( f.attribute ( " unsum" ).toDouble (), 0.0 );
303
+ }
304
+
249
305
QGSTEST_MAIN ( TestQgsZonalStatistics )
250
306
#include " testqgszonalstatistics.moc"
0 commit comments