Skip to content
Permalink
Browse files

Add method to set QgsAbstractGeometry for QgsGeometry, add tests

for implicit sharing of QgsGeometry
  • Loading branch information
nyalldawson committed Jun 12, 2015
1 parent acfdcd9 commit 80cfbbf37e52120268f9a5866a1f814e0d96cf47
@@ -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();
@@ -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*/
@@ -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 );
@@ -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 );
@@ -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*/
@@ -30,6 +30,7 @@
#include <qgsapplication.h>
#include <qgsgeometry.h>
#include <qgspoint.h>
#include "qgspointv2.h"

//qgs unit test utility class
#include "qgsrenderchecker.h"
@@ -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();
@@ -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 );

0 comments on commit 80cfbbf

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