diff --git a/src/core/geometry/qgsgeometry.cpp b/src/core/geometry/qgsgeometry.cpp index ad7979fbd5e3..03ae4403f2f3 100644 --- a/src/core/geometry/qgsgeometry.cpp +++ b/src/core/geometry/qgsgeometry.cpp @@ -388,7 +388,7 @@ QgsPointXY QgsGeometry::closestVertex( const QgsPointXY &point, int &atVertex, i return QgsPointXY(); } - QgsPoint pt( point.x(), point.y() ); + QgsPoint pt( point ); QgsVertexId id; QgsPoint vp = QgsGeometryUtils::closestVertex( *( d->geometry ), pt, id ); @@ -630,7 +630,7 @@ double QgsGeometry::closestVertexWithContext( const QgsPointXY &point, int &atVe } QgsVertexId vId; - QgsPoint pt( point.x(), point.y() ); + QgsPoint pt( point ); QgsPoint closestPoint = QgsGeometryUtils::closestVertex( *( d->geometry ), pt, vId ); if ( !vId.isValid() ) return -1; diff --git a/src/core/geometry/qgsgeometryutils.cpp b/src/core/geometry/qgsgeometryutils.cpp index 6b49f4e0f51f..c7a7e815e87b 100644 --- a/src/core/geometry/qgsgeometryutils.cpp +++ b/src/core/geometry/qgsgeometryutils.cpp @@ -71,6 +71,9 @@ QgsPoint QgsGeometryUtils::closestVertex( const QgsAbstractGeometry &geom, const QgsPoint minDistPoint; id = QgsVertexId(); // set as invalid + if ( geom.isEmpty() || pt.isEmpty() ) + return minDistPoint; + QgsVertexId vertexId; QgsPoint vertex; while ( geom.nextVertex( vertexId, vertex ) ) diff --git a/src/core/geometry/qgspoint.cpp b/src/core/geometry/qgspoint.cpp index 2dd528fe31ad..6c91df6d48c1 100644 --- a/src/core/geometry/qgspoint.cpp +++ b/src/core/geometry/qgspoint.cpp @@ -67,6 +67,11 @@ QgsPoint::QgsPoint( const QgsPointXY &p ) , mM( std::numeric_limits::quiet_NaN() ) { mWkbType = QgsWkbTypes::Point; + if ( p.isEmpty() ) + { + mX = std::numeric_limits::quiet_NaN(); + mY = std::numeric_limits::quiet_NaN(); + } } QgsPoint::QgsPoint( QPointF p ) diff --git a/tests/src/python/test_qgsgeometry.py b/tests/src/python/test_qgsgeometry.py index 6073f48f25b7..709290305e84 100644 --- a/tests/src/python/test_qgsgeometry.py +++ b/tests/src/python/test_qgsgeometry.py @@ -1193,6 +1193,10 @@ def testClosestVertex(self): assert abs(dist - exp) < 0.00001, "Expected: %f; Got:%f" % (exp, dist) self.assertEqual(leftOf, -1) + (point, atVertex, beforeVertex, afterVertex, dist) = polygon.closestVertex(QgsPointXY()) + self.assertTrue(point.isEmpty()) + self.assertEqual(dist, -1) + (point, atVertex, beforeVertex, afterVertex, dist) = QgsGeometry().closestVertex(QgsPointXY(42, 42)) self.assertTrue(point.isEmpty()) diff --git a/tests/src/python/test_qgspoint.py b/tests/src/python/test_qgspoint.py index e2770ebf2096..a895c5940f42 100644 --- a/tests/src/python/test_qgspoint.py +++ b/tests/src/python/test_qgspoint.py @@ -86,6 +86,10 @@ def test_issue_32443(self): p = QgsPoint(1, 2, m=4, z=3) assert p.wkbType() == QgsWkbTypes.PointZM and p.x() == 1 and p.y() == 2 and p.z() == 3 and p.m() == 4 + def test_empty_QgsPointXY(self): + p = QgsPoint(QgsPointXY()) + assert p.isEmpty() + class TestQgsPoint(unittest.TestCase):