Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Expand equality operators for QgsAbstractGeometry subclasses
  • Loading branch information
nyalldawson committed Jan 4, 2018
1 parent df95536 commit 13aa521
Show file tree
Hide file tree
Showing 24 changed files with 145 additions and 64 deletions.
3 changes: 3 additions & 0 deletions python/core/geometry/qgsabstractgeometry.sip
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions python/core/geometry/qgscircularstring.sip
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions python/core/geometry/qgscompoundcurve.sip
Expand Up @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions python/core/geometry/qgscurve.sip
Expand Up @@ -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/;

Expand Down
6 changes: 4 additions & 2 deletions python/core/geometry/qgscurvepolygon.sip
Expand Up @@ -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();

Expand Down
5 changes: 5 additions & 0 deletions python/core/geometry/qgsgeometrycollection.sip
Expand Up @@ -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/;


Expand Down
4 changes: 1 addition & 3 deletions python/core/geometry/qgslinestring.sip
Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions python/core/geometry/qgspoint.sip
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/core/geometry/qgsabstractgeometry.h
Expand Up @@ -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
*/
Expand Down
7 changes: 1 addition & 6 deletions src/core/geometry/qgscircularstring.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 >();
Expand Down
3 changes: 1 addition & 2 deletions src/core/geometry/qgscircularstring.h
Expand Up @@ -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;
Expand Down
7 changes: 1 addition & 6 deletions src/core/geometry/qgscompoundcurve.cpp
Expand Up @@ -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 )
Expand All @@ -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 >();
Expand Down
3 changes: 1 addition & 2 deletions src/core/geometry/qgscompoundcurve.h
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/core/geometry/qgscurve.cpp
Expand Up @@ -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 )
Expand Down
6 changes: 4 additions & 2 deletions src/core/geometry/qgscurve.h
Expand Up @@ -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;

Expand Down
26 changes: 15 additions & 11 deletions src/core/geometry/qgscurvepolygon.cpp
Expand Up @@ -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 );
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgscurvepolygon.h
Expand Up @@ -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;

Expand Down
26 changes: 26 additions & 0 deletions src/core/geometry/qgsgeometrycollection.cpp
Expand Up @@ -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 >();
Expand Down
3 changes: 3 additions & 0 deletions src/core/geometry/qgsgeometrycollection.h
Expand Up @@ -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;

/**
Expand Down
7 changes: 1 addition & 6 deletions src/core/geometry/qgslinestring.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 );
Expand Down
3 changes: 1 addition & 2 deletions src/core/geometry/qgslinestring.h
Expand Up @@ -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.
Expand Down
18 changes: 11 additions & 7 deletions src/core/geometry/qgspoint.cpp
Expand Up @@ -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 );
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/geometry/qgspoint.h
Expand Up @@ -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.
Expand Down

0 comments on commit 13aa521

Please sign in to comment.