Skip to content

Commit

Permalink
Make features valid on setAttribute and setGeometry
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Mar 18, 2017
1 parent 921a0c7 commit 255eb98
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/core/qgsfeature.cpp
Expand Up @@ -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()
Expand Down Expand Up @@ -220,6 +222,7 @@ bool QgsFeature::setAttribute( int idx, const QVariant &value )

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

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

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

Expand Down
5 changes: 4 additions & 1 deletion src/core/qgsfeature.h
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 21 additions & 0 deletions tests/src/python/test_qgsfeature.py
Expand Up @@ -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')
Expand Down

0 comments on commit 255eb98

Please sign in to comment.