Skip to content

Commit d2a2b18

Browse files
committedFeb 14, 2017
QgsFeature: add pythonic notes to api doc and add QgsFeature.attribute(fieldIdx) binding
1 parent 65e2dda commit d2a2b18

File tree

2 files changed

+45
-5
lines changed

2 files changed

+45
-5
lines changed
 

‎python/core/qgsfeature.sip

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,11 @@ class QgsFeature
232232
*/
233233
QgsFeature( const QgsFeature & rhs );
234234

235-
//! Destructor
236235
~QgsFeature();
237236

238237
/** Get the feature ID for this feature.
239238
* @returns feature ID
240-
* @see setFeatureId
239+
* @see setId()
241240
*/
242241
QgsFeatureId id() const;
243242

@@ -252,6 +251,7 @@ class QgsFeature
252251
* @returns list of feature's attributes
253252
* @see setAttributes
254253
* @note added in QGIS 2.9
254+
* @note Alternatively in Python: iterate feature, eg. @code [attr for attr in feature] @endcode
255255
*/
256256
QgsAttributes attributes() const;
257257

@@ -267,6 +267,7 @@ class QgsFeature
267267
* @param attr the value of the attribute
268268
* @return false, if the field index does not exist
269269
* @note For Python: raises a KeyError exception instead of returning false
270+
* @note Alternatively in Python: @code feature[field] = attr @endcode
270271
* @see setAttributes
271272
*/
272273
bool setAttribute( int field, const QVariant& attr /GetWrapper/);
@@ -300,6 +301,7 @@ class QgsFeature
300301
* @param field the index of the field
301302
* @see setAttribute
302303
* @note For Python: raises a KeyError exception if the field is not found
304+
* @note Alternatively in Python: @code del feature[field] @endcode
303305
*/
304306
void deleteAttribute( int field );
305307
%MethodCode
@@ -311,6 +313,7 @@ class QgsFeature
311313
sipIsErr = 1;
312314
}
313315
%End
316+
314317
/** Returns the validity of this feature. This is normally set by
315318
* the provider to indicate some problem that makes the feature
316319
* invalid or to indicate a null feature.
@@ -372,6 +375,7 @@ class QgsFeature
372375
* @param value The value to set
373376
* @return false if attribute name could not be converted to index (C++ only)
374377
* @note For Python: raises a KeyError exception instead of returning false
378+
* @note Alternatively in Python: @code feature[name] = attr @endcode
375379
* @see setFields
376380
*/
377381
void setAttribute( const QString& name, const QVariant& value /GetWrapper/ );
@@ -394,11 +398,13 @@ class QgsFeature
394398
}
395399
}
396400
%End
401+
397402
/** Removes an attribute value by field name. Field map must be associated using @link setFields @endlink
398403
* before this method can be used.
399404
* @param name The name of the field to delete
400405
* @return false if attribute name could not be converted to index (C++ only)
401406
* @note For Python: raises a KeyError exception instead of returning false
407+
* @note Alternatively in Python: @code del feature[name] @endcode
402408
* @see setFields
403409
*/
404410
bool deleteAttribute( const QString& name );
@@ -416,11 +422,13 @@ class QgsFeature
416422
sipRes = true;
417423
}
418424
%End
425+
419426
/** Lookup attribute value from attribute name. Field map must be associated using @link setFields @endlink
420427
* before this method can be used.
421428
* @param name The name of the attribute to get
422429
* @return The value of the attribute (C++: Invalid variant if no such name exists )
423-
* @note For Python: raises a KeyError exception if field is not found
430+
* @note For Python: raises a KeyError exception if the field is not found
431+
* @note Alternatively in Python: @code feature[name] @endcode
424432
* @see setFields
425433
*/
426434
SIP_PYOBJECT attribute( const QString& name ) const;
@@ -437,6 +445,31 @@ class QgsFeature
437445
sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
438446
}
439447
%End
448+
449+
/** Lookup attribute value from its index. Field map must be associated using @link setFields @endlink
450+
* before this method can be used.
451+
* @param fieldIdx The index of the attribute to get
452+
* @return The value of the attribute (C++: Invalid variant if no such index exists )
453+
* @note For Python: raises a KeyError exception if the field is not found
454+
* @note Alternatively in Python: @code feature[fieldIdx] @endcode
455+
* @see setFields
456+
*/
457+
SIP_PYOBJECT attribute( int fieldIdx ) const;
458+
%MethodCode
459+
{
460+
if (a0 < 0 || a0 >= sipCpp->attributes().count())
461+
{
462+
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
463+
sipIsErr = 1;
464+
}
465+
else
466+
{
467+
QVariant* v = new QVariant( sipCpp->attribute(a0) );
468+
sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
469+
}
470+
}
471+
%End
472+
440473
/** Utility method to get attribute index from name. Field map must be associated using @link setFields @endlink
441474
* before this method can be used.
442475
* @param fieldName name of field to get attribute index of

‎src/core/qgsfeature.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ class CORE_EXPORT QgsFeature
191191
* @returns list of feature's attributes
192192
* @see setAttributes
193193
* @note added in QGIS 2.9
194+
* @note Alternatively in Python: iterate feature, eg. @code [attr for attr in feature] @endcode
194195
*/
195196
QgsAttributes attributes() const;
196197

