16
16
#include < QtTest/QtTest>
17
17
#include " qgsapplication.h"
18
18
#include " qgsvectorlayer.h"
19
+ #include " qgsrasterlayer.h"
19
20
#include " qgsfeature.h"
20
21
#include " qgsgeometry.h"
21
22
#include " qgsvectordataprovider.h"
24
25
#include " qgsunittypes.h"
25
26
#include " qgsmaptoolidentifyaction.h"
26
27
28
+ #include " cpl_conv.h"
29
+
27
30
class TestQgsMapToolIdentifyAction : public QObject
28
31
{
29
32
Q_OBJECT
@@ -40,9 +43,13 @@ class TestQgsMapToolIdentifyAction : public QObject
40
43
void lengthCalculation (); // test calculation of derived length attributes
41
44
void perimeterCalculation (); // test calculation of derived perimeter attribute
42
45
void areaCalculation (); // test calculation of derived area attribute
46
+ void identifyRasterFloat32 (); // test pixel identification and decimal precision
47
+ void identifyRasterFloat64 (); // test pixel identification and decimal precision
43
48
44
49
private:
45
50
QgsMapCanvas* canvas;
51
+
52
+ QString testIdentifyRaster ( QgsRasterLayer* layer, double xGeoref, double yGeoref );
46
53
};
47
54
48
55
void TestQgsMapToolIdentifyAction::initTestCase ()
@@ -241,6 +248,73 @@ void TestQgsMapToolIdentifyAction::areaCalculation()
241
248
QVERIFY ( qgsDoubleNear ( area, 389.6117 , 0.001 ) );
242
249
}
243
250
251
+ QString TestQgsMapToolIdentifyAction::testIdentifyRaster ( QgsRasterLayer* layer, double xGeoref, double yGeoref )
252
+ {
253
+ QScopedPointer< QgsMapToolIdentifyAction > action ( new QgsMapToolIdentifyAction ( canvas ) );
254
+ QgsPoint mapPoint = canvas->getCoordinateTransform ()->transform ( xGeoref, yGeoref );
255
+ QList<QgsMapToolIdentify::IdentifyResult> result = action->identify ( mapPoint.x (), mapPoint.y (), QList<QgsMapLayer*>() << layer );
256
+ if ( result.length () != 1 )
257
+ return " " ;
258
+ return result[0 ].mAttributes [" Band 1" ];
259
+ }
260
+
261
+ void TestQgsMapToolIdentifyAction::identifyRasterFloat32 ()
262
+ {
263
+ // create a temporary layer
264
+ QString raster = QString ( TEST_DATA_DIR ) + " /raster/test.asc" ;
265
+
266
+ // By default the QgsRasterLayer forces AAIGRID_DATATYPE=Float64
267
+ CPLSetConfigOption ( " AAIGRID_DATATYPE" , " Float32" );
268
+ QScopedPointer< QgsRasterLayer> tempLayer ( new QgsRasterLayer ( raster ) );
269
+ CPLSetConfigOption ( " AAIGRID_DATATYPE" , nullptr );
270
+
271
+ QVERIFY ( tempLayer->isValid () );
272
+
273
+ canvas->setExtent ( QgsRectangle ( 0 , 0 , 7 , 1 ) );
274
+
275
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 0.5 , 0.5 ), QString ( " -999.9" ) );
276
+
277
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 1.5 , 0.5 ), QString ( " -999.987" ) );
278
+
279
+ // More than 6 significant digits : precision loss in Float32
280
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 2.5 , 0.5 ), QString ( " 1.234568" ) ); // in .asc file : 1.2345678
281
+
282
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 3.5 , 0.5 ), QString ( " 123456" ) );
283
+
284
+ // More than 6 significant digits: no precision loss here for that particular value
285
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 4.5 , 0.5 ), QString ( " 1234567" ) );
286
+
287
+ // More than 6 significant digits: no precision loss here for that particular value
288
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 5.5 , 0.5 ), QString ( " -999.9876" ) );
289
+
290
+ // More than 6 significant digits: no precision loss here
291
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 6.5 , 0.5 ), QString ( " 1.234568" ) ); // in .asc file : 1.2345678901234
292
+ }
293
+
294
+ void TestQgsMapToolIdentifyAction::identifyRasterFloat64 ()
295
+ {
296
+ // create a temporary layer
297
+ QString raster = QString ( TEST_DATA_DIR ) + " /raster/test.asc" ;
298
+ QScopedPointer< QgsRasterLayer> tempLayer ( new QgsRasterLayer ( raster ) );
299
+ QVERIFY ( tempLayer->isValid () );
300
+
301
+ canvas->setExtent ( QgsRectangle ( 0 , 0 , 7 , 1 ) );
302
+
303
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 0.5 , 0.5 ), QString ( " -999.9" ) );
304
+
305
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 1.5 , 0.5 ), QString ( " -999.987" ) );
306
+
307
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 2.5 , 0.5 ), QString ( " 1.2345678" ) );
308
+
309
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 3.5 , 0.5 ), QString ( " 123456" ) );
310
+
311
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 4.5 , 0.5 ), QString ( " 1234567" ) );
312
+
313
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 5.5 , 0.5 ), QString ( " -999.9876" ) );
314
+
315
+ QCOMPARE ( testIdentifyRaster ( tempLayer.data (), 6.5 , 0.5 ), QString ( " 1.2345678901234" ) );
316
+ }
317
+
244
318
245
319
QTEST_MAIN ( TestQgsMapToolIdentifyAction )
246
320
#include " testqgsmaptoolidentifyaction.moc"
0 commit comments