1616#include < QtTest/QtTest>
1717#include " qgsapplication.h"
1818#include " qgsvectorlayer.h"
19+ #include " qgsrasterlayer.h"
1920#include " qgsfeature.h"
2021#include " qgsgeometry.h"
2122#include " qgsvectordataprovider.h"
2425#include " qgsunittypes.h"
2526#include " qgsmaptoolidentifyaction.h"
2627
28+ #include " cpl_conv.h"
29+
2730class TestQgsMapToolIdentifyAction : public QObject
2831{
2932 Q_OBJECT
@@ -40,9 +43,13 @@ class TestQgsMapToolIdentifyAction : public QObject
4043 void lengthCalculation (); // test calculation of derived length attributes
4144 void perimeterCalculation (); // test calculation of derived perimeter attribute
4245 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
4348
4449 private:
4550 QgsMapCanvas* canvas;
51+
52+ QString testIdentifyRaster ( QgsRasterLayer* layer, double xGeoref, double yGeoref );
4653};
4754
4855void TestQgsMapToolIdentifyAction::initTestCase ()
@@ -241,6 +248,73 @@ void TestQgsMapToolIdentifyAction::areaCalculation()
241248 QVERIFY ( qgsDoubleNear ( area, 389.6117 , 0.001 ) );
242249}
243250
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+
244318
245319QTEST_MAIN ( TestQgsMapToolIdentifyAction )
246320#include " testqgsmaptoolidentifyaction.moc"
0 commit comments