Skip to content
Permalink
Browse files
Make features valid on setAttribute and setGeometry
  • Loading branch information
m-kuhn committed Mar 18, 2017
1 parent 921a0c7 commit 255eb98be61d2a44f8e2f9546887172323563f5d
Showing with 29 additions and 1 deletion.
  1. +4 −0 src/core/qgsfeature.cpp
  2. +4 −1 src/core/qgsfeature.h
  3. +21 −0 tests/src/python/test_qgsfeature.py
@@ -142,12 +142,14 @@ void QgsFeature::setAttributes( const QgsAttributes &attrs )

d.detach();
d->attributes = attrs;
d->valid = true;
}

void QgsFeature::setGeometry( const QgsGeometry &geometry )
{
d.detach();
d->geometry = geometry;
d->valid = true;
}

void QgsFeature::clearGeometry()
@@ -220,6 +222,7 @@ bool QgsFeature::setAttribute( int idx, const QVariant &value )

d.detach();
d->attributes[idx] = value;
d->valid = true;
return true;
}

@@ -237,6 +240,7 @@ bool QgsFeature::setAttribute( const QString &name, const QVariant &value )

d.detach();
d->attributes[fieldIdx] = value;
d->valid = true;
return true;
}

@@ -196,13 +196,15 @@ class CORE_EXPORT QgsFeature
QgsAttributes attributes() const;

/** Sets the feature's attributes.
* The feature will be valid after.
* @param attrs attribute list
* @see setAttribute
* @see attributes
*/
void setAttributes( const QgsAttributes &attrs );

/** Set an attribute's value by field index.
* The feature will be valid if it was successful.
* @param field the index of the field to set
* @param attr the value of the attribute
* @return false, if the field index does not exist
@@ -251,7 +253,7 @@ class CORE_EXPORT QgsFeature
*/
QgsGeometry geometry() const;

/** Set the feature's geometry.
/** Set the feature's geometry. The feature will be valid after.
* @param geometry new feature geometry
* @see geometry()
* @see clearGeometry()
@@ -282,6 +284,7 @@ class CORE_EXPORT QgsFeature

/** Insert a value into attribute. Returns false if attribute name could not be converted to index.
* Field map must be associated using @link setFields @endlink before this method can be used.
* The feature will be valid if it was successful
* @param name The name of the field to set
* @param value The value to set
* @return false if attribute name could not be converted to index (C++ only)
@@ -46,6 +46,27 @@ def test_ValidFeature(self):
myMessage = '\nExpected: %s\nGot: %s' % ("True", myValidValue)
assert myValidValue, myMessage

def test_Validity(self):
f = QgsFeature()
self.assertFalse(f.isValid())
f.setGeometry(QgsGeometry())
self.assertTrue(f.isValid())
f.setValid(False)
self.assertFalse(f.isValid())
fields = QgsFields()
field1 = QgsField('my_field')
fields.append(field1)
field2 = QgsField('my_field2')
fields.append(field2)
f.setFields(fields)
f.setAttribute(0, 0)
self.assertTrue(f.isValid())
f.setValid(False)
self.assertFalse(f.isValid())

f.setValid(False)
self.assertFalse(f.isValid())

def test_Attributes(self):
myPath = os.path.join(unitTestDataPath(), 'lines.shp')
myLayer = QgsVectorLayer(myPath, 'Lines', 'ogr')

0 comments on commit 255eb98

Please sign in to comment.