Add QgsGeometry::isEmpty to test if underlying geometry exists

This commit is contained in:
Nyall Dawson 2015-06-13 13:42:13 +10:00
parent 64e43f806a
commit 53218948b1
4 changed files with 36 additions and 0 deletions

View File

@ -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*/

View File

@ -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 );

View File

@ -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*/

View File

@ -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();
@ -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 );