Skip to content
Permalink
Browse files

Expand equality operators for QgsAbstractGeometry subclasses

  • Loading branch information
nyalldawson committed Jan 4, 2018
1 parent df95536 commit 13aa5211d1dc5f7d1410fe5fdf4a9ffcdff62215
@@ -72,6 +72,9 @@ Constructor for QgsAbstractGeometry.
virtual ~QgsAbstractGeometry();
QgsAbstractGeometry( const QgsAbstractGeometry &geom );

virtual bool operator==( const QgsAbstractGeometry &other ) const = 0;
virtual bool operator!=( const QgsAbstractGeometry &other ) const = 0;

virtual QgsAbstractGeometry *clone() const = 0 /Factory/;
%Docstring
Clones the geometry by performing a deep copy
@@ -25,9 +25,7 @@ class QgsCircularString: QgsCurve
public:
QgsCircularString();

virtual bool operator==( const QgsCurve &other ) const;

virtual bool operator!=( const QgsCurve &other ) const;
virtual bool equals( const QgsCurve &other ) const;


virtual QString geometryType() const;
@@ -25,9 +25,7 @@ class QgsCompoundCurve: QgsCurve
QgsCompoundCurve( const QgsCompoundCurve &curve );
~QgsCompoundCurve();

virtual bool operator==( const QgsCurve &other ) const;

virtual bool operator!=( const QgsCurve &other ) const;
virtual bool equals( const QgsCurve &other ) const;


virtual QString geometryType() const;
@@ -28,8 +28,12 @@ class QgsCurve: QgsAbstractGeometry
Constructor for QgsCurve.
%End

virtual bool operator==( const QgsCurve &other ) const = 0;
virtual bool operator!=( const QgsCurve &other ) const = 0;
virtual bool equals( const QgsCurve &other ) const = 0;

virtual bool operator==( const QgsAbstractGeometry &other ) const;

virtual bool operator!=( const QgsAbstractGeometry &other ) const;


virtual QgsCurve *clone() const = 0 /Factory/;

@@ -25,8 +25,10 @@ class QgsCurvePolygon: QgsSurface
QgsCurvePolygon();
QgsCurvePolygon( const QgsCurvePolygon &p );

bool operator==( const QgsCurvePolygon &other ) const;
bool operator!=( const QgsCurvePolygon &other ) const;
virtual bool operator==( const QgsAbstractGeometry &other ) const;

virtual bool operator!=( const QgsAbstractGeometry &other ) const;


~QgsCurvePolygon();

@@ -27,6 +27,11 @@ class QgsGeometryCollection: QgsAbstractGeometry
QgsGeometryCollection( const QgsGeometryCollection &c );
~QgsGeometryCollection();

virtual bool operator==( const QgsAbstractGeometry &other ) const;

virtual bool operator!=( const QgsAbstractGeometry &other ) const;


virtual QgsGeometryCollection *clone() const /Factory/;


@@ -57,9 +57,7 @@ or repeatedly calling addVertex()
.. versionadded:: 3.0
%End

virtual bool operator==( const QgsCurve &other ) const;

virtual bool operator!=( const QgsCurve &other ) const;
virtual bool equals( const QgsCurve &other ) const;


QgsPoint pointN( int i ) const;
@@ -85,8 +85,10 @@ Construct a QgsPoint from a QPointF
%End


bool operator==( const QgsPoint &pt ) const;
bool operator!=( const QgsPoint &pt ) const;
virtual bool operator==( const QgsAbstractGeometry &other ) const;

virtual bool operator!=( const QgsAbstractGeometry &other ) const;


double x() const;
%Docstring
@@ -110,6 +110,9 @@ class CORE_EXPORT QgsAbstractGeometry
QgsAbstractGeometry( const QgsAbstractGeometry &geom );
QgsAbstractGeometry &operator=( const QgsAbstractGeometry &geom );

