QGIS API Documentation  2.8.6-Wien
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 
32 // feature id class (currently 64 bit)
33 #if 0
34 #include <limits>
35 
36 class QgsFeatureId
37 {
38  public:
39  QgsFeatureId( qint64 id = 0 ) : mId( id ) {}
40  QgsFeatureId( QString str ) : mId( str.toLongLong() ) {}
41  QgsFeatureId &operator=( const QgsFeatureId &other ) { mId = other.mId; return *this; }
42  QgsFeatureId &operator++() { mId++; return *this; }
43  QgsFeatureId operator++( int ) { QgsFeatureId pId = mId; ++( *this ); return pId; }
44 
45  bool operator==( const QgsFeatureId &id ) const { return mId == id.mId; }
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  operator QString() const { return QString::number( mId ); }
50 
51  bool isNew() const
52  {
53  return mId < 0;
54  }
55 
56  qint64 toLongLong() const
57  {
58  return mId;
59  }
60 
61  private:
62  qint64 mId;
63 
64  friend uint qHash( const QgsFeatureId &id );
65 };
66 
67 inline uint qHash( const QgsFeatureId &id )
68 {
69  return qHash( id.mId );
70 }
71 
72 #define FID_IS_NEW(fid) (fid).isNew()
73 #define FID_TO_NUMBER(fid) (fid).toLongLong()
74 #define FID_TO_STRING(fid) static_cast<QString>(fid)
75 #define STRING_TO_FID(str) QgsFeatureId(str)
76 #endif
77 
78 // 64 bit feature ids
79 #if 1
80 typedef qint64 QgsFeatureId;
81 #define FID_IS_NEW(fid) (fid<0)
82 #define FID_TO_NUMBER(fid) static_cast<qint64>(fid)
83 #define FID_TO_STRING(fid) QString::number( fid )
84 #define STRING_TO_FID(str) (str).toLongLong()
85 #endif
86 
87 // 32 bit feature ids
88 #if 0
89 typedef int QgsFeatureId;
90 #define FID_IS_NEW(fid) (fid<0)
91 #define FID_TO_NUMBER(fid) static_cast<int>(fid)
92 #define FID_TO_STRING(fid) QString::number( fid )
93 #define STRING_TO_FID(str) (str).toLong()
94 #endif
95 
96 
97 // key = field index, value = field value
98 typedef QMap<int, QVariant> QgsAttributeMap;
99 
103 class CORE_EXPORT QgsAttributes : public QVector<QVariant>
104 {
105  public:
107  : QVector<QVariant>()
108  {}
109  QgsAttributes( int size )
110  : QVector<QVariant>( size )
111  {}
112  QgsAttributes( int size, const QVariant& v )
113  : QVector<QVariant>( size, v )
114  {}
115 
116  QgsAttributes( const QVector<QVariant>& v )
117  : QVector<QVariant>( v )
118  {}
119 
129  bool operator==( const QgsAttributes &v ) const
130  {
131  if ( size() != v.size() )
132  return false;
133  const QVariant* b = constData();
134  const QVariant* i = b + size();
135  const QVariant* j = v.constData() + size();
136  while ( i != b )
137  if ( !( *--i == *--j && i->isNull() == j->isNull() ) )
138  return false;
139  return true;
140  }
141 
142  inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
143 };
144 
145 class QgsField;
146 
147 #include "qgsfield.h"
148 
149 
156 class CORE_EXPORT QgsFeature
157 {
158  public:
161 
162  QgsFeature( const QgsFields& fields, QgsFeatureId id = QgsFeatureId() );
163 
165  QgsFeature( const QgsFeature & rhs );
166 
168  QgsFeature & operator=( QgsFeature const & rhs );
169 
171  ~QgsFeature();
172 
177  QgsFeatureId id() const;
178 
183  void setFeatureId( QgsFeatureId id );
184 
185  const QgsAttributes& attributes() const { return mAttributes; }
186  QgsAttributes& attributes() { return mAttributes; }
187  void setAttributes( const QgsAttributes& attrs ) { mAttributes = attrs; }
188 
199  bool setAttribute( int field, const QVariant& attr );
200 
205  void initAttributes( int fieldCount );
206 
214  void deleteAttribute( int field );
215 
221  bool isValid() const;
222 
226  void setValid( bool validity );
227 
231  QgsGeometry *geometry() const;
232 
237  QgsGeometry *geometryAndOwnership();
238 
241  void setGeometry( const QgsGeometry& geom );
242 
246  void setGeometry( QgsGeometry* geom );
247 
253  void setGeometryAndOwnership( unsigned char * geom, size_t length );
254 
268  void setFields( const QgsFields* fields, bool initAttributes = false );
269 
274  const QgsFields* fields() const { return &mFields; }
275 
286  bool setAttribute( const QString& name, QVariant value );
287 
298  bool deleteAttribute( const QString& name );
299 
310  QVariant attribute( const QString& name ) const;
311 
320  QVariant attribute( int fieldIdx ) const;
321 
325  int fieldNameIndex( const QString& fieldName ) const;
326 
327  private:
328 
330  QgsFeatureId mFid;
331 
333  QgsAttributes mAttributes;
334 
339  QgsGeometry *mGeometry;
340 
344  bool mOwnsGeometry;
345 
347  bool mValid;
348 
350  QgsFields mFields;
351 
352 }; // class QgsFeature
353 
354 // key = feature id, value = changed attributes
355 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
356 
357 // key = feature id, value = changed geometry
358 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
359 
360 typedef QSet<QgsFeatureId> QgsFeatureIds;
361 
362 // key = field index, value = field name
363 typedef QMap<int, QString> QgsFieldNameMap;
364 
365 typedef QList<QgsFeature> QgsFeatureList;
366 
369 
370 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:363
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:358
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QMap< int, QVariant > QgsAttributeMap
Definition: qgsfeature.h:98
QgsAttributes(const QVector< QVariant > &v)
Definition: qgsfeature.h:116
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:360
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:365
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Container of fields for a vector layer.
Definition: qgsfield.h:172
void setAttributes(const QgsAttributes &attrs)
Definition: qgsfeature.h:187
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:156
bool operator==(const QgsAttributes &v) const
Compares two vectors of attributes.
Definition: qgsfeature.h:129
const QgsFields * fields() const
Get associated field map.
Definition: qgsfeature.h:274
QgsAttributes(int size, const QVariant &v)
Definition: qgsfeature.h:112
const QgsAttributes & attributes() const
Definition: qgsfeature.h:185
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:33
QgsAttributes(int size)
Definition: qgsfeature.h:109
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:355
qint64 QgsFeatureId
Definition: qgsfeature.h:30
bool operator!=(const QgsAttributes &v) const
Definition: qgsfeature.h:142
QgsAttributes & attributes()
Definition: qgsfeature.h:186
A vector of attributes.
Definition: qgsfeature.h:103
Q_DECLARE_METATYPE(QgsExpression::Interval)
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:244