QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
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 = 0 );
182 #endif
183 
189 #ifndef SIP_RUN
190  QgsFeature( const QgsFields &fields, QgsFeatureId id = QgsFeatureId() );
191 #else
192  QgsFeature( const QgsFields &fields, qint64 id = 0 );
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 
222  QgsFeatureId id() const;
223 
229  void setId( QgsFeatureId id );
230 
238  QgsAttributes attributes() const;
239 
247  void setAttributes( const QgsAttributes &attrs );
248 
259 #ifndef SIP_RUN
260  bool setAttribute( int field, const QVariant &attr );
261 #else
262  bool setAttribute( int field, const QVariant &attr / GetWrapper / );
263  % MethodCode
264  bool rv;
265 
266  if ( a1Wrapper == Py_None )
267  {
268  rv = sipCpp->setAttribute( a0, QVariant( QVariant::Int ) );
269  }
270  else
271  {
272  rv = sipCpp->setAttribute( a0, *a1 );
273  }
274 
275  if ( !rv )
276  {
277  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
278  sipIsErr = 1;
279  }
280 
281  sipRes = rv;
282  % End
283 #endif
284 
289  void initAttributes( int fieldCount );
290 
298  void deleteAttribute( int field );
299 #ifdef SIP_RUN
300  % MethodCode
301  if ( a0 >= 0 && a0 < sipCpp->attributes().count() )
302  sipCpp->deleteAttribute( a0 );
303  else
304  {
305  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
306  sipIsErr = 1;
307  }
308  % End
309 #endif
310 
317  bool isValid() const;
318 
324  void setValid( bool validity );
325 
331  bool hasGeometry() const;
332 
339  QgsGeometry geometry() const;
340 
347  void setGeometry( const QgsGeometry &geometry );
348 
376 #ifndef SIP_RUN
377  void setGeometry( std::unique_ptr< QgsAbstractGeometry > geometry );
378 #else
379  void setGeometry( QgsAbstractGeometry *geometry SIP_TRANSFER );
380  % MethodCode
381  sipCpp->setGeometry( std::unique_ptr< QgsAbstractGeometry>( a0 ) );
382  % End
383 #endif
384 
391  void clearGeometry();
392 
402  void setFields( const QgsFields &fields, bool initAttributes = false SIP_PYARGDEFAULT( true ) );
403 
408  QgsFields fields() const;
409 
421 #ifndef SIP_RUN
422  bool setAttribute( const QString &name, const QVariant &value );
423 #else
424  void setAttribute( const QString &name, const QVariant &value / GetWrapper / );
425  % MethodCode
426  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
427  if ( fieldIdx == -1 )
428  {
429  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
430  sipIsErr = 1;
431  }
432  else
433  {
434  if ( a1Wrapper == Py_None )
435  {
436  sipCpp->setAttribute( *a0, QVariant( QVariant::Int ) );
437  }
438  else
439  {
440  sipCpp->setAttribute( fieldIdx, *a1 );
441  }
442  }
443  % End
444 #endif
445 
455  bool deleteAttribute( const QString &name );
456 #ifdef SIP_RUN
457  % MethodCode
458  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
459  if ( fieldIdx == -1 )
460  {
461  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
462  sipIsErr = 1;
463  sipRes = false;
464  }
465  else
466  {
467  sipCpp->deleteAttribute( fieldIdx );
468  sipRes = true;
469  }
470  % End
471 #endif
472 
482 #ifndef SIP_RUN
483  QVariant attribute( const QString &name ) const;
484 #else
485  SIP_PYOBJECT attribute( const QString &name ) const;
486  % MethodCode
487  int fieldIdx = sipCpp->fieldNameIndex( *a0 );
488  if ( fieldIdx == -1 )
489  {
490  PyErr_SetString( PyExc_KeyError, a0->toLatin1() );
491  sipIsErr = 1;
492  }
493  else
494  {
495  QVariant *v = new QVariant( sipCpp->attribute( fieldIdx ) );
496  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
497  }
498  % End
499 #endif
500 
510 #ifndef SIP_RUN
511  QVariant attribute( int fieldIdx ) const;
512 #else
513  SIP_PYOBJECT attribute( int fieldIdx ) const;
514  % MethodCode
515  {
516  if ( a0 < 0 || a0 >= sipCpp->attributes().count() )
517  {
518  PyErr_SetString( PyExc_KeyError, QByteArray::number( a0 ) );
519  sipIsErr = 1;
520  }
521  else
522  {
523  QVariant *v = new QVariant( sipCpp->attribute( a0 ) );
524  sipRes = sipConvertFromNewType( v, sipType_QVariant, Py_None );
525  }
526  }
527  % End
528 #endif
529 
537  int fieldNameIndex( const QString &fieldName ) const;
538 
540  operator QVariant() const
541  {
542  return QVariant::fromValue( *this );
543  }
544 
545  private:
546 
547  QExplicitlySharedDataPointer<QgsFeaturePrivate> d;
548 
549 }; // class QgsFeature
550 
552 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFeature &feature ) SIP_SKIP;
554 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFeature &feature ) SIP_SKIP;
555 
556 // key = feature id, value = changed attributes
557 #ifndef SIP_RUN
558 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
559 #else
560 typedef QMap<qint64, QMap<int, QVariant> > QgsChangedAttributesMap;
561 #endif
562 
563 #include "qgsgeometry.h"
564 
565 // key = feature id, value = changed geometry
566 #ifndef SIP_RUN
567 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
568 #else
569 typedef QMap<qint64, QgsGeometry> QgsGeometryMap;
570 #endif
571 
572 typedef QList<QgsFeature> QgsFeatureList;
573 
574 uint qHash( const QgsFeature &key, uint seed = 0 ) SIP_SKIP;
575 
578 
579 #endif
qgsfields.h
SIP_PYARGDEFAULT
#define SIP_PYARGDEFAULT(value)
Definition: qgis_sip.h:141
operator==
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:399
QgsFields
Definition: qgsfields.h:44
operator<<
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:288
qgsfeatureid.h
QgsChangedAttributesMap
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:558
operator!=
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Definition: qgsfeatureiterator.h:404
QgsRectangle
Definition: qgsrectangle.h:41
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
QgsFeatureList
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:572
qHash
uint qHash(const QgsFeature &key, uint seed=0)
Definition: qgsfeature.cpp:319
QgsAbstractGeometry
Abstract base class for all geometries.
Definition: qgsabstractgeometry.h:71
qgsattributes.h
qgsgeometry.h
QgsGeometry
Definition: qgsgeometry.h:122
QgsAttributes
Definition: qgsattributes.h:57
QgsGeometryMap
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:567
QgsFeature
Definition: qgsfeature.h:55
operator>>
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:305
QgsFeatureId
qint64 QgsFeatureId
Definition: qgsfeatureid.h:25
QgsField
Definition: qgsfield.h:49