Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix QgsFeature equality operator handling of null geometries
Browse files Browse the repository at this point in the history
We were previously considering all features an unequal if they
had no geometry
nyalldawson authored and github-actions[bot] committed Aug 1, 2022
1 parent 94c8fdb commit 3d7cec6
Showing 2 changed files with 38 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/core/qgsfeature.cpp
Original file line number Diff line number Diff line change
@@ -64,15 +64,22 @@ bool QgsFeature::operator ==( const QgsFeature &other ) const
if ( d == other.d )
return true;

if ( d->fid == other.d->fid
&& d->valid == other.d->valid
&& d->fields == other.d->fields
&& d->attributes == other.d->attributes
&& d->geometry.equals( other.d->geometry )
&& d->symbol == other.d->symbol )
if ( !( d->fid == other.d->fid
&& d->valid == other.d->valid
&& d->fields == other.d->fields
&& d->attributes == other.d->attributes
&& d->symbol == other.d->symbol ) )
return false;

// compare geometry
if ( d->geometry.isNull() && other.d->geometry.isNull() )
return true;
else if ( d->geometry.isNull() || other.d->geometry.isNull() )
return false;
else if ( !d->geometry.equals( other.d->geometry ) )
return false;

return false;
return true;
}

bool QgsFeature::operator!=( const QgsFeature &other ) const
24 changes: 24 additions & 0 deletions tests/src/core/testqgsfeature.cpp
Original file line number Diff line number Diff line change
@@ -519,6 +519,30 @@ void TestQgsFeature::equality()
feature7.setGeometry( QgsGeometry( new QgsPoint( 1, 3 ) ) );

QVERIFY( feature != feature7 );

// features without geometry
QgsFeature feature8;
feature8.setFields( mFields, true );
feature8.setAttribute( 0, QStringLiteral( "attr1" ) );
feature8.setAttribute( 1, QStringLiteral( "attr2" ) );
feature8.setAttribute( 2, QStringLiteral( "attr3" ) );
feature8.setValid( true );
feature8.setId( 1 );
QgsFeature feature9;
feature9.setFields( mFields, true );
feature9.setAttribute( 0, QStringLiteral( "attr1" ) );
feature9.setAttribute( 1, QStringLiteral( "attr2" ) );
feature9.setAttribute( 2, QStringLiteral( "attr3" ) );
feature9.setValid( true );
feature9.setId( 1 );
QVERIFY( feature8 == feature9 );
feature8.setGeometry( QgsGeometry( new QgsPoint( 1, 3 ) ) );
QVERIFY( feature8 != feature9 );
feature8.clearGeometry();
feature9.setGeometry( QgsGeometry( new QgsPoint( 1, 3 ) ) );
QVERIFY( feature8 != feature9 );
feature8.setGeometry( QgsGeometry( new QgsPoint( 1, 3 ) ) );
QVERIFY( feature8 == feature9 );
}

void TestQgsFeature::attributeUsingField()

0 comments on commit 3d7cec6

Please sign in to comment.