Skip to content

Commit d2bf8d8

Browse files
committed
Add equality operators to QgsLineStringV2
1 parent 6dfffc5 commit d2bf8d8

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed

python/core/geometry/qgslinestringv2.sip

+3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ class QgsLineStringV2: public QgsCurveV2
1414
QgsLineStringV2();
1515
~QgsLineStringV2();
1616

17+
bool operator==( const QgsLineStringV2& other ) const;
18+
bool operator!=( const QgsLineStringV2& other ) const;
19+
1720
/** Returns the specified point from inside the line string.
1821
* @param i index of point, starting at 0 for the first point
1922
*/

src/core/geometry/qgslinestringv2.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,35 @@ QgsLineStringV2::QgsLineStringV2(): QgsCurveV2()
4141
QgsLineStringV2::~QgsLineStringV2()
4242
{}
4343

44+
bool QgsLineStringV2::operator==( const QgsLineStringV2& other ) const
45+
{
46+
if ( mWkbType != other.mWkbType )
47+
return false;
48+
49+
if ( mX.count() != other.mX.count() )
50+
return false;
51+
52+
for ( int i = 0; i < mX.count(); ++i )
53+
{
54+
if ( !qgsDoubleNear( mX.at( i ), other.mX.at( i ) )
55+
|| !qgsDoubleNear( mY.at( i ), other.mY.at( i ) ) )
56+
return false;
57+
58+
if ( is3D() && !qgsDoubleNear( mZ.at( i ), other.mZ.at( i ) ) )
59+
return false;
60+
61+
if ( isMeasure() && !qgsDoubleNear( mM.at( i ), other.mM.at( i ) ) )
62+
return false;
63+
}
64+
65+
return true;
66+
}
67+
68+
bool QgsLineStringV2::operator!=( const QgsLineStringV2& other ) const
69+
{
70+
return !operator==( other );
71+
}
72+
4473
QgsLineStringV2 *QgsLineStringV2::clone() const
4574
{
4675
return new QgsLineStringV2( *this );

src/core/geometry/qgslinestringv2.h

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class CORE_EXPORT QgsLineStringV2: public QgsCurveV2
3939
QgsLineStringV2();
4040
~QgsLineStringV2();
4141

42+
bool operator==( const QgsLineStringV2& other ) const;
43+
bool operator!=( const QgsLineStringV2& other ) const;
44+
4245
/** Returns the specified point from inside the line string.
4346
* @param i index of point, starting at 0 for the first point
4447
*/

tests/src/core/testqgsgeometry.cpp

+41
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,47 @@ void TestQgsGeometry::lineStringV2()
10751075
QCOMPARE( l10.pointN( 1 ), QgsPointV2( QgsWKBTypes::Point25D, 31, 32, 33 ) );
10761076
QCOMPARE( l10.pointN( 2 ), QgsPointV2( QgsWKBTypes::Point25D, 41, 42, 43 ) );
10771077

1078+
//equality
1079+
QgsLineStringV2 e1;
1080+
QgsLineStringV2 e2;
1081+
QVERIFY( e1 == e2 );
1082+
QVERIFY( !( e1 != e2 ) );
1083+
e1.addVertex( QgsPointV2( 1, 2 ) );
1084+
QVERIFY( !( e1 == e2 ) ); //different number of vertices
1085+
QVERIFY( e1 != e2 );
1086+
e2.addVertex( QgsPointV2( 1, 2 ) );
1087+
QVERIFY( e1 == e2 );
1088+
QVERIFY( !( e1 != e2 ) );
1089+
e1.addVertex( QgsPointV2( 1 / 3.0, 4 / 3.0 ) );
1090+
e2.addVertex( QgsPointV2( 2 / 6.0, 8 / 6.0 ) );
1091+
QVERIFY( e1 == e2 ); //check non-integer equality
1092+
QVERIFY( !( e1 != e2 ) );
1093+
e1.addVertex( QgsPointV2( 7, 8 ) );
1094+
e2.addVertex( QgsPointV2( 6, 9 ) );
1095+
QVERIFY( !( e1 == e2 ) ); //different coordinates
1096+
QVERIFY( e1 != e2 );
1097+
QgsLineStringV2 e3;
1098+
e3.setPoints( QList< QgsPointV2 >() << QgsPointV2( QgsWKBTypes::PointZ, 1, 2, 0 )
1099+
<< QgsPointV2( QgsWKBTypes::PointZ, 1 / 3.0, 4 / 3.0, 0 )
1100+
<< QgsPointV2( QgsWKBTypes::PointZ, 7, 8, 0 ) );
1101+
QVERIFY( !( e1 == e3 ) ); //different dimension
1102+
QVERIFY( e1 != e3 );
1103+
QgsLineStringV2 e4;
1104+
e4.setPoints( QList< QgsPointV2 >() << QgsPointV2( QgsWKBTypes::PointZ, 1, 2, 2 )
1105+
<< QgsPointV2( QgsWKBTypes::PointZ, 1 / 3.0, 4 / 3.0, 3 )
1106+
<< QgsPointV2( QgsWKBTypes::PointZ, 7, 8, 4 ) );
1107+
QVERIFY( !( e3 == e4 ) ); //different z coordinates
1108+
QVERIFY( e3 != e4 );
1109+
QgsLineStringV2 e5;
1110+
e5.setPoints( QList< QgsPointV2 >() << QgsPointV2( QgsWKBTypes::PointM, 1, 2, 0, 1 )
1111+
<< QgsPointV2( QgsWKBTypes::PointM, 1 / 3.0, 4 / 3.0, 0, 2 )
1112+
<< QgsPointV2( QgsWKBTypes::PointM, 7, 8, 0, 3 ) );
1113+
QgsLineStringV2 e6;
1114+
e6.setPoints( QList< QgsPointV2 >() << QgsPointV2( QgsWKBTypes::PointM, 1, 2, 0, 11 )
1115+
<< QgsPointV2( QgsWKBTypes::PointM, 1 / 3.0, 4 / 3.0, 0, 12 )
1116+
<< QgsPointV2( QgsWKBTypes::PointM, 7, 8, 0, 13 ) );
1117+
QVERIFY( !( e5 == e6 ) ); //different m values
1118+
QVERIFY( e5 != e6 );
10781119

10791120
//close/isClosed
10801121
QgsLineStringV2 l11;

0 commit comments

Comments
 (0)