QGIS API Documentation  2.3.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
100 typedef QVector<QVariant> QgsAttributes;
101 
102 class QgsField;
103 typedef QMap<int, QgsField> QgsFieldMap;
104 
105 #include "qgsfield.h"
106 
107 
114 class CORE_EXPORT QgsFeature
115 {
116  public:
119 
120  QgsFeature( const QgsFields& fields, QgsFeatureId id = QgsFeatureId() );
121 
123  QgsFeature( const QgsFeature & rhs );
124 
126  QgsFeature & operator=( QgsFeature const & rhs );
127 
129  ~QgsFeature();
130 
135  QgsFeatureId id() const;
136 
141  void setFeatureId( QgsFeatureId id );
142 
143  const QgsAttributes& attributes() const { return mAttributes; }
144  QgsAttributes& attributes() { return mAttributes; }
145  void setAttributes( const QgsAttributes& attrs ) { mAttributes = attrs; }
146 
157  bool setAttribute( int field, const QVariant& attr );
158 
163  void initAttributes( int fieldCount );
164 
172  void deleteAttribute( int field );
173 
179  bool isValid() const;
180 
184  void setValid( bool validity );
185 
189  QgsGeometry *geometry() const;
190 
195  QgsGeometry *geometryAndOwnership();
196 
199  void setGeometry( const QgsGeometry& geom );
200 
204  void setGeometry( QgsGeometry* geom );
205 
211  void setGeometryAndOwnership( unsigned char * geom, size_t length );
212 
227  void setFields( const QgsFields* fields, bool initAttributes = false );
228 
234  const QgsFields* fields() const { return &mFields; }
235 
247  bool setAttribute( const QString& name, QVariant value );
248 
260  bool deleteAttribute( const QString& name );
261 
273  QVariant attribute( const QString& name ) const;
274 
284  QVariant attribute( int fieldIdx ) const;
285 
291  int fieldNameIndex( const QString& fieldName ) const;
292 
293  private:
294 
297 
300 
306 
311 
313  bool mValid;
314 
317 
318 }; // class QgsFeature
319 
320 // key = feature id, value = changed attributes
321 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
322 
323 // key = feature id, value = changed geometry
324 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
325 
326 typedef QSet<QgsFeatureId> QgsFeatureIds;
327 
328 // key = field index, value = field name
329 typedef QMap<int, QString> QgsFieldNameMap;
330 
331 typedef QList<QgsFeature> QgsFeatureList;
332 
334 
335 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:329
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:324
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QMap< int, QVariant > QgsAttributeMap
Definition: qgsfeature.h:98
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:326
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:331
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsGeometry * mGeometry
pointer to geometry in binary WKB format
Definition: qgsfeature.h:305
Container of fields for a vector layer.
Definition: qgsfield.h:164
void setAttributes(const QgsAttributes &attrs)
Definition: qgsfeature.h:145
QMap< int, QgsField > QgsFieldMap
Definition: qgsfeature.h:102
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:114
const QgsFields * fields() const
Get associated field map.
Definition: qgsfeature.h:234
const QgsAttributes & attributes() const
Definition: qgsfeature.h:143
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:31
bool mOwnsGeometry
Indicator if the mGeometry is owned by this QgsFeature.
Definition: qgsfeature.h:310
QgsAttributes mAttributes
attributes accessed by field index
Definition: qgsfeature.h:299
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:321
bool mValid
Flag to indicate if this feature is valid.
Definition: qgsfeature.h:313
QVector< QVariant > QgsAttributes
Definition: qgsfeature.h:100
qint64 QgsFeatureId
Definition: qgsfeature.h:30
QgsFields mFields
Optional field map for name-based attribute lookups.
Definition: qgsfeature.h:316
QgsAttributes & attributes()
Definition: qgsfeature.h:144
QgsFeatureId mFid
feature id
Definition: qgsfeature.h:296
Q_DECLARE_METATYPE(QgsExpression::Interval)
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:216