Skip to content

Commit

Permalink
Add method to set QgsAbstractGeometry for QgsGeometry, add tests
Browse files Browse the repository at this point in the history
for implicit sharing of QgsGeometry
  • Loading branch information
nyalldawson committed Jun 12, 2015
1 parent acfdcd9 commit 80cfbbf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 3 deletions.
10 changes: 8 additions & 2 deletions python/core/geometry/qgsgeometry.sip
Expand Up @@ -34,10 +34,11 @@ class QgsGeometry
/** copy constructor will prompt a deep copy of the object */
QgsGeometry( const QgsGeometry & );

/** Creates a geometry from an abstract geometry object.
/** Creates a geometry from an abstract geometry object. Ownership of
* geom is transferred.
* @note added in QGIS 2.10
*/
QgsGeometry( QgsAbstractGeometryV2* geom );
QgsGeometry( QgsAbstractGeometryV2* geom /Transfer/ );

//! Destructor
~QgsGeometry();
Expand All @@ -47,6 +48,11 @@ class QgsGeometry
*/
const QgsAbstractGeometryV2* geometry() const;

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

/** Creates a new geometry from a WKT string */
static QgsGeometry* fromWkt( QString wkt ) /Factory/;
/** Creates a new geometry from a QgsPoint object*/
Expand Down
6 changes: 6 additions & 0 deletions src/core/geometry/qgsgeometry.cpp
Expand Up @@ -143,6 +143,12 @@ const QgsAbstractGeometryV2* QgsGeometry::geometry() const
return d->geometry;
}

void QgsGeometry::setGeometry( QgsAbstractGeometryV2* geometry )
{
detach( false );
d->geometry = geometry;
}

QgsGeometry* QgsGeometry::fromWkt( QString wkt )
{
QgsAbstractGeometryV2* geom = QgsGeometryImport::geomFromWkt( wkt );
Expand Down
8 changes: 7 additions & 1 deletion src/core/geometry/qgsgeometry.h
Expand Up @@ -80,7 +80,8 @@ class CORE_EXPORT QgsGeometry
*/
QgsGeometry & operator=( QgsGeometry const & rhs );

/** Creates a geometry from an abstract geometry object.
/** Creates a geometry from an abstract geometry object. Ownership of
* geom is transferred.
* @note added in QGIS 2.10
*/
QgsGeometry( QgsAbstractGeometryV2* geom );
Expand All @@ -93,6 +94,11 @@ class CORE_EXPORT QgsGeometry
*/
const QgsAbstractGeometryV2* geometry() const;

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

/** Creates a new geometry from a WKT string */
static QgsGeometry* fromWkt( QString wkt );
/** Creates a new geometry from a QgsPoint object*/
Expand Down
49 changes: 49 additions & 0 deletions tests/src/core/testqgsgeometry.cpp
Expand Up @@ -30,6 +30,7 @@
#include <qgsapplication.h>
#include <qgsgeometry.h>
#include <qgspoint.h>
#include "qgspointv2.h"

//qgs unit test utility class
#include "qgsrenderchecker.h"
Expand All @@ -50,6 +51,9 @@ class TestQgsGeometry : public QObject
void init();// will be called before each testfunction is executed.
void cleanup();// will be called after every testfunction.

void copy();
void assignment();

void fromQgsPoint();
void fromQPoint();
void fromQPolygonF();
Expand Down Expand Up @@ -207,6 +211,51 @@ void TestQgsGeometry::cleanup()
delete mpPainter;
}

void TestQgsGeometry::copy()
{
//create a point geometry
QgsGeometry original( new QgsPointV2( 1.0, 2.0 ) );
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );

//implicitly shared copy
QgsGeometry copy( original );
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );

//trigger a detach
copy.setGeometry( new QgsPointV2( 3.0, 4.0 ) );
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 3.0 );
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 4.0 );

//make sure original was untouched
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
}

void TestQgsGeometry::assignment()
{
//create a point geometry
QgsGeometry original( new QgsPointV2( 1.0, 2.0 ) );
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );

//assign to implicitly shared copy
QgsGeometry copy;
copy = original;
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );

//trigger a detach
copy.setGeometry( new QgsPointV2( 3.0, 4.0 ) );
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 3.0 );
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 4.0 );

//make sure original was untouched
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
}

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

0 comments on commit 80cfbbf

Please sign in to comment.