Skip to content

Commit 80cfbbf

Browse files
committed
Add method to set QgsAbstractGeometry for QgsGeometry, add tests
for implicit sharing of QgsGeometry
1 parent acfdcd9 commit 80cfbbf

File tree

4 files changed

+70
-3
lines changed

4 files changed

+70
-3
lines changed

python/core/geometry/qgsgeometry.sip

+8-2
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@ class QgsGeometry
3434
/** copy constructor will prompt a deep copy of the object */
3535
QgsGeometry( const QgsGeometry & );
3636

37-
/** Creates a geometry from an abstract geometry object.
37+
/** Creates a geometry from an abstract geometry object. Ownership of
38+
* geom is transferred.
3839
* @note added in QGIS 2.10
3940
*/
40-
QgsGeometry( QgsAbstractGeometryV2* geom );
41+
QgsGeometry( QgsAbstractGeometryV2* geom /Transfer/ );
4142

4243
//! Destructor
4344
~QgsGeometry();
@@ -47,6 +48,11 @@ class QgsGeometry
4748
*/
4849
const QgsAbstractGeometryV2* geometry() const;
4950

51+
/** Sets the underlying geometry store. Ownership of geometry is transferred.
52+
* @note added in QGIS 2.10
53+
*/
54+
void setGeometry( QgsAbstractGeometryV2* geometry /Transfer/ );
55+
5056
/** Creates a new geometry from a WKT string */
5157
static QgsGeometry* fromWkt( QString wkt ) /Factory/;
5258
/** Creates a new geometry from a QgsPoint object*/

src/core/geometry/qgsgeometry.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,12 @@ const QgsAbstractGeometryV2* QgsGeometry::geometry() const
143143
return d->geometry;
144144
}
145145

146+
void QgsGeometry::setGeometry( QgsAbstractGeometryV2* geometry )
147+
{
148+
detach( false );
149+
d->geometry = geometry;
150+
}
151+
146152
QgsGeometry* QgsGeometry::fromWkt( QString wkt )
147153
{
148154
QgsAbstractGeometryV2* geom = QgsGeometryImport::geomFromWkt( wkt );

src/core/geometry/qgsgeometry.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class CORE_EXPORT QgsGeometry
8080
*/
8181
QgsGeometry & operator=( QgsGeometry const & rhs );
8282

83-
/** Creates a geometry from an abstract geometry object.
83+
/** Creates a geometry from an abstract geometry object. Ownership of
84+
* geom is transferred.
8485
* @note added in QGIS 2.10
8586
*/
8687
QgsGeometry( QgsAbstractGeometryV2* geom );
@@ -93,6 +94,11 @@ class CORE_EXPORT QgsGeometry
9394
*/
9495
const QgsAbstractGeometryV2* geometry() const;
9596

97+
/** Sets the underlying geometry store. Ownership of geometry is transferred.
98+
* @note added in QGIS 2.10
99+
*/
100+
void setGeometry( QgsAbstractGeometryV2* geometry );
101+
96102
/** Creates a new geometry from a WKT string */
97103
static QgsGeometry* fromWkt( QString wkt );
98104
/** Creates a new geometry from a QgsPoint object*/

tests/src/core/testqgsgeometry.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <qgsapplication.h>
3131
#include <qgsgeometry.h>
3232
#include <qgspoint.h>
33+
#include "qgspointv2.h"
3334

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

54+
void copy();
55+
void assignment();
56+
5357
void fromQgsPoint();
5458
void fromQPoint();
5559
void fromQPolygonF();
@@ -207,6 +211,51 @@ void TestQgsGeometry::cleanup()
207211
delete mpPainter;
208212
}
209213

214+
void TestQgsGeometry::copy()
215+
{
216+
//create a point geometry
217+
QgsGeometry original( new QgsPointV2( 1.0, 2.0 ) );
218+
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
219+
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
220+
221+
//implicitly shared copy
222+
QgsGeometry copy( original );
223+
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
224+
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
225+
226+
//trigger a detach
227+
copy.setGeometry( new QgsPointV2( 3.0, 4.0 ) );
228+
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 3.0 );
229+
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 4.0 );
230+
231+
//make sure original was untouched
232+
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
233+
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
234+
}
235+
236+
void TestQgsGeometry::assignment()
237+
{
238+
//create a point geometry
239+
QgsGeometry original( new QgsPointV2( 1.0, 2.0 ) );
240+
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
241+
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
242+
243+
//assign to implicitly shared copy
244+
QgsGeometry copy;
245+
copy = original;
246+
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
247+
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
248+
249+
//trigger a detach
250+
copy.setGeometry( new QgsPointV2( 3.0, 4.0 ) );
251+
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 3.0 );
252+
QCOMPARE( copy.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 4.0 );
253+
254+
//make sure original was untouched
255+
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).x(), 1.0 );
256+
QCOMPARE( original.geometry()->vertexAt( QgsVertexId( 0, 0, 0 ) ).y(), 2.0 );
257+
}
258+
210259
void TestQgsGeometry::fromQgsPoint()
211260
{
212261
QgsPoint point( 1.0, 2.0 );

0 commit comments

Comments
 (0)