Skip to content

Commit

Permalink
Add QgsGeometry::isEmpty to test if underlying geometry exists
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 13, 2015
1 parent 64e43f8 commit 5321894
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions python/core/geometry/qgsgeometry.sip
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,23 @@ class QgsGeometry

/** Returns the underlying geometry store.
* @note added in QGIS 2.10
* @see setGeometry
*/
const QgsAbstractGeometryV2* geometry() const;

/** Sets the underlying geometry store. Ownership of geometry is transferred.
* @note added in QGIS 2.10
* @see geometry
*/
void setGeometry( QgsAbstractGeometryV2* geometry /Transfer/ );

/** Returns true if the geometry is empty (ie, contains no underlying geometry
* accessible via @link geometry @endlink).
* @see geometry
* @note added in QGIS 2.10
*/
bool isEmpty() const;

/** Creates a new geometry from a WKT string */
static QgsGeometry* fromWkt( QString wkt ) /Factory/;
/** Creates a new geometry from a QgsPoint object*/
Expand Down
5 changes: 5 additions & 0 deletions src/core/geometry/qgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ void QgsGeometry::setGeometry( QgsAbstractGeometryV2* geometry )
d->geometry = geometry;
}

bool QgsGeometry::isEmpty() const
{
return !d || !d->geometry;
}

QgsGeometry* QgsGeometry::fromWkt( QString wkt )
{
QgsAbstractGeometryV2* geom = QgsGeometryImport::geomFromWkt( wkt );
Expand Down
9 changes: 9 additions & 0 deletions src/core/geometry/qgsgeometry.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,23 @@ class CORE_EXPORT QgsGeometry

/** Returns the underlying geometry store.
* @note added in QGIS 2.10
* @see setGeometry
*/
const QgsAbstractGeometryV2* geometry() const;

/** Sets the underlying geometry store. Ownership of geometry is transferred.
* @note added in QGIS 2.10
* @see geometry
*/
void setGeometry( QgsAbstractGeometryV2* geometry );

/** Returns true if the geometry is empty (ie, contains no underlying geometry
* accessible via @link geometry @endlink).
* @see geometry
* @note added in QGIS 2.10
*/
bool isEmpty() const;

/** Creates a new geometry from a WKT string */
static QgsGeometry* fromWkt( QString wkt );
/** Creates a new geometry from a QgsPoint object*/
Expand Down
13 changes: 13 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TestQgsGeometry : public QObject
void copy();
void assignment();
void asVariant(); //test conversion to and from a QVariant
void isEmpty();

void fromQgsPoint();
void fromQPoint();
Expand Down Expand Up @@ -286,6 +287,18 @@ void TestQgsGeometry::asVariant()
QCOMPARE( fromVar3.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
}

void TestQgsGeometry::isEmpty()
{
QgsGeometry geom;
QVERIFY( geom.isEmpty() );

geom.setGeometry( new QgsPointV2( 1.0, 2.0 ) );
QVERIFY( !geom.isEmpty() );

geom.setGeometry( 0 );
QVERIFY( geom.isEmpty() );
}

void TestQgsGeometry::fromQgsPoint()
{
QgsPoint point( 1.0, 2.0 );
Expand Down

0 comments on commit 5321894

Please sign in to comment.