QGIS API Documentation  2.99.0-Master (314842d)
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 
196  QgsAttributes attributes() const;
197 
204  void setAttributes( const QgsAttributes &attrs );
205 
215  bool setAttribute( int field, const QVariant &attr );
216 
220  void initAttributes( int fieldCount );
221 
228  void deleteAttribute( int field );
229 
235  bool isValid() const;
236 
241  void setValid( bool validity );
242 
247  bool hasGeometry() const;
248 
254  QgsGeometry geometry() const;
255 
261  void setGeometry( const QgsGeometry &geometry );
262 
268  void clearGeometry();
269 
278  void setFields( const QgsFields &fields, bool initAttributes = false );
279 
283  QgsFields fields() const;
284 
295  bool setAttribute( const QString &name, const QVariant &value );
296 
305  bool deleteAttribute( const QString &name );
306 
315  QVariant attribute( const QString &name ) const;
316 
325  QVariant attribute( int fieldIdx ) const;
326 
333  int fieldNameIndex( const QString &fieldName ) const;
334 
336  operator QVariant() const
337  {
338  return QVariant::fromValue( *this );
339  }
340 
341  private:
342 
343  QExplicitlySharedDataPointer<QgsFeaturePrivate> d;
344 
345 }; // class QgsFeature
346 
348 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsFeature &feature );
350 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsFeature &feature );
351 
352 // key = feature id, value = changed attributes
353 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
354 
355 // key = feature id, value = changed geometry
356 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
357 
358 typedef QSet<QgsFeatureId> QgsFeatureIds;
359 
360 // key = field index, value = field name
361 typedef QMap<int, QString> QgsFieldNameMap;
362 
363 typedef QList<QgsFeature> QgsFeatureList;
364 
365 uint qHash( const QgsFeature &key, uint seed = 0 );
366 
369 
370 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:361
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:356
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:358
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:363
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:353
uint qHash(const QgsFeature &key, uint seed=0)
Definition: qgsfeature.cpp:324
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:310
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:293
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