QGIS API Documentation  2.14.0-Essen
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 <QHash>
24 #include <QVector>
25 #include <QSet>
26 
27 class QgsGeometry;
28 class QgsRectangle;
29 class QgsFeature;
30 class QgsFields;
31 class QgsFeaturePrivate;
32 
33 // feature id class (currently 64 bit)
34 #if 0
35 #include <limits>
36 
37 class QgsFeatureId
38 {
39  public:
40  QgsFeatureId( qint64 id = 0 ) : mId( id ) {}
41  QgsFeatureId( QString str ) : mId( str.toLongLong() ) {}
42  QgsFeatureId &operator=( const QgsFeatureId &other ) { mId = other.mId; return *this; }
43  QgsFeatureId &operator++() { mId++; return *this; }
44  QgsFeatureId operator++( int ) { QgsFeatureId pId = mId; ++( *this ); return pId; }
45 
46  bool operator==( const QgsFeatureId &id ) const { return mId == id.mId; }
47  bool operator!=( const QgsFeatureId &id ) const { return mId != id.mId; }
48  bool operator<( const QgsFeatureId &id ) const { return mId < id.mId; }
49  bool operator>( const QgsFeatureId &id ) const { return mId > id.mId; }
50  operator QString() const { return QString::number( mId ); }
51 
52  bool isNew() const
53  {
54  return mId < 0;
55  }
56 
57  qint64 toLongLong() const
58  {
59  return mId;
60  }
61 
62  private:
63  qint64 mId;
64 
65  friend uint qHash( const QgsFeatureId &id );
66 };
67 
69 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFeatureId& featureId );
71 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFeatureId& featureId );
72 
73 inline uint qHash( const QgsFeatureId &id )
74 {
75  return qHash( id.mId );
76 }
77 
78 #define FID_IS_NEW(fid) (fid).isNew()
79 #define FID_TO_NUMBER(fid) (fid).toLongLong()
80 #define FID_TO_STRING(fid) static_cast<QString>(fid)
81 #define STRING_TO_FID(str) QgsFeatureId(str)
82 #endif
83 
84 // 64 bit feature ids
85 #if 1
86 typedef qint64 QgsFeatureId;
87 #define FID_IS_NEW(fid) (fid<0)
88 #define FID_TO_NUMBER(fid) static_cast<qint64>(fid)
89 #define FID_TO_STRING(fid) QString::number( fid )
90 #define STRING_TO_FID(str) (str).toLongLong()
91 #endif
92 
93 // 32 bit feature ids
94 #if 0
95 typedef int QgsFeatureId;
96 #define FID_IS_NEW(fid) (fid<0)
97 #define FID_TO_NUMBER(fid) static_cast<int>(fid)
98 #define FID_TO_STRING(fid) QString::number( fid )
99 #define STRING_TO_FID(str) (str).toLong()
100 #endif
101 
102 
103 // key = field index, value = field value
105 
106 /***************************************************************************
107  * This class is considered CRITICAL and any change MUST be accompanied with
108  * full unit tests in testqgsfeature.cpp.
109  * See details in QEP #17
110  ****************************************************************************/
111 
115 class CORE_EXPORT QgsAttributes : public QVector<QVariant>
116 {
117  public:
119  : QVector<QVariant>()
120  {}
126  QgsAttributes( int size )
127  : QVector<QVariant>( size )
128  {}
134  QgsAttributes( int size, const QVariant& v )
135  : QVector<QVariant>( size, v )
136  {}
137 
143  : QVector<QVariant>( v )
144  {}
145 
155  bool operator==( const QgsAttributes &v ) const
156  {
157  if ( size() != v.size() )
158  return false;
159  const QVariant* b = constData();
160  const QVariant* i = b + size();
161  const QVariant* j = v.constData() + size();
162  while ( i != b )
163  if ( !( *--i == *--j && i->isNull() == j->isNull() ) )
164  return false;
165  return true;
166  }
167 
168  inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
169 };
170 
171 class QgsField;
172 
173 #include "qgsfield.h"
174 
175 /***************************************************************************
176  * This class is considered CRITICAL and any change MUST be accompanied with
177  * full unit tests in testqgsfeature.cpp.
178  * See details in QEP #17
179  ****************************************************************************/
180 
187 class CORE_EXPORT QgsFeature
188 {
189  public:
190 
195 
200  QgsFeature( const QgsFields& fields, QgsFeatureId id = QgsFeatureId() );
201 
204  QgsFeature( const QgsFeature & rhs );
205 
208  QgsFeature & operator=( QgsFeature const & rhs );
209 
211  virtual ~QgsFeature();
212 
217  QgsFeatureId id() const;
218 
223  void setFeatureId( QgsFeatureId id );
224 
231  QgsAttributes attributes() const;
232 
238  void setAttributes( const QgsAttributes& attrs );
239 
247  bool setAttribute( int field, const QVariant& attr );
248 
252  void initAttributes( int fieldCount );
253 
259  void deleteAttribute( int field );
260 
266  bool isValid() const;
267 
272  void setValid( bool validity );
273 
288  QgsGeometry* geometry();
289 
301  const QgsGeometry* constGeometry() const;
302 
310  Q_DECL_DEPRECATED QgsGeometry *geometryAndOwnership();
311 
320  void setGeometry( const QgsGeometry& geom );
321 
331  void setGeometry( QgsGeometry* geom );
332 
343  Q_DECL_DEPRECATED void setGeometryAndOwnership( unsigned char * geom, int length );
344 
353  Q_DECL_DEPRECATED void setFields( const QgsFields* fields, bool initAttributes = false );
354 
363  void setFields( const QgsFields& fields, bool initAttributes = false );
364 
369  const QgsFields* fields() const;
370 
379  bool setAttribute( const QString& name, const QVariant& value );
380 
388  bool deleteAttribute( const QString& name );
389 
397  QVariant attribute( const QString& name ) const;
398 
406  QVariant attribute( int fieldIdx ) const;
407 
414  int fieldNameIndex( const QString& fieldName ) const;
415 
416  private:
417 
419 
420 }; // class QgsFeature
421 
423 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFeature& feature );
425 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFeature& feature );
426 
427 // key = feature id, value = changed attributes
429 
430 // key = feature id, value = changed geometry
432 
434 
435 // key = field index, value = field name
437 
439 
440 Q_DECLARE_METATYPE( QgsFeature )
441 Q_DECLARE_METATYPE( QgsFeatureList )
442 
443 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:436
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:431
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QMap< int, QVariant > QgsAttributeMap
Definition: qgsfeature.h:104
QgsAttributes(const QVector< QVariant > &v)
Copies another vector of attributes.
Definition: qgsfeature.h:142
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:433
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:438
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Container of fields for a vector layer.
Definition: qgsfield.h:187
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:187
bool operator==(const QgsAttributes &v) const
Compares two vectors of attributes.
Definition: qgsfeature.h:155
QString number(int n, int base)
bool isNull() const
QgsAttributes(int size, const QVariant &v)
Constructs a vector with an initial size of size elements.
Definition: qgsfeature.h:134
const T * constData() const
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:44
QgsAttributes(int size)
Create a new vector of attributes with the given size.
Definition: qgsfeature.h:126
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:428
qint64 QgsFeatureId
Definition: qgsfeature.h:31
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsFeature &feature)
Reads a feature from stream in into feature.
Definition: qgsfeature.cpp:308
bool operator!=(const QgsAttributes &v) const
Definition: qgsfeature.h:168
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out.
Definition: qgsfeature.cpp:291
A vector of attributes.
Definition: qgsfeature.h:115
int size() const
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:254