QGIS API Documentation  2.12.0-Lyon
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 
109 class CORE_EXPORT QgsAttributes : public QVector<QVariant>
110 {
111  public:
113  : QVector<QVariant>()
114  {}
120  QgsAttributes( int size )
121  : QVector<QVariant>( size )
122  {}
128  QgsAttributes( int size, const QVariant& v )
129  : QVector<QVariant>( size, v )
130  {}
131 
137  : QVector<QVariant>( v )
138  {}
139 
149  bool operator==( const QgsAttributes &v ) const
150  {
151  if ( size() != v.size() )
152  return false;
153  const QVariant* b = constData();
154  const QVariant* i = b + size();
155  const QVariant* j = v.constData() + size();
156  while ( i != b )
157  if ( !( *--i == *--j && i->isNull() == j->isNull() ) )
158  return false;
159  return true;
160  }
161 
162  inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
163 };
164 
165 class QgsField;
166 
167 #include "qgsfield.h"
168 
169 
176 class CORE_EXPORT QgsFeature
177 {
178  public:
179 
184 
189  QgsFeature( const QgsFields& fields, QgsFeatureId id = QgsFeatureId() );
190 
193  QgsFeature( const QgsFeature & rhs );
194 
197  QgsFeature & operator=( QgsFeature const & rhs );
198 
200  virtual ~QgsFeature();
201 
206  QgsFeatureId id() const;
207 
212  void setFeatureId( QgsFeatureId id );
213 
220  QgsAttributes attributes() const;
221 
227  void setAttributes( const QgsAttributes& attrs );
228 
236  bool setAttribute( int field, const QVariant& attr );
237 
241  void initAttributes( int fieldCount );
242 
248  void deleteAttribute( int field );
249 
255  bool isValid() const;
256 
261  void setValid( bool validity );
262 
277  QgsGeometry* geometry();
278 
290  const QgsGeometry* constGeometry() const;
291 
298  Q_DECL_DEPRECATED QgsGeometry *geometryAndOwnership();
299 
308  void setGeometry( const QgsGeometry& geom );
309 
320  void setGeometry( QgsGeometry* geom );
321 
332  void setGeometryAndOwnership( unsigned char * geom, size_t length );
333 
342  Q_DECL_DEPRECATED void setFields( const QgsFields* fields, bool initAttributes = false );
343 
352  void setFields( const QgsFields& fields, bool initAttributes = false );
353 
358  const QgsFields* fields() const;
359 
368  bool setAttribute( const QString& name, const QVariant& value );
369 
377  bool deleteAttribute( const QString& name );
378 
386  QVariant attribute( const QString& name ) const;
387 
395  QVariant attribute( int fieldIdx ) const;
396 
403  int fieldNameIndex( const QString& fieldName ) const;
404 
405  private:
406 
408 
409 }; // class QgsFeature
410 
412 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFeature& feature );
414 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFeature& feature );
415 
416 // key = feature id, value = changed attributes
418 
419 // key = feature id, value = changed geometry
421 
423 
424 // key = field index, value = field name
426 
428 
431 
432 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:425
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:420
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:136
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:422
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:427
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Container of fields for a vector layer.
Definition: qgsfield.h:177
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:176
bool operator==(const QgsAttributes &v) const
Compares two vectors of attributes.
Definition: qgsfeature.h:149
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:128
const T * constData() const
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:40
QgsAttributes(int size)
Create a new vector of attributes with the given size.
Definition: qgsfeature.h:120
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:417
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:269
bool operator!=(const QgsAttributes &v) const
Definition: qgsfeature.h:162
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out.
Definition: qgsfeature.cpp:252
A vector of attributes.
Definition: qgsfeature.h:109
Q_NOWARN_DEPRECATED_POP Q_DECLARE_METATYPE(QgsExpression::Interval)
int size() const
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:252