QGIS API Documentation  3.3.0-Master (208571a)
qgsfeature.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeature.h - Spatial Feature Class
3  --------------------------------------
4 Date : 09-Sep-2003
5 Copyright : (C) 2003 by Gary E.Sherman
6 email : sherman at mrcc.com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSFEATURE_H
17 #define QGSFEATURE_H
18 
19 #include "qgis_core.h"
20 #include "qgis.h"
21 
22 #include <QExplicitlySharedDataPointer>
23 #include <QList>
24 #include <QMap>
25 #include <QSet>
26 #include <QString>
27 #include <QVariant>
28 #include <QVector>
29 
30 #include "qgsattributes.h"
31 #include "qgsfields.h"
32 #include "qgsfeatureid.h"
33 
34 class QgsFeature;
35 class QgsFeaturePrivate;
36 class QgsField;
37 class QgsGeometry;
38 class QgsRectangle;
39 
40 
41 /***************************************************************************
42  * This class is considered CRITICAL and any change MUST be accompanied with
43  * full unit tests in testqgsfeature.cpp.
44  * See details in QEP #17
45  ****************************************************************************/
46 
47 
54 class CORE_EXPORT QgsFeature
55 {
56 #ifdef SIP_RUN
57 #if (SIP_VERSION >= 0x040900 && SIP_VERSION < 0x040c01)
58 #define sipType_QVariant ((sipWrapperType *) sipTypeAsPyTypeObject (sipType_QVariant))
59 #endif
60 #endif
61  Q_GADGET
62 
63  Q_PROPERTY( QgsFeatureId id READ id WRITE setId )
64  Q_PROPERTY( QgsAttributes attributes READ attributes WRITE setAttributes )
65  Q_PROPERTY( QgsFields fields READ fields WRITE setFields )
66  Q_PROPERTY( QgsGeometry geometry READ geometry WRITE setGeometry )
67 
68  public:
69 
70 #ifdef SIP_RUN
71  SIP_PYOBJECT __iter__();
72  % MethodCode
73  QgsAttributes attributes = sipCpp->attributes();
74  PyObject *attrs = sipConvertFromType( &attributes, sipType_QgsAttributes, Py_None );
75  sipRes = PyObject_GetIter( attrs );
76  % End
77 
78  SIP_PYOBJECT __getitem__( int key );
79  % MethodCode
80  QgsAttributes attrs = sipCpp->attributes();
81  if ( a0 < 0 || a0 >= attrs.count() )
82  {
83  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
84  sipIsErr = 1;
85  }
86  else
87  {
88  QVariant *v = new QVariant( attrs.at( a0 ) );
89  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
90  }
91  % End
92 
93  SIP_PYOBJECT __getitem__( const QString &name );
94  % MethodCode
95  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
96  if ( fieldIdx == -1 )
97  {
98  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
99  sipIsErr = 1;
100  }
101  else
102  {
103  QVariant *v = new QVariant( sipCpp->attribute( fieldIdx ) );
104  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
105  }
106  % End
107 
108  void __setitem__( int key, QVariant value / GetWrapper / );
109  % MethodCode
110  bool rv;
111 
112  if ( a1Wrapper == Py_None )
113  {
114  rv = sipCpp->setAttribute( a0, QVariant( QVariant::Int ) );
115  }
116  else
117  {
118  rv = sipCpp->setAttribute( a0, *a1 );
119  }
120 
121  if ( !rv )
122  {
123  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
124  sipIsErr = 1;
125  }
126  % End
127 
128  void __setitem__( const QString &key, QVariant value / GetWrapper / );
129  % MethodCode
130  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
131  if ( fieldIdx == -1 )
132  {
133  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
134  sipIsErr = 1;
135  }
136  else
137  {
138  if ( a1Wrapper == Py_None )
139  {
140  sipCpp->setAttribute( *a0, QVariant( QVariant::Int ) );
141  }
142  else
143  {
144  sipCpp->setAttribute( fieldIdx, *a1 );
145  }
146  }
147  % End
148 
149  void __delitem__( int key );
150  % MethodCode
151  if ( a0 >= 0 && a0 < sipCpp->attributes().count() )
152  sipCpp->deleteAttribute( a0 );
153  else
154  {
155  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
156  sipIsErr = 1;
157  }
158  % End
159 
160  void __delitem__( const QString &name );
161  % MethodCode
162  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
163  if ( fieldIdx == -1 )
164  {
165  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
166  sipIsErr = 1;
167  }
168  else
169  sipCpp->deleteAttribute( fieldIdx );
170  % End
171 #endif
172 
177 #ifndef SIP_RUN
179 #else
180  QgsFeature( qint64 id = 0 );
181 #endif
182 
188 #ifndef SIP_RUN
189  QgsFeature( const QgsFields &fields, QgsFeatureId id = QgsFeatureId() );
190 #else
191  QgsFeature( const QgsFields &fields, qint64 id = 0 );
192 #endif
193 
197  QgsFeature( const QgsFeature &rhs );
198 
202  QgsFeature &operator=( const QgsFeature &rhs ) SIP_SKIP;
203 
207  bool operator==( const QgsFeature &other ) const SIP_SKIP;
208 
212  bool operator!=( const QgsFeature &other ) const SIP_SKIP;
213 
214  virtual ~QgsFeature();
215 
221  QgsFeatureId id() const;
222 
228  void setId( QgsFeatureId id );
229 
237  QgsAttributes attributes() const;
238 
246  void setAttributes( const QgsAttributes &attrs );
247 
258 #ifndef SIP_RUN
259  bool setAttribute( int field, const QVariant &attr );
260 #else
261  bool setAttribute( int field, const QVariant &attr / GetWrapper / );
262  % MethodCode
263  bool rv;
264 
265  if ( a1Wrapper == Py_None )
266  {
267  rv = sipCpp->setAttribute( a0, QVariant( QVariant::Int ) );
268  }
269  else
270  {
271  rv = sipCpp->setAttribute( a0, *a1 );
272  }
273 
274  if ( !rv )
275  {
276  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
277  sipIsErr = 1;
278  }
279 
280  sipRes = rv;
281  % End
282 #endif
283 
288  void initAttributes( int fieldCount );
289 
297  void deleteAttribute( int field );
298 #ifdef SIP_RUN
299  % MethodCode
300  if ( a0 >= 0 && a0 < sipCpp->attributes().count() )
301  sipCpp->deleteAttribute( a0 );
302  else
303  {
304  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
305  sipIsErr = 1;
306  }
307  % End
308 #endif
309 
316  bool isValid() const;
317 
323  void setValid( bool validity );
324 
330  bool hasGeometry() const;
331 
338  QgsGeometry geometry() const;
339 
346  void setGeometry( const QgsGeometry &geometry );
347 
354  void clearGeometry();
355 
365  void setFields( const QgsFields &fields, bool initAttributes = false SIP_PYARGDEFAULT( true ) );
366 
371  QgsFields fields() const;
372 
384 #ifndef SIP_RUN
385  bool setAttribute( const QString &name, const QVariant &value );
386 #else
387  void setAttribute( const QString &name, const QVariant &value / GetWrapper / );
388  % MethodCode
389  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
390  if ( fieldIdx == -1 )
391  {
392  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
393  sipIsErr = 1;
394  }
395  else
396  {
397  if ( a1Wrapper == Py_None )
398  {
399  sipCpp->setAttribute( *a0, QVariant( QVariant::Int ) );
400  }
401  else
402  {
403  sipCpp->setAttribute( fieldIdx, *a1 );
404  }
405  }
406  % End
407 #endif
408 
418  bool deleteAttribute( const QString &name );
419 #ifdef SIP_RUN
420  % MethodCode
421  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
422  if ( fieldIdx == -1 )
423  {
424  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
425  sipIsErr = 1;
426  sipRes = false;
427  }
428  else
429  {
430  sipCpp->deleteAttribute( fieldIdx );
431  sipRes = true;
432  }
433  % End
434 #endif
435 
445 #ifndef SIP_RUN
446  QVariant attribute( const QString &name ) const;
447 #else
448  SIP_PYOBJECT attribute( const QString &name ) const;
449  % MethodCode
450  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
451  if ( fieldIdx == -1 )
452  {
453  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
454  sipIsErr = 1;
455  }
456  else
457  {
458  QVariant *v = new QVariant( sipCpp->attribute( fieldIdx ) );
459  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
460  }
461  % End
462 #endif
463 
473 #ifndef SIP_RUN
474  QVariant attribute( int fieldIdx ) const;
475 #else
476  SIP_PYOBJECT attribute( int fieldIdx ) const;
477  % MethodCode
478  {
479  if ( a0 < 0 || a0 >= sipCpp->attributes().count() )
480  {
481  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
482  sipIsErr = 1;
483  }
484  else
485  {
486  QVariant *v = new QVariant( sipCpp->attribute( a0 ) );
487  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
488  }
489  }
490  % End
491 #endif
492 
500  int fieldNameIndex( const QString &fieldName ) const;
501 
503  operator QVariant() const
504  {
505  return QVariant::fromValue( *this );
506  }
507 
508  private:
509 
510  QExplicitlySharedDataPointer<QgsFeaturePrivate> d;
511 
512 }; // class QgsFeature
513 
515 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFeature &feature ) SIP_SKIP;
517 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFeature &feature ) SIP_SKIP;
518 
519 // key = feature id, value = changed attributes
520 #ifndef SIP_RUN
521 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
522 #else
523 typedef QMap<qint64, QMap<int, QVariant> > QgsChangedAttributesMap;
524 #endif
525 
526 #include "qgsgeometry.h"
527 
528 // key = feature id, value = changed geometry
529 #ifndef SIP_RUN
530 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
531 #else
532 typedef QMap<qint64, QgsGeometry> QgsGeometryMap;
533 #endif
534 
535 typedef QList<QgsFeature> QgsFeatureList;
536 
537 uint qHash( const QgsFeature &key, uint seed = 0 ) SIP_SKIP;
538 
540 Q_DECLARE_METATYPE( QgsFeatureList )
541 
542 #endif
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:530
A rectangle specified with double values.
Definition: qgsrectangle.h:40
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:535
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:104
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:54
Q_DECLARE_METATYPE(QModelIndex)
#define SIP_SKIP
Definition: qgis_sip.h:119
void seed(uint32_t value)
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:521
uint qHash(const QgsFeature &key, uint seed=0)
Definition: qgsfeature.cpp:312
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsFeature &feature)
Reads a feature from stream in into feature. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:298
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:281
A vector of attributes.
Definition: qgsattributes.h:57
#define SIP_PYARGDEFAULT(value)
Definition: qgis_sip.h:134