virtual bool operator==( const QgsAbstractGeometry &other ) const = 0;
virtual bool operator!=( const QgsAbstractGeometry &other ) const = 0;

/**
* Clones the geometry by performing a deep copy
*/
@@ -33,7 +33,7 @@ QgsCircularString::QgsCircularString()
mWkbType = QgsWkbTypes::CircularString;
}

bool QgsCircularString::operator==( const QgsCurve &other ) const
bool QgsCircularString::equals( const QgsCurve &other ) const
{
const QgsCircularString *otherLine = dynamic_cast< const QgsCircularString * >( &other );
if ( !otherLine )
@@ -61,11 +61,6 @@ bool QgsCircularString::operator==( const QgsCurve &other ) const
return true;
}

bool QgsCircularString::operator!=( const QgsCurve &other ) const
{
return !operator==( other );
}

QgsCircularString *QgsCircularString::createEmptyWithSameType() const
{
auto result = qgis::make_unique< QgsCircularString >();
@@ -36,8 +36,7 @@ class CORE_EXPORT QgsCircularString: public QgsCurve
public:
QgsCircularString();

bool operator==( const QgsCurve &other ) const override;
bool operator!=( const QgsCurve &other ) const override;
bool equals( const QgsCurve &other ) const override;

QString geometryType() const override;
int dimension() const override;
@@ -35,7 +35,7 @@ QgsCompoundCurve::~QgsCompoundCurve()
clear();
}

bool QgsCompoundCurve::operator==( const QgsCurve &other ) const
bool QgsCompoundCurve::equals( const QgsCurve &other ) const
{
const QgsCompoundCurve *otherCurve = qgsgeometry_cast< const QgsCompoundCurve * >( &other );
if ( !otherCurve )
@@ -56,11 +56,6 @@ bool QgsCompoundCurve::operator==( const QgsCurve &other ) const
return true;
}

bool QgsCompoundCurve::operator!=( const QgsCurve &other ) const
{
return !operator==( other );
}

