@@ -55,6 +55,7 @@ class TestQgsMapToolIdentifyAction : public QObject
55
55
void identifyRasterFloat64 (); // test pixel identification and decimal precision
56
56
void identifyInvalidPolygons (); // test selecting invalid polygons
57
57
void clickxy (); // test if clicked_x and clicked_y variables are propagated
58
+ void closestPoint ();
58
59
59
60
private:
60
61
void doAction ();
@@ -209,6 +210,64 @@ void TestQgsMapToolIdentifyAction::clickxy()
209
210
mIdentifyAction ->canvasReleaseEvent ( &mapReleases );
210
211
}
211
212
213
+ void TestQgsMapToolIdentifyAction::closestPoint ()
214
+ {
215
+ QgsSettings s;
216
+ s.setValue ( QStringLiteral ( " /qgis/measure/keepbaseunit" ), true );
217
+
218
+ // create a temporary layer
219
+ std::unique_ptr< QgsVectorLayer> tempLayer ( new QgsVectorLayer ( QStringLiteral ( " LineStringZM?crs=epsg:3111&field=pk:int&field=col1:double" ), QStringLiteral ( " vl" ), QStringLiteral ( " memory" ) ) );
220
+ QVERIFY ( tempLayer->isValid () );
221
+ QgsFeature f1 ( tempLayer->dataProvider ()->fields (), 1 );
222
+ f1.setAttribute ( QStringLiteral ( " pk" ), 1 );
223
+ f1.setAttribute ( QStringLiteral ( " col1" ), 0.0 );
224
+ QgsPolylineXY line3111;
225
+ line3111 << QgsPointXY ( 2484588 , 2425722 ) << QgsPointXY ( 2482767 , 2398853 );
226
+ QgsGeometry line3111G = QgsGeometry::fromWkt ( QStringLiteral ( " LineStringZM( 2484588 2425722 11 31, 2484588 2398853 15 37)" ) ) ;
227
+ f1.setGeometry ( line3111G );
228
+ tempLayer->dataProvider ()->addFeatures ( QgsFeatureList () << f1 );
229
+
230
+ // set project CRS and ellipsoid
231
+ QgsCoordinateReferenceSystem srs ( 3111 , QgsCoordinateReferenceSystem::EpsgCrsId );
232
+ canvas->setDestinationCrs ( srs );
233
+ canvas->setExtent ( f1.geometry ().boundingBox () );
234
+ QgsProject::instance ()->setCrs ( srs );
235
+ QgsProject::instance ()->setEllipsoid ( QStringLiteral ( " WGS84" ) );
236
+ QgsProject::instance ()->setDistanceUnits ( QgsUnitTypes::DistanceMeters );
237
+
238
+ QgsPointXY mapPoint = canvas->getCoordinateTransform ()->transform ( 2484587 , 2399800 );
239
+
240
+ std::unique_ptr< QgsMapToolIdentifyAction > action ( new QgsMapToolIdentifyAction ( canvas ) );
241
+
242
+ // check that closest point attributes are present
243
+ QList<QgsMapToolIdentify::IdentifyResult> result = action->identify ( mapPoint.x (), mapPoint.y (), QList<QgsMapLayer *>() << tempLayer.get () );
244
+ QCOMPARE ( result.length (), 1 );
245
+ QCOMPARE ( result.at ( 0 ).mDerivedAttributes [tr ( " Closest X" )], QStringLiteral ( " 2484588" ) );
246
+ QCOMPARE ( result.at ( 0 ).mDerivedAttributes [tr ( " Closest Y" )], QStringLiteral ( " 2399800" ) );
247
+ QCOMPARE ( result.at ( 0 ).mDerivedAttributes [tr ( " Interpolated M" )].left ( 4 ), QStringLiteral ( " 36.7" ) );
248
+ QCOMPARE ( result.at ( 0 ).mDerivedAttributes [tr ( " Interpolated Z" )].left ( 4 ), QStringLiteral ( " 14.8" ) );
249
+
250
+ // polygons
251
+ // create a temporary layer
252
+ std::unique_ptr< QgsVectorLayer> tempLayer2 ( new QgsVectorLayer ( QStringLiteral ( " PolygonZM?crs=epsg:3111&field=pk:int&field=col1:double" ), QStringLiteral ( " vl" ), QStringLiteral ( " memory" ) ) );
253
+ QVERIFY ( tempLayer2->isValid () );
254
+ f1 = QgsFeature ( tempLayer2->dataProvider ()->fields (), 1 );
255
+ f1.setAttribute ( QStringLiteral ( " pk" ), 1 );
256
+ f1.setAttribute ( QStringLiteral ( " col1" ), 0.0 );
257
+
258
+ f1.setGeometry ( QgsGeometry::fromWkt ( QStringLiteral ( " PolygonZM((2484588 2425722 1 11, 2484588 2398853 2 12, 2520109 2397715 3 13, 2520792 2425494 4 14, 2484588 2425722 1 11))" ) ) );
259
+ QVERIFY ( f1.hasGeometry () );
260
+ tempLayer2->dataProvider ()->addFeatures ( QgsFeatureList () << f1 );
261
+
262
+ mapPoint = canvas->getCoordinateTransform ()->transform ( 2484589 , 2399800 );
263
+ result = action->identify ( mapPoint.x (), mapPoint.y (), QList<QgsMapLayer *>() << tempLayer2.get () );
264
+ QCOMPARE ( result.length (), 1 );
265
+ QCOMPARE ( result.at ( 0 ).mDerivedAttributes [tr ( " Closest X" )], QStringLiteral ( " 2484588" ) );
266
+ QCOMPARE ( result.at ( 0 ).mDerivedAttributes [tr ( " Closest Y" )], QStringLiteral ( " 2399800" ) );
267
+ QCOMPARE ( result.at ( 0 ).mDerivedAttributes [tr ( " Interpolated M" )].left ( 4 ), QStringLiteral ( " 11.9" ) );
268
+ QCOMPARE ( result.at ( 0 ).mDerivedAttributes [tr ( " Interpolated Z" )].left ( 4 ), QStringLiteral ( " 1.96" ) );
269
+ }
270
+
212
271
void TestQgsMapToolIdentifyAction::lengthCalculation ()
213
272
{
214
273
QgsSettings s;
0 commit comments