Skip to content
Permalink
Browse files
add a is2DClosed test
  • Loading branch information
lbartoletti authored and nyalldawson committed Jun 8, 2021
1 parent c4c7ec2 commit 7cda3a8a0d9f1bb1608ea270a4b5c405953e6b23
@@ -37,7 +37,7 @@ bool QgsCurve::operator!=( const QgsAbstractGeometry &other ) const
return !operator==( other );
}

bool QgsCurve::isClosed() const
bool QgsCurve::is2DClosed() const
{
if ( numPoints() == 0 )
return false;
@@ -46,10 +46,18 @@ bool QgsCurve::isClosed() const
QgsPoint start = startPoint();
QgsPoint end = endPoint();

bool closed = qgsDoubleNear( start.x(), end.x() ) &&
qgsDoubleNear( start.y(), end.y() );
return qgsDoubleNear( start.x(), end.x() ) &&
qgsDoubleNear( start.y(), end.y() );
}
bool QgsCurve::isClosed() const
{
bool closed = is2DClosed();
if ( is3D() && closed )
{
QgsPoint start = startPoint();
QgsPoint end = endPoint();
closed &= qgsDoubleNear( start.z(), end.z() ) || ( std::isnan( start.z() ) && std::isnan( end.z() ) );
}
return closed;
}

@@ -66,9 +66,22 @@ class CORE_EXPORT QgsCurve: public QgsAbstractGeometry SIP_ABSTRACT

/**
* Returns TRUE if the curve is closed.
*
* \see is2DClosed()
*/
virtual bool isClosed() const SIP_HOLDGIL;

/**
* Returns true if the curve is closed.
*
* Unlike isClosed. It looks only for XY coordinates.
*
* \see isClosed()
*
* \since QGIS 3.20
*/
virtual bool is2DClosed() const SIP_HOLDGIL;

/**
* Returns TRUE if the curve is a ring.
*/
@@ -406,13 +406,19 @@ bool QgsLineString::removeDuplicateNodes( double epsilon, bool useZValues )
return result;
}

bool QgsLineString::isClosed() const
bool QgsLineString::is2DClosed() const
{
if ( mX.empty() )
return false;

bool closed = qgsDoubleNear( mX.first(), mX.last() ) &&
qgsDoubleNear( mY.first(), mY.last() );
return qgsDoubleNear( mX.first(), mX.last() ) &&
qgsDoubleNear( mY.first(), mY.last() );
}

bool QgsLineString::isClosed() const
{
bool closed = is2DClosed();

if ( is3D() && closed )
closed &= qgsDoubleNear( mZ.first(), mZ.last() ) || ( std::isnan( mZ.first() ) && std::isnan( mZ.last() ) );
return closed;
@@ -779,6 +779,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
QgsLineString *snappedToGrid( double hSpacing, double vSpacing, double dSpacing = 0, double mSpacing = 0 ) const override SIP_FACTORY;
bool removeDuplicateNodes( double epsilon = 4 * std::numeric_limits<double>::epsilon(), bool useZValues = false ) override;
bool isClosed() const override SIP_HOLDGIL;
bool is2DClosed() const override SIP_HOLDGIL;
bool boundingBoxIntersects( const QgsRectangle &rectangle ) const override SIP_HOLDGIL;

/**
@@ -1805,11 +1805,13 @@ void TestQgsGeometry::circularString()

//isClosed
QgsCircularString l11;
QVERIFY( !l11.is2DClosed() );
QVERIFY( !l11.isClosed() );
l11.setPoints( QgsPointSequence() << QgsPoint( 1, 2 )
<< QgsPoint( 11, 2 )
<< QgsPoint( 11, 22 )
<< QgsPoint( 1, 22 ) );
QVERIFY( !l11.is2DClosed() );
QVERIFY( !l11.isClosed() );
QCOMPARE( l11.numPoints(), 4 );
QCOMPARE( l11.area(), 0.0 );
@@ -1820,8 +1822,19 @@ void TestQgsGeometry::circularString()
<< QgsPoint( QgsWkbTypes::PointM, 11, 2, 0, 4 )
<< QgsPoint( QgsWkbTypes::PointM, 11, 22, 0, 5 )
<< QgsPoint( QgsWkbTypes::PointM, 1, 2, 0, 6 ) );
QVERIFY( l11.is2DClosed() );
QVERIFY( l11.isClosed() );

// test with z
l11.addZValue( 123.0 );
QVERIFY( l11.is2DClosed() );
QVERIFY( l11.isClosed() );
QgsPoint pEnd = l11.endPoint();
pEnd.setZ( 234.0 );
l11.moveVertex( QgsVertexId( 0, 0, l11.numPoints() - 1 ), pEnd );
QVERIFY( l11.is2DClosed() );
QVERIFY( !l11.isClosed() );

//polygonf
QgsCircularString l13;
l13.setPoints( QgsPointSequence() << QgsPoint( QgsWkbTypes::PointZM, 1, 2, 3, 4 )

0 comments on commit 7cda3a8

Please sign in to comment.