QgsCompoundCurve *QgsCompoundCurve::createEmptyWithSameType() const
{
auto result = qgis::make_unique< QgsCompoundCurve >();
@@ -36,8 +36,7 @@ class CORE_EXPORT QgsCompoundCurve: public QgsCurve
QgsCompoundCurve &operator=( const QgsCompoundCurve &curve );
~QgsCompoundCurve() override;

bool operator==( const QgsCurve &other ) const override;
bool operator!=( const QgsCurve &other ) const override;
bool equals( const QgsCurve &other ) const override;

QString geometryType() const override;
int dimension() const override;
@@ -22,6 +22,20 @@
#include "qgspoint.h"
#include "qgsmultipoint.h"

bool QgsCurve::operator==( const QgsAbstractGeometry &other ) const
{
const QgsCurve *otherCurve = qgsgeometry_cast< const QgsCurve * >( &other );
if ( !otherCurve )
return false;

return equals( *otherCurve );
}

bool QgsCurve::operator!=( const QgsAbstractGeometry &other ) const
{
return !operator==( other );
}

bool QgsCurve::isClosed() const
{
if ( numPoints() == 0 )
@@ -41,8 +41,10 @@ class CORE_EXPORT QgsCurve: public QgsAbstractGeometry
*/
QgsCurve() = default;

virtual bool operator==( const QgsCurve &other ) const = 0;
virtual bool operator!=( const QgsCurve &other ) const = 0;
virtual bool equals( const QgsCurve &other ) const = 0;

bool operator==( const QgsAbstractGeometry &other ) const override;
bool operator!=( const QgsAbstractGeometry &other ) const override;

QgsCurve *clone() const override = 0 SIP_FACTORY;

@@ -90,40 +90,44 @@ QgsCurvePolygon &QgsCurvePolygon::operator=( const QgsCurvePolygon &p )
return *this;
}

bool QgsCurvePolygon::operator==( const QgsCurvePolygon &other ) const
bool QgsCurvePolygon::operator==( const QgsAbstractGeometry &other ) const
{
const QgsCurvePolygon *otherPolygon = qgsgeometry_cast< const QgsCurvePolygon * >( &other );
if ( !otherPolygon )
return false;

//run cheap checks first
if ( mWkbType != other.mWkbType )
if ( mWkbType != otherPolygon->mWkbType )
return false;

if ( ( !mExteriorRing && other.mExteriorRing ) || ( mExteriorRing && !other.mExteriorRing ) )
if ( ( !mExteriorRing && otherPolygon->mExteriorRing ) || ( mExteriorRing && !otherPolygon->mExteriorRing ) )
return false;

if ( mInteriorRings.count() != other.mInteriorRings.count() )
if ( mInteriorRings.count() != otherPolygon->mInteriorRings.count() )
return false;

// compare rings
if ( mExteriorRing && other.mExteriorRing )
if ( mExteriorRing && otherPolygon->mExteriorRing )
{
if ( *mExteriorRing != *other.mExteriorRing )
if ( *mExteriorRing != *otherPolygon->mExteriorRing )
return false;
}

for ( int i = 0; i < mInteriorRings.count(); ++i )
{
if ( ( !mInteriorRings.at( i ) && other.mInteriorRings.at( i ) ) ||
( mInteriorRings.at( i ) && !other.mInteriorRings.at( i ) ) )
if ( ( !mInteriorRings.at( i ) && otherPolygon->mInteriorRings.at( i ) ) ||
( mInteriorRings.at( i ) && !otherPolygon->mInteriorRings.at( i ) ) )
return false;

if ( mInteriorRings.at( i ) && other.mInteriorRings.at( i ) &&
*mInteriorRings.at( i ) != *other.mInteriorRings.at( i ) )
if ( mInteriorRings.at( i ) && otherPolygon->mInteriorRings.at( i ) &&
*mInteriorRings.at( i ) != *otherPolygon->mInteriorRings.at( i ) )
return false;
}

return true;
}

bool QgsCurvePolygon::operator!=( const QgsCurvePolygon &other ) const
bool QgsCurvePolygon::operator!=( const QgsAbstractGeometry &other ) const
{
return !operator==( other );
}
@@ -38,8 +38,8 @@ class CORE_EXPORT QgsCurvePolygon: public QgsSurface
QgsCurvePolygon( const QgsCurvePolygon &p );
QgsCurvePolygon &operator=( const QgsCurvePolygon &p );

bool operator==( const QgsCurvePolygon &other ) const;
bool operator!=( const QgsCurvePolygon &other ) const;
bool operator==( const QgsAbstractGeometry &other ) const override;
bool operator!=( const QgsAbstractGeometry &other ) const override;

~QgsCurvePolygon() override;

@@ -64,6 +64,32 @@ QgsGeometryCollection::~QgsGeometryCollection()
clear();
}

bool QgsGeometryCollection::operator==( const QgsAbstractGeometry &other ) const
{
const QgsGeometryCollection *otherCollection = qgsgeometry_cast< const QgsGeometryCollection * >( &other );
if ( !otherCollection )
return false;

if ( mWkbType != otherCollection->mWkbType )
return false;

if ( mGeometries.count() != otherCollection->mGeometries.count() )
return false;

for ( int i = 0; i < mGeometries.count(); ++i )
{
if ( mGeometries.at( i ) != otherCollection->mGeometries.at( i ) )
return false;
}

return true;
}

bool QgsGeometryCollection::operator!=( const QgsAbstractGeometry &other ) const
{
return !operator==( other );
}

