QGIS API Documentation  3.0.2-Girona (307d082)
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 
33 class QgsFeature;
34 class QgsFeaturePrivate;
35 class QgsField;
36 class QgsGeometry;
38 
39 
40 /***************************************************************************
41  * This class is considered CRITICAL and any change MUST be accompanied with
42  * full unit tests in testqgsfeature.cpp.
43  * See details in QEP #17
44  ****************************************************************************/
45 
46 // feature id class (currently 64 bit)
47 
48 // 64 bit feature ids
49 typedef qint64 QgsFeatureId SIP_SKIP;
50 #define FID_IS_NEW(fid) (fid<0)
51 #define FID_TO_NUMBER(fid) static_cast<qint64>(fid)
52 #define FID_TO_STRING(fid) QString::number( fid )
53 #define STRING_TO_FID(str) (str).toLongLong()
54 
55 
62 class CORE_EXPORT QgsFeature
63 {
64 #ifdef SIP_RUN
65 #if (SIP_VERSION >= 0x040900 && SIP_VERSION < 0x040c01)
66 #define sipType_QVariant ((sipWrapperType *) sipTypeAsPyTypeObject (sipType_QVariant))
67 #endif
68 #endif
69  Q_GADGET
70 
71  Q_PROPERTY( QgsFeatureId id READ id WRITE setId )
72  Q_PROPERTY( QgsAttributes attributes READ attributes WRITE setAttributes )
73  Q_PROPERTY( QgsFields fields READ fields WRITE setFields )
74 
75  public:
76 
77 #ifdef SIP_RUN
78  SIP_PYOBJECT __iter__();
79  % MethodCode
80  QgsAttributes attributes = sipCpp->attributes();
81  PyObject *attrs = sipConvertFromType( &attributes, sipType_QgsAttributes, Py_None );
82  sipRes = PyObject_GetIter( attrs );
83  % End
84 
85  SIP_PYOBJECT __getitem__( int key );
86  % MethodCode
87  QgsAttributes attrs = sipCpp->attributes();
88  if ( a0 < 0 || a0 >= attrs.count() )
89  {
90  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
91  sipIsErr = 1;
92  }
93  else
94  {
95  QVariant *v = new QVariant( attrs.at( a0 ) );
96  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
97  }
98  % End
99 
100  SIP_PYOBJECT __getitem__( const QString &name );
101  % MethodCode
102  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
103  if ( fieldIdx == -1 )
104  {
105  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
106  sipIsErr = 1;
107  }
108  else
109  {
110  QVariant *v = new QVariant( sipCpp->attribute( fieldIdx ) );
111  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
112  }
113  % End
114 
115  void __setitem__( int key, QVariant value / GetWrapper / );
116  % MethodCode
117  bool rv;
118 
119  if ( a1Wrapper == Py_None )
120  {
121  rv = sipCpp->setAttribute( a0, QVariant( QVariant::Int ) );
122  }
123  else
124  {
125  rv = sipCpp->setAttribute( a0, *a1 );
126  }
127 
128  if ( !rv )
129  {
130  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
131  sipIsErr = 1;
132  }
133  % End
134 
135  void __setitem__( const QString &key, QVariant value / GetWrapper / );
136  % MethodCode
137  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
138  if ( fieldIdx == -1 )
139  {
140  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
141  sipIsErr = 1;
142  }
143  else
144  {
145  if ( a1Wrapper == Py_None )
146  {
147  sipCpp->setAttribute( *a0, QVariant( QVariant::Int ) );
148  }
149  else
150  {
151  sipCpp->setAttribute( fieldIdx, *a1 );
152  }
153  }
154  % End
155 
156  void __delitem__( int key );
157  % MethodCode
158  if ( a0 >= 0 && a0 < sipCpp->attributes().count() )
159  sipCpp->deleteAttribute( a0 );
160  else
161  {
162  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
163  sipIsErr = 1;
164  }
165  % End
166 
167  void __delitem__( const QString &name );
168  % MethodCode
169  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
170  if ( fieldIdx == -1 )
171  {
172  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
173  sipIsErr = 1;
174  }
175  else
176  sipCpp->deleteAttribute( fieldIdx );
177  % End
178 #endif
179 
184 #ifndef SIP_RUN
186 #else
187  QgsFeature( qint64 id = 0 );
188 #endif
189 
195 #ifndef SIP_RUN
196  QgsFeature( const QgsFields &fields, QgsFeatureId id = QgsFeatureId() );
197 #else
198  QgsFeature( const QgsFields &fields, qint64 id = 0 );
199 #endif
200 
204  QgsFeature( const QgsFeature &rhs );
205 
209  QgsFeature &operator=( const QgsFeature &rhs ) SIP_SKIP;
210 
214  bool operator==( const QgsFeature &other ) const SIP_SKIP;
215 
219  bool operator!=( const QgsFeature &other ) const SIP_SKIP;
220 
221  virtual ~QgsFeature();
222 
228  QgsFeatureId id() const;
229 
235  void setId( QgsFeatureId id );
236 
244  QgsAttributes attributes() const;
245 
253  void setAttributes( const QgsAttributes &attrs );
254 
265 #ifndef SIP_RUN
266  bool setAttribute( int field, const QVariant &attr );
267 #else
268  bool setAttribute( int field, const QVariant &attr / GetWrapper / );
269  % MethodCode
270  bool rv;
271 
272  if ( a1Wrapper == Py_None )
273  {
274  rv = sipCpp->setAttribute( a0, QVariant( QVariant::Int ) );
275  }
276  else
277  {
278  rv = sipCpp->setAttribute( a0, *a1 );
279  }
280 
281  if ( !rv )
282  {
283  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
284  sipIsErr = 1;
285  }
286 
287  sipRes = rv;
288  % End
289 #endif
290 
295  void initAttributes( int fieldCount );
296 
304  void deleteAttribute( int field );
305 #ifdef SIP_RUN
306  % MethodCode
307  if ( a0 >= 0 && a0 < sipCpp->attributes().count() )
308  sipCpp->deleteAttribute( a0 );
309  else
310  {
311  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
312  sipIsErr = 1;
313  }
314  % End
315 #endif
316 
323  bool isValid() const;
324 
330  void setValid( bool validity );
331 
337  bool hasGeometry() const;
338 
345  QgsGeometry geometry() const;
346 
353  void setGeometry( const QgsGeometry &geometry );
354 
361  void clearGeometry();
362 
372  void setFields( const QgsFields &fields, bool initAttributes = false SIP_PYARGDEFAULT( true ) );
373 
378  QgsFields fields() const;
379 
391 #ifndef SIP_RUN
392  bool setAttribute( const QString &name, const QVariant &value );
393 #else
394  void setAttribute( const QString &name, const QVariant &value / GetWrapper / );
395  % MethodCode
396  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
397  if ( fieldIdx == -1 )
398  {
399  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
400  sipIsErr = 1;
401  }
402  else
403  {
404  if ( a1Wrapper == Py_None )
405  {
406  sipCpp->setAttribute( *a0, QVariant( QVariant::Int ) );
407  }
408  else
409  {
410  sipCpp->setAttribute( fieldIdx, *a1 );
411  }
412  }
413  % End
414 #endif
415 
425  bool deleteAttribute( const QString &name );
426 #ifdef SIP_RUN
427  % MethodCode
428  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
429  if ( fieldIdx == -1 )
430  {
431  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
432  sipIsErr = 1;
433  sipRes = false;
434  }
435  else
436  {
437  sipCpp->deleteAttribute( fieldIdx );
438  sipRes = true;
439  }
440  % End
441 #endif
442 
452 #ifndef SIP_RUN
453  QVariant attribute( const QString &name ) const;
454 #else
455  SIP_PYOBJECT attribute( const QString &name ) const;
456  % MethodCode
457  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
458  if ( fieldIdx == -1 )
459  {
460  PyErr_SetString( PyExc_KeyError, a0->toAscii() );
461  sipIsErr = 1;
462  }
463  else
464  {
465  QVariant *v = new QVariant( sipCpp->attribute( fieldIdx ) );
466  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
467  }
468  % End
469 #endif
470 
480 #ifndef SIP_RUN
481  QVariant attribute( int fieldIdx ) const;
482 #else
483  SIP_PYOBJECT attribute( int fieldIdx ) const;
484  % MethodCode
485  {
486  if ( a0 < 0 || a0 >= sipCpp->attributes().count() )
487  {
488  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
489  sipIsErr = 1;
490  }
491  else
492  {
493  QVariant *v = new QVariant( sipCpp->attribute( a0 ) );
494  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
495  }
496  }
497  % End
498 #endif
499 
507  int fieldNameIndex( const QString &fieldName ) const;
508 
510  operator QVariant() const
511  {
512  return QVariant::fromValue( *this );
513  }
514 
515  private:
516 
517  QExplicitlySharedDataPointer<QgsFeaturePrivate> d;
518 
519 }; // class QgsFeature
520 
522 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFeature &feature ) SIP_SKIP;
524 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFeature &feature ) SIP_SKIP;
525 
526 // key = feature id, value = changed attributes
527 #ifndef SIP_RUN
528 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
529 #else
530 typedef QMap<qint64, QMap<int, QVariant> > QgsChangedAttributesMap;
531 #endif
532 
533 #include "qgsgeometry.h"
534 
535 // key = feature id, value = changed geometry
536 #ifndef SIP_RUN
537 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
538 #else
539 typedef QMap<qint64, QgsGeometry> QgsGeometryMap;
540 #endif
541 
542 
543 #ifndef SIP_RUN
544 typedef QSet<QgsFeatureId> QgsFeatureIds;
545 #else
546 typedef QSet<qint64> QgsFeatureIds;
547 #endif
548 
549 typedef QList<QgsFeature> QgsFeatureList;
550 
551 uint qHash( const QgsFeature &key, uint seed = 0 ) SIP_SKIP;
552 
554 Q_DECLARE_METATYPE( QgsFeatureList )
555 
556 #endif
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:537
A rectangle specified with double values.
Definition: qgsrectangle.h:39
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:544
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:549
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Container of fields for a vector layer.
Definition: qgsfields.h:42
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:111
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:62
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:528
uint qHash(const QgsFeature &key, uint seed=0)
Definition: qgsfeature.cpp:312
qint64 QgsFeatureId
Definition: qgsfeature.h:37
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:58
#define SIP_PYARGDEFAULT(value)
Definition: qgis_sip.h:134