QGIS API Documentation  3.20.0-Odense (decaadbb31)
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_sip.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 #include <memory>
34 class QgsFeature;
35 class QgsFeaturePrivate;
36 class QgsField;
37 class QgsGeometry;
38 class QgsRectangle;
40 class QgsSymbol;
41 
42 /***************************************************************************
43  * This class is considered CRITICAL and any change MUST be accompanied with
44  * full unit tests in testqgsfeature.cpp.
45  * See details in QEP #17
46  ****************************************************************************/
47 
48 
55 class CORE_EXPORT QgsFeature
56 {
57 #ifdef SIP_RUN
58 #if (SIP_VERSION >= 0x040900 && SIP_VERSION < 0x040c01)
59 #define sipType_QVariant ((sipWrapperType *) sipTypeAsPyTypeObject (sipType_QVariant))
60 #endif
61 #endif
62  Q_GADGET
63 
64  Q_PROPERTY( QgsFeatureId id READ id WRITE setId )
65  Q_PROPERTY( QgsAttributes attributes READ attributes WRITE setAttributes )
66  Q_PROPERTY( QgsFields fields READ fields WRITE setFields )
67  Q_PROPERTY( QgsGeometry geometry READ geometry WRITE setGeometry )
68 
69  public:
70 
71 #ifdef SIP_RUN
72  SIP_PYOBJECT __iter__();
73  % MethodCode
74  QgsAttributes attributes = sipCpp->attributes();
75  PyObject *attrs = sipConvertFromType( &attributes, sipType_QgsAttributes, Py_None );
76  sipRes = PyObject_GetIter( attrs );
77  % End
78 
79  SIP_PYOBJECT __getitem__( int key );
80  % MethodCode
81  QgsAttributes attrs = sipCpp->attributes();
82  if ( a0 < 0 || a0 >= attrs.count() )
83  {
84  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
85  sipIsErr = 1;
86  }
87  else
88  {
89  QVariant *v = new QVariant( attrs.at( a0 ) );
90  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
91  }
92  % End
93 
94  SIP_PYOBJECT __getitem__( const QString &name );
95  % MethodCode
96  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
97  if ( fieldIdx == -1 )
98  {
99  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
100  sipIsErr = 1;
101  }
102  else
103  {
104  QVariant *v = new QVariant( sipCpp->attribute( fieldIdx ) );
105  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
106  }
107  % End
108 
109  void __setitem__( int key, QVariant value / GetWrapper / );
110  % MethodCode
111  bool rv;
112 
113  if ( a1Wrapper == Py_None )
114  {
115  rv = sipCpp->setAttribute( a0, QVariant( QVariant::Int ) );
116  }
117  else
118  {
119  rv = sipCpp->setAttribute( a0, *a1 );
120  }
121 
122  if ( !rv )
123  {
124  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
125  sipIsErr = 1;
126  }
127  % End
128 
129  void __setitem__( const QString &key, QVariant value / GetWrapper / );
130  % MethodCode
131  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
132  if ( fieldIdx == -1 )
133  {
134  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
135  sipIsErr = 1;
136  }
137  else
138  {
139  if ( a1Wrapper == Py_None )
140  {
141  sipCpp->setAttribute( *a0, QVariant( QVariant::Int ) );
142  }
143  else
144  {
145  sipCpp->setAttribute( fieldIdx, *a1 );
146  }
147  }
148  % End
149 
150  void __delitem__( int key );
151  % MethodCode
152  if ( a0 >= 0 && a0 < sipCpp->attributes().count() )
153  sipCpp->deleteAttribute( a0 );
154  else
155  {
156  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
157  sipIsErr = 1;
158  }
159  % End
160 
161  void __delitem__( const QString &name );
162  % MethodCode
163  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
164  if ( fieldIdx == -1 )
165  {
166  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
167  sipIsErr = 1;
168  }
169  else
170  sipCpp->deleteAttribute( fieldIdx );
171  % End
172 #endif
173 
178 #ifndef SIP_RUN
180 #else
181  QgsFeature( qint64 id = FID_NULL );
182 #endif
183 
189 #ifndef SIP_RUN
190  QgsFeature( const QgsFields &fields, QgsFeatureId id = FID_NULL );
191 #else
192  QgsFeature( const QgsFields &fields, qint64 id = FID_NULL );
193 #endif
194 
198  QgsFeature( const QgsFeature &rhs );
199 
203  QgsFeature &operator=( const QgsFeature &rhs ) SIP_SKIP;
204 
208  bool operator==( const QgsFeature &other ) const SIP_SKIP;
209 
213  bool operator!=( const QgsFeature &other ) const SIP_SKIP;
214 
215  virtual ~QgsFeature();
216 
221  QgsFeatureId id() const;
222 
228  void setId( QgsFeatureId id );
229 
236  QgsAttributes attributes() const;
237 
242  int attributeCount() const;
243 
260  void setAttributes( const QgsAttributes &attrs );
261 
262 #ifndef SIP_RUN
263 
274  bool setAttribute( int field, const QVariant &attr );
275 #else
276 
288  bool setAttribute( int field, const QVariant &attr / GetWrapper / );
289  % MethodCode
290  bool rv;
291 
292  if ( a1Wrapper == Py_None )
293  {
294  rv = sipCpp->setAttribute( a0, QVariant( QVariant::Int ) );
295  }
296  else
297  {
298  rv = sipCpp->setAttribute( a0, *a1 );
299  }
300 
301  if ( !rv )
302  {
303  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
304  sipIsErr = 1;
305  }
306 
307  sipRes = rv;
308  % End
309 #endif
310 
320  void initAttributes( int fieldCount );
321 
335  void resizeAttributes( int fieldCount );
336 
343  void padAttributes( int count );
344 
345 #ifndef SIP_RUN
346 
354  void deleteAttribute( int field );
355 #else
356 
366  void deleteAttribute( int field );
367  % MethodCode
368  if ( a0 >= 0 && a0 < sipCpp->attributes().count() )
369  sipCpp->deleteAttribute( a0 );
370  else
371  {
372  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
373  sipIsErr = 1;
374  }
375  % End
376 #endif
377 
386  bool isValid() const;
387 
395  void setValid( bool validity );
396 
402  bool hasGeometry() const;
403 
410  QgsGeometry geometry() const;
411 
421  void setGeometry( const QgsGeometry &geometry );
422 
453 #ifndef SIP_RUN
454  void setGeometry( std::unique_ptr< QgsAbstractGeometry > geometry );
455 #else
456  void setGeometry( QgsAbstractGeometry *geometry SIP_TRANSFER );
457  % MethodCode
458  sipCpp->setGeometry( std::unique_ptr< QgsAbstractGeometry>( a0 ) );
459  % End
460 #endif
461 
468  void clearGeometry();
469 
477  void setFields( const QgsFields &fields, bool initAttributes = false SIP_PYARGDEFAULT( true ) );
478 
483  QgsFields fields() const;
484 
485 #ifndef SIP_RUN
486 
501  bool setAttribute( const QString &name, const QVariant &value );
502 #else
503 
519  void setAttribute( const QString &name, const QVariant &value / GetWrapper / );
520  % MethodCode
521  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
522  if ( fieldIdx == -1 )
523  {
524  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
525  sipIsErr = 1;
526  }
527  else
528  {
529  if ( a1Wrapper == Py_None )
530  {
531  sipCpp->setAttribute( *a0, QVariant( QVariant::Int ) );
532  }
533  else
534  {
535  sipCpp->setAttribute( fieldIdx, *a1 );
536  }
537  }
538  % End
539 #endif
540 
541 #ifndef SIP_RUN
542 
552  bool deleteAttribute( const QString &name );
553 #else
554 
565  bool deleteAttribute( const QString &name );
566  % MethodCode
567  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
568  if ( fieldIdx == -1 )
569  {
570  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
571  sipIsErr = 1;
572  sipRes = false;
573  }
574  else
575  {
576  sipCpp->deleteAttribute( fieldIdx );
577  sipRes = true;
578  }
579  % End
580 #endif
581 
582 #ifndef SIP_RUN
583 
593  QVariant attribute( const QString &name ) const;
594 #else
595 
607  SIP_PYOBJECT attribute( const QString &name ) const;
608  % MethodCode
609  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
610  if ( fieldIdx == -1 )
611  {
612  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
613  sipIsErr = 1;
614  }
615  else
616  {
617  QVariant *v = new QVariant( sipCpp->attribute( fieldIdx ) );
618  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
619  }
620  % End
621 #endif
622 
623 #ifndef SIP_RUN
624 
634  QVariant attribute( int fieldIdx ) const;
635 #else
636 
648  SIP_PYOBJECT attribute( int fieldIdx ) const;
649  % MethodCode
650  {
651  if ( a0 < 0 || a0 >= sipCpp->attributes().count() )
652  {
653  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
654  sipIsErr = 1;
655  }
656  else
657  {
658  QVariant *v = new QVariant( sipCpp->attribute( a0 ) );
659  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
660  }
661  }
662  % End
663 #endif
664 
670  const QgsSymbol *embeddedSymbol() const;
671 
679  void setEmbeddedSymbol( QgsSymbol *symbol SIP_TRANSFER );
680 
690  int fieldNameIndex( const QString &fieldName ) const;
691 
701  int approximateMemoryUsage() const;
702 
704  operator QVariant() const
705  {
706  return QVariant::fromValue( *this );
707  }
708 
709  private:
710 
711  QExplicitlySharedDataPointer<QgsFeaturePrivate> d;
712 
713 }; // class QgsFeature
714 
716 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFeature &feature ) SIP_SKIP;
718 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFeature &feature ) SIP_SKIP;
719 
720 // key = feature id, value = changed attributes
721 #ifndef SIP_RUN
722 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
723 #else
724 typedef QMap<qint64, QMap<int, QVariant> > QgsChangedAttributesMap;
725 #endif
726 
727 #include "qgsgeometry.h"
728 
729 // key = feature id, value = changed geometry
730 #ifndef SIP_RUN
731 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
732 #else
733 typedef QMap<qint64, QgsGeometry> QgsGeometryMap;
734 #endif
735 
736 typedef QList<QgsFeature> QgsFeatureList;
737 
738 uint qHash( const QgsFeature &key, uint seed = 0 ) SIP_SKIP;
739 
742 
743 #endif
Abstract base class for all geometries.
A vector of attributes.
Definition: qgsattributes.h:58
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition: qgsfeature.h:56
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:51
Container of fields for a vector layer.
Definition: qgsfields.h:45
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:124
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Abstract base class for all rendered symbols.
Definition: qgssymbol.h:38
#define SIP_PYARGDEFAULT(value)
Definition: qgis_sip.h:141
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:731
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:397
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:722
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:380
uint qHash(const QgsFeature &key, uint seed=0)
Definition: qgsfeature.cpp:411
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:736
#define FID_NULL
Definition: qgsfeatureid.h:29
qint64 QgsFeatureId
64 bit feature ids negative numbers are used for uncommitted/newly added features
Definition: qgsfeatureid.h:28
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
const QgsField & field
Definition: qgsfield.h:463
Q_DECLARE_METATYPE(QgsMeshTimeSettings)