QgsGeometryCollection *QgsGeometryCollection::createEmptyWithSameType() const
{
auto result = qgis::make_unique< QgsGeometryCollection >();
@@ -39,6 +39,9 @@ class CORE_EXPORT QgsGeometryCollection: public QgsAbstractGeometry
QgsGeometryCollection &operator=( const QgsGeometryCollection &c );
~QgsGeometryCollection() override;

bool operator==( const QgsAbstractGeometry &other ) const override;
bool operator!=( const QgsAbstractGeometry &other ) const override;

QgsGeometryCollection *clone() const override SIP_FACTORY;

/**
@@ -136,7 +136,7 @@ QgsLineString::QgsLineString( const QVector<QgsPointXY> &points )
}
}

bool QgsLineString::operator==( const QgsCurve &other ) const
bool QgsLineString::equals( const QgsCurve &other ) const
{
const QgsLineString *otherLine = qgsgeometry_cast< const QgsLineString * >( &other );
if ( !otherLine )
@@ -164,11 +164,6 @@ bool QgsLineString::operator==( const QgsCurve &other ) const
return true;
}

bool QgsLineString::operator!=( const QgsCurve &other ) const
{
return !operator==( other );
}

QgsLineString *QgsLineString::clone() const
{
return new QgsLineString( *this );
@@ -71,8 +71,7 @@ class CORE_EXPORT QgsLineString: public QgsCurve
*/
QgsLineString( const QVector<QgsPointXY> &points );

bool operator==( const QgsCurve &other ) const override;
bool operator!=( const QgsCurve &other ) const override;
bool equals( const QgsCurve &other ) const override;

/**
* Returns the specified point from inside the line string.
@@ -91,22 +91,26 @@ QgsPoint::QgsPoint( QgsWkbTypes::Type wkbType, double x, double y, double z, dou
* See details in QEP #17
****************************************************************************/

bool QgsPoint::operator==( const QgsPoint &pt ) const
bool QgsPoint::operator==( const QgsAbstractGeometry &other ) const
{
const QgsPoint *pt = qgsgeometry_cast< const QgsPoint * >( &other );
if ( !pt )
return false;

const QgsWkbTypes::Type type = wkbType();

bool equal = pt.wkbType() == type;
equal &= qgsDoubleNear( pt.x(), mX, 1E-8 );
equal &= qgsDoubleNear( pt.y(), mY, 1E-8 );
bool equal = pt->wkbType() == type;
equal &= qgsDoubleNear( pt->x(), mX, 1E-8 );
equal &= qgsDoubleNear( pt->y(), mY, 1E-8 );
if ( QgsWkbTypes::hasZ( type ) )
equal &= qgsDoubleNear( pt.z(), mZ, 1E-8 ) || ( std::isnan( pt.z() ) && std::isnan( mZ ) );
equal &= qgsDoubleNear( pt->z(), mZ, 1E-8 ) || ( std::isnan( pt->z() ) && std::isnan( mZ ) );
if ( QgsWkbTypes::hasM( type ) )
equal &= qgsDoubleNear( pt.m(), mM, 1E-8 ) || ( std::isnan( pt.m() ) && std::isnan( mM ) );
equal &= qgsDoubleNear( pt->m(), mM, 1E-8 ) || ( std::isnan( pt->m() ) && std::isnan( mM ) );

return equal;
}

bool QgsPoint::operator!=( const QgsPoint &pt ) const
bool QgsPoint::operator!=( const QgsAbstractGeometry &pt ) const
{
return !operator==( pt );
}
@@ -117,8 +117,8 @@ class CORE_EXPORT QgsPoint: public QgsAbstractGeometry
*/
explicit QgsPoint( QgsWkbTypes::Type wkbType, double x = 0.0, double y = 0.0, double z = std::numeric_limits<double>::quiet_NaN(), double m = std::numeric_limits<double>::quiet_NaN() ) SIP_SKIP;

bool operator==( const QgsPoint &pt ) const;
bool operator!=( const QgsPoint &pt ) const;
bool operator==( const QgsAbstractGeometry &other ) const override;
bool operator!=( const QgsAbstractGeometry &other ) const override;

/**
* Returns the point's x-coordinate.

0 comments on commit 13aa521

Please sign in to comment.
You can’t perform that action at this time.