@@ -206,6 +207,7 @@ class CORE_EXPORT QgsFeature
206207
* @param attr the value of the attribute
207208
* @return false, if the field index does not exist
208209
* @note For Python: raises a KeyError exception instead of returning false
210+
* @note Alternatively in Python: @code feature[field] = attr @endcode
209211
* @see setAttributes
210212
*/
211213
bool setAttribute( int field, const QVariant& attr );
@@ -219,6 +221,7 @@ class CORE_EXPORT QgsFeature
219221
* @param field the index of the field
220222
* @see setAttribute
221223
* @note For Python: raises a KeyError exception if the field is not found
224+
* @note Alternatively in Python: @code del feature[field] @endcode
222225
*/
223226
void deleteAttribute( int field );
224227

@@ -283,6 +286,7 @@ class CORE_EXPORT QgsFeature
283286
* @param value The value to set
284287
* @return false if attribute name could not be converted to index (C++ only)
285288
* @note For Python: raises a KeyError exception instead of returning false
289+
* @note Alternatively in Python: @code feature[name] = attr @endcode
286290
* @see setFields
287291
*/
288292
bool setAttribute( const QString& name, const QVariant& value );
@@ -292,6 +296,7 @@ class CORE_EXPORT QgsFeature
292296
* @param name The name of the field to delete
293297
* @return false if attribute name could not be converted to index (C++ only)
294298
* @note For Python: raises a KeyError exception instead of returning false
299+
* @note Alternatively in Python: @code del feature[name] @endcode
295300
* @see setFields
296301
*/
297302
bool deleteAttribute( const QString& name );
@@ -300,7 +305,8 @@ class CORE_EXPORT QgsFeature
300305
* before this method can be used.
301306
* @param name The name of the attribute to get
302307
* @return The value of the attribute (C++: Invalid variant if no such name exists )
303-
* @note For Python: raises a KeyError exception if field is not found
308+
* @note For Python: raises a KeyError exception if the field is not found
309+
* @note Alternatively in Python: @code feature[name] @endcode
304310
* @see setFields
305311
*/
306312
QVariant attribute( const QString& name ) const;
@@ -309,7 +315,8 @@ class CORE_EXPORT QgsFeature
309315
* before this method can be used.
310316
* @param fieldIdx The index of the attribute to get
311317
* @return The value of the attribute (C++: Invalid variant if no such index exists )
312-
* @note For Python: raises a KeyError exception if field is not found
318+
* @note For Python: raises a KeyError exception if the field is not found
319+
* @note Alternatively in Python: @code feature[fieldIdx] @endcode
313320
* @see setFields
314321
*/
315322
QVariant attribute( int fieldIdx ) const;

0 commit comments

Comments
 (0)
Please sign in to comment.