QGIS API Documentation  2.99.0-Master (0a63d1f)
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 <QMap>
21 #include <QString>
22 #include <QVariant>
23 #include <QList>
24 #include <QVector>
25 #include <QSet>
26 #include <QExplicitlySharedDataPointer>
27 
28 #include "qgsfields.h"
29 
30 class QgsGeometry;
31 class QgsRectangle;
32 class QgsFeature;
33 class QgsFeaturePrivate;
34 
35 // feature id class (currently 64 bit)
36 
37 // 64 bit feature ids
38 typedef qint64 QgsFeatureId;
39 #define FID_IS_NEW(fid) (fid<0)
40 #define FID_TO_NUMBER(fid) static_cast<qint64>(fid)
41 #define FID_TO_STRING(fid) QString::number( fid )
42 #define STRING_TO_FID(str) (str).toLongLong()
43 
44 // key = field index, value = field value
45 typedef QMap<int, QVariant> QgsAttributeMap;
46 
47 /***************************************************************************
48  * This class is considered CRITICAL and any change MUST be accompanied with
49  * full unit tests in testqgsfeature.cpp.
50  * See details in QEP #17
51  ****************************************************************************/
52 
56 class CORE_EXPORT QgsAttributes : public QVector<QVariant>
57 {
58  public:
60  : QVector<QVariant>()
61  {}
62 
68  QgsAttributes( int size )
69  : QVector<QVariant>( size )
70  {}
71 
77  QgsAttributes( int size, const QVariant& v )
78  : QVector<QVariant>( size, v )
79  {}
80 
85  QgsAttributes( const QVector<QVariant>& v )
86  : QVector<QVariant>( v )
87  {}
88 
98  bool operator==( const QgsAttributes &v ) const
99  {
100  if ( size() != v.size() )
101  return false;
102  const QVariant* b = constData();
103  const QVariant* i = b + size();
104  const QVariant* j = v.constData() + size();
105  while ( i != b )
106  if ( !( *--i == *--j && i->isNull() == j->isNull() ) )
107  return false;
108  return true;
109  }
110 
117  QgsAttributeMap toMap() const;
118 
119  inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
120 };
121 
122 class QgsField;
123 
124 /***************************************************************************
125  * This class is considered CRITICAL and any change MUST be accompanied with
126  * full unit tests in testqgsfeature.cpp.
127  * See details in QEP #17
128  ****************************************************************************/
129 
136 class CORE_EXPORT QgsFeature
137 {
138  Q_GADGET
139 
140  Q_PROPERTY( QgsFeatureId id READ id WRITE setId )
141  Q_PROPERTY( QgsAttributes attributes READ attributes WRITE setAttributes )
142  Q_PROPERTY( QgsFields fields READ fields WRITE setFields )
143 
144  public:
145 
150 
155  QgsFeature( const QgsFields& fields, QgsFeatureId id = QgsFeatureId() );
156 
159  QgsFeature( const QgsFeature& rhs );
160 
163  QgsFeature& operator=( const QgsFeature& rhs );
164 
168  bool operator==( const QgsFeature& other ) const;
169 
173  bool operator!=( const QgsFeature& other ) const;
174 
175  virtual ~QgsFeature();
176 
181  QgsFeatureId id() const;
182 
187  void setId( QgsFeatureId id );
188 
195  QgsAttributes attributes() const;
196 
202  void setAttributes( const QgsAttributes& attrs );
203 
211  bool setAttribute( int field, const QVariant& attr );
212 
216  void initAttributes( int fieldCount );
217 
223  void deleteAttribute( int field );
224 
230  bool isValid() const;
231 
236  void setValid( bool validity );
237 
242  bool hasGeometry() const;
243 
249  QgsGeometry geometry() const;
250 
256  void setGeometry( const QgsGeometry& geometry );
257 
263  void clearGeometry();
264 
273  void setFields( const QgsFields& fields, bool initAttributes = false );
274 
278  QgsFields fields() const;
279 
288  bool setAttribute( const QString& name, const QVariant& value );
289 
297  bool deleteAttribute( const QString& name );
298 
306  QVariant attribute( const QString& name ) const;
307 
315  QVariant attribute( int fieldIdx ) const;
316 
323  int fieldNameIndex( const QString& fieldName ) const;
324 
326  operator QVariant() const
327  {
328  return QVariant::fromValue( *this );
329  }
330 
331  private:
332 
333  QExplicitlySharedDataPointer<QgsFeaturePrivate> d;
334 
335 }; // class QgsFeature
336 
338 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFeature& feature );
340 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFeature& feature );
341 
342 // key = feature id, value = changed attributes
343 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
344 
345 // key = feature id, value = changed geometry
346 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
347 
348 typedef QSet<QgsFeatureId> QgsFeatureIds;
349 
350 // key = field index, value = field name
351 typedef QMap<int, QString> QgsFieldNameMap;
352 
353 typedef QList<QgsFeature> QgsFeatureList;
354 
355 uint qHash( const QgsFeature& key, uint seed = 0 );
356 
359 
360 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:351
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:346
A rectangle specified with double values.
Definition: qgsrectangle.h:36
QMap< int, QVariant > QgsAttributeMap
Definition: qgsfeature.h:45
QgsAttributes(const QVector< QVariant > &v)
Copies another vector of attributes.
Definition: qgsfeature.h:85
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:348
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:353
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
Container of fields for a vector layer.
Definition: qgsfields.h:39
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:79
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:136
void seed(uint32_t value)
QgsAttributes(int size, const QVariant &v)
Constructs a vector with an initial size of size elements.
Definition: qgsfeature.h:77
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:45
QgsAttributes(int size)
Create a new vector of attributes with the given size.
Definition: qgsfeature.h:68
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:343
uint qHash(const QgsFeature &key, uint seed=0)
Definition: qgsfeature.cpp:319
qint64 QgsFeatureId
Definition: qgsfeature.h:33
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
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
bool operator!=(const QgsAttributes &v) const
Definition: qgsfeature.h:119
A vector of attributes.
Definition: qgsfeature.h:56
bool operator==(const QgsAttributes &v) const
Compares two vectors of attributes.
Definition: qgsfeature.h:98