Skip to content
Permalink
Browse files

Add method to set QgsFeature geometry directly from QgsAbstractGeometry

Allows

    feat.setGeometry(QgsPoint(1,2))

instead of the more obscure

    feat.setGeometry(QgsGeometry(QgsPoint(1,2)))

(cherry picked from commit e21ea25)
  • Loading branch information
nyalldawson committed Nov 29, 2018
1 parent 054a8a0 commit 257e5e52d021151c6fbcab03c3ee7d2647e53905
@@ -13,7 +13,6 @@




class QgsFeature
{
%Docstring
@@ -329,6 +328,21 @@ Set the feature's geometry. The feature will be valid after.
.. seealso:: :py:func:`clearGeometry`
%End

void setGeometry( QgsAbstractGeometry *geometry /Transfer/ );
%Docstring
Set the feature's ``geometry``. Ownership of the geometry is transferred to the feature.
The feature will be made valid after calling this method.

.. seealso:: :py:func:`geometry`

.. seealso:: :py:func:`clearGeometry`

.. versionadded:: 3.6
%End
%MethodCode
sipCpp->setGeometry( std::unique_ptr< QgsAbstractGeometry>( a0 ) );
%End

void clearGeometry();
%Docstring
Removes any geometry associated with the feature.
@@ -141,6 +141,13 @@ void QgsFeature::setGeometry( const QgsGeometry &geometry )
d->valid = true;
}

void QgsFeature::setGeometry( std::unique_ptr<QgsAbstractGeometry> geometry )
{
d.detach();
d->geometry = QgsGeometry( std::move( geometry ) );
d->valid = true;
}

void QgsFeature::clearGeometry()
{
setGeometry( QgsGeometry() );
@@ -30,12 +30,13 @@ email : sherman at mrcc.com
#include "qgsattributes.h"
#include "qgsfields.h"
#include "qgsfeatureid.h"

#include <memory>
class QgsFeature;
class QgsFeaturePrivate;
class QgsField;
class QgsGeometry;
class QgsRectangle;
class QgsAbstractGeometry;


/***************************************************************************
@@ -345,6 +346,22 @@ class CORE_EXPORT QgsFeature
*/
void setGeometry( const QgsGeometry &geometry );

/**
* Set the feature's \a geometry. Ownership of the geometry is transferred to the feature.
* The feature will be made valid after calling this method.
* \see geometry()
* \see clearGeometry()
* \since QGIS 3.6
*/
#ifndef SIP_RUN
void setGeometry( std::unique_ptr< QgsAbstractGeometry > geometry );
#else
void setGeometry( QgsAbstractGeometry *geometry SIP_TRANSFER );
% MethodCode
sipCpp->setGeometry( std::unique_ptr< QgsAbstractGeometry>( a0 ) );
% End
#endif

/**
* Removes any geometry associated with the feature.
* \see setGeometry()
@@ -306,6 +306,13 @@ void TestQgsFeature::geometry()
QCOMPARE( copy.geometry().asWkb(), geomByRef.asWkb() );
QCOMPARE( feature.geometry().asWkb(), mGeometry.asWkb() );

//setGeometry using abstract geom
copy = feature;
QCOMPARE( copy.geometry().asWkb(), mGeometry.asWkb() );
copy.setGeometry( qgis::make_unique< QgsPoint >( 5, 6 ) );
QCOMPARE( copy.geometry().asWkt(), QStringLiteral( "Point (5 6)" ) );
QCOMPARE( feature.geometry().asWkb(), mGeometry.asWkb() );

//clearGeometry
QgsFeature geomFeature;
geomFeature.setGeometry( QgsGeometry( mGeometry2 ) );
@@ -15,7 +15,14 @@
import qgis # NOQA

import os
from qgis.core import QgsFeature, QgsGeometry, QgsPointXY, QgsVectorLayer, NULL, QgsFields, QgsField
from qgis.core import (QgsFeature,
QgsPoint,
QgsGeometry,
QgsPointXY,
QgsVectorLayer,
NULL,
QgsFields,
QgsField)
from qgis.testing import start_app, unittest
from utilities import unitTestDataPath

@@ -138,6 +145,10 @@ def test_SetGeometry(self):
myMessage = '\nExpected: %s\nGot: %s' % (myExpectedGeometry, myGeometry)
assert myGeometry is not None, myMessage

# set from QgsAbstractGeometry
feat.setGeometry(QgsPoint(12, 34))
self.assertEqual(feat.geometry().asWkt(), 'Point (12 34)')


if __name__ == '__main__':
unittest.main()

0 comments on commit 257e5e5

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