@@ -55,6 +55,7 @@ class TestQgsGeometryUtils: public QObject
55
55
void testGradient ();
56
56
void testCoefficients ();
57
57
void testPerpendicularSegment ();
58
+ void testClosestPoint ();
58
59
};
59
60
60
61
@@ -633,5 +634,41 @@ void TestQgsGeometryUtils::testPerpendicularSegment()
633
634
QCOMPARE ( line.pointN ( 1 ), line_r.pointN ( 1 ) );
634
635
}
635
636
637
+ void TestQgsGeometryUtils::testClosestPoint ()
638
+ {
639
+ QgsLineString linestringZ ( QVector<QgsPoint>()
640
+ << QgsPoint ( 1 , 1 , 1 )
641
+ << QgsPoint ( 1 , 3 , 2 ) );
642
+
643
+ QgsPoint pt1 = QgsGeometryUtils::closestPoint ( linestringZ, QgsPoint ( 1 , 0 ) );
644
+ QGSCOMPARENEAR ( pt1.z (), 1 , 0.0001 );
645
+ QVERIFY ( qIsNaN ( pt1.m () ) );
646
+
647
+ QgsLineString linestringM ( QVector<QgsPoint>()
648
+ << QgsPoint ( 1 , 1 , std::numeric_limits<double >::quiet_NaN (), 1 )
649
+ << QgsPoint ( 1 , 3 , std::numeric_limits<double >::quiet_NaN (), 2 ) );
650
+
651
+ QgsPoint pt2 = QgsGeometryUtils::closestPoint ( linestringM, QgsPoint ( 1 , 4 ) );
652
+ QVERIFY ( qIsNaN ( pt2.z () ) );
653
+ QGSCOMPARENEAR ( pt2.m (), 2 , 0.0001 );
654
+
655
+ QgsLineString linestringZM ( QVector<QgsPoint>()
656
+ << QgsPoint ( 1 , 1 , 1 , 1 )
657
+ << QgsPoint ( 1 , 3 , 2 , 2 ) );
658
+
659
+ QgsPoint pt3 = QgsGeometryUtils::closestPoint ( linestringZM, QgsPoint ( 2 , 2 ) );
660
+ QGSCOMPARENEAR ( pt3.z (), 1.5 , 0.0001 );
661
+ QGSCOMPARENEAR ( pt3.m (), 1.5 , 0.0001 );
662
+
663
+ QgsLineString linestringDuplicatedPoint ( QVector<QgsPoint>()
664
+ << QgsPoint ( 1 , 1 , 1 , 1 )
665
+ << QgsPoint ( 1 , 1 , 1 , 1 )
666
+ << QgsPoint ( 1 , 3 , 2 , 2 ) );
667
+
668
+ QgsPoint pt4 = QgsGeometryUtils::closestPoint ( linestringDuplicatedPoint, QgsPoint ( 1 , 0 ) );
669
+ QGSCOMPARENEAR ( pt4.z (), 1 , 0.0001 );
670
+ QGSCOMPARENEAR ( pt4.m (), 1 , 0.0001 );
671
+ }
672
+
636
673
QGSTEST_MAIN ( TestQgsGeometryUtils )
637
674
#include " testqgsgeometryutils.moc"
0 commit comments