QGIS API Documentation  2.99.0-Master (e077efd)
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 <QMap>
20 #include <QString>
21 #include <QVariant>
22 #include <QList>
23 #include <QVector>
24 #include <QSet>
25 #include <QExplicitlySharedDataPointer>
26 
27 #include "qgsfields.h"
28 
29 class QgsGeometry;
30 class QgsRectangle;
31 class QgsFeature;
32 class QgsFeaturePrivate;
33 
34 // feature id class (currently 64 bit)
35 
36 // 64 bit feature ids
37 typedef qint64 QgsFeatureId;
38 #define FID_IS_NEW(fid) (fid<0)
39 #define FID_TO_NUMBER(fid) static_cast<qint64>(fid)
40 #define FID_TO_STRING(fid) QString::number( fid )
41 #define STRING_TO_FID(str) (str).toLongLong()
42 
43 // key = field index, value = field value
44 typedef QMap<int, QVariant> QgsAttributeMap;
45 
46 /***************************************************************************
47  * This class is considered CRITICAL and any change MUST be accompanied with
48  * full unit tests in testqgsfeature.cpp.
49  * See details in QEP #17
50  ****************************************************************************/
51 
55 class CORE_EXPORT QgsAttributes : public QVector<QVariant>
56 {
57  public:
59  : QVector<QVariant>()
60  {}
61 
67  QgsAttributes( int size )
68  : QVector<QVariant>( size )
69  {}
70 
76  QgsAttributes( int size, const QVariant& v )
77  : QVector<QVariant>( size, v )
78  {}
79 
84  QgsAttributes( const QVector<QVariant>& v )
85  : QVector<QVariant>( v )
86  {}
87 
97  bool operator==( const QgsAttributes &v ) const
98  {
99  if ( size() != v.size() )
100  return false;
101  const QVariant* b = constData();
102  const QVariant* i = b + size();
103  const QVariant* j = v.constData() + size();
104  while ( i != b )
105  if ( !( *--i == *--j && i->isNull() == j->isNull() ) )
106  return false;
107  return true;
108  }
109 
116  QgsAttributeMap toMap() const;
117 
118  inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
119 };
120 
121 class QgsField;
122 
123 /***************************************************************************
124  * This class is considered CRITICAL and any change MUST be accompanied with
125  * full unit tests in testqgsfeature.cpp.
126  * See details in QEP #17
127  ****************************************************************************/
128 
135 class CORE_EXPORT QgsFeature
136 {
137  Q_GADGET
138 
139  Q_PROPERTY( QgsFeatureId id READ id WRITE setId )
140  Q_PROPERTY( QgsAttributes attributes READ attributes WRITE setAttributes )
141  Q_PROPERTY( QgsFields fields READ fields WRITE setFields )
142 
143  public:
144 
149 
154  QgsFeature( const QgsFields& fields, QgsFeatureId id = QgsFeatureId() );
155 
158  QgsFeature( const QgsFeature& rhs );
159 
162  QgsFeature& operator=( const QgsFeature& rhs );
163 
167  bool operator==( const QgsFeature& other ) const;
168 
172  bool operator!=( const QgsFeature& other ) const;
173 
174 
176  virtual ~QgsFeature();
177 
182  QgsFeatureId id() const;
183 
188  void setFeatureId( QgsFeatureId id );
189 
194  void setId( QgsFeatureId id );
195 
202  QgsAttributes attributes() const;
203 
209  void setAttributes( const QgsAttributes& attrs );
210 
218  bool setAttribute( int field, const QVariant& attr );
219 
223  void initAttributes( int fieldCount );
224 
230  void deleteAttribute( int field );
231 
237  bool isValid() const;
238 
243  void setValid( bool validity );
244 
249  bool hasGeometry() const;
250 
256  QgsGeometry geometry() const;
257 
263  void setGeometry( const QgsGeometry& geometry );
264 
270  void clearGeometry();
271 
280  void setFields( const QgsFields& fields, bool initAttributes = false );
281 
285  QgsFields fields() const;
286 
295  bool setAttribute( const QString& name, const QVariant& value );
296 
304  bool deleteAttribute( const QString& name );
305 
313  QVariant attribute( const QString& name ) const;
314 
322  QVariant attribute( int fieldIdx ) const;
323 
330  int fieldNameIndex( const QString& fieldName ) const;
331 
333  operator QVariant() const
334  {
335  return QVariant::fromValue( *this );
336  }
337 
338  private:
339 
340  QExplicitlySharedDataPointer<QgsFeaturePrivate> d;
341 
342 }; // class QgsFeature
343 
345 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFeature& feature );
347 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFeature& feature );
348 
349 // key = feature id, value = changed attributes
350 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
351 
352 // key = feature id, value = changed geometry
353 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
354 
355 typedef QSet<QgsFeatureId> QgsFeatureIds;
356 
357 // key = field index, value = field name
358 typedef QMap<int, QString> QgsFieldNameMap;
359 
360 typedef QList<QgsFeature> QgsFeatureList;
361 
362 uint qHash( const QgsFeature& key, uint seed = 0 );
363 
366 
367 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:358
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:353
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QMap< int, QVariant > QgsAttributeMap
Definition: qgsfeature.h:44
QgsAttributes(const QVector< QVariant > &v)
Copies another vector of attributes.
Definition: qgsfeature.h:84
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:355
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:360
Q_DECLARE_METATYPE(QgsMimeDataUtils::UriList)
Container of fields for a vector layer.
Definition: qgsfields.h:36
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:78
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
void seed(uint32_t value)
QgsAttributes(int size, const QVariant &v)
Constructs a vector with an initial size of size elements.
Definition: qgsfeature.h:76
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:47
QgsAttributes(int size)
Create a new vector of attributes with the given size.
Definition: qgsfeature.h:67
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:350
uint qHash(const QgsFeature &key, uint seed=0)
Definition: qgsfeature.cpp:334
qint64 QgsFeatureId
Definition: qgsfeature.h:32
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:320
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:303
bool operator!=(const QgsAttributes &v) const
Definition: qgsfeature.h:118
A vector of attributes.
Definition: qgsfeature.h:55
bool operator==(const QgsAttributes &v) const
Compares two vectors of attributes.
Definition: qgsfeature.h:97