Skip to content

Commit

Permalink
also fix return for empty parameter point
Browse files Browse the repository at this point in the history
  • Loading branch information
uclaros authored and nyalldawson committed May 19, 2020
1 parent 0f3a6f0 commit e6fef7a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/core/geometry/qgsgeometry.cpp
Expand Up @@ -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 );
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions src/core/geometry/qgsgeometryutils.cpp
Expand Up @@ -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 ) )
Expand Down
5 changes: 5 additions & 0 deletions src/core/geometry/qgspoint.cpp
Expand Up @@ -67,6 +67,11 @@ QgsPoint::QgsPoint( const QgsPointXY &p )
, mM( std::numeric_limits<double>::quiet_NaN() )
{
mWkbType = QgsWkbTypes::Point;
if ( p.isEmpty() )
{
mX = std::numeric_limits<double>::quiet_NaN();
mY = std::numeric_limits<double>::quiet_NaN();
}
}

QgsPoint::QgsPoint( QPointF p )
Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_qgsgeometry.py
Expand Up @@ -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())

Expand Down
4 changes: 4 additions & 0 deletions tests/src/python/test_qgspoint.py
Expand Up @@ -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):

Expand Down

0 comments on commit e6fef7a

Please sign in to comment.