QGIS API Documentation  2.4.0-Chugiak
 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 
104 #include "qgsfield.h"
105 
106 
113 class CORE_EXPORT QgsFeature
114 {
115  public:
118 
119  QgsFeature( const QgsFields& fields, QgsFeatureId id = QgsFeatureId() );
120 
122  QgsFeature( const QgsFeature & rhs );
123 
125  QgsFeature & operator=( QgsFeature const & rhs );
126 
128  ~QgsFeature();
129 
134  QgsFeatureId id() const;
135 
140  void setFeatureId( QgsFeatureId id );
141 
142  const QgsAttributes& attributes() const { return mAttributes; }
143  QgsAttributes& attributes() { return mAttributes; }
144  void setAttributes( const QgsAttributes& attrs ) { mAttributes = attrs; }
145 
156  bool setAttribute( int field, const QVariant& attr );
157 
162  void initAttributes( int fieldCount );
163 
171  void deleteAttribute( int field );
172 
178  bool isValid() const;
179 
183  void setValid( bool validity );
184 
188  QgsGeometry *geometry() const;
189 
194  QgsGeometry *geometryAndOwnership();
195 
198  void setGeometry( const QgsGeometry& geom );
199 
203  void setGeometry( QgsGeometry* geom );
204 
210  void setGeometryAndOwnership( unsigned char * geom, size_t length );
211 
226  void setFields( const QgsFields* fields, bool initAttributes = false );
227 
233  const QgsFields* fields() const { return &mFields; }
234 
246  bool setAttribute( const QString& name, QVariant value );
247 
259  bool deleteAttribute( const QString& name );
260 
272  QVariant attribute( const QString& name ) const;
273 
283  QVariant attribute( int fieldIdx ) const;
284 
290  int fieldNameIndex( const QString& fieldName ) const;
291 
292  private:
293 
296 
299 
305 
310 
312  bool mValid;
313 
316 
317 }; // class QgsFeature
318 
319 // key = feature id, value = changed attributes
320 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
321 
322 // key = feature id, value = changed geometry
323 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
324 
325 typedef QSet<QgsFeatureId> QgsFeatureIds;
326 
327 // key = field index, value = field name
328 typedef QMap<int, QString> QgsFieldNameMap;
329 
330 typedef QList<QgsFeature> QgsFeatureList;
331 
333 
334 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:328
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:323
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:325
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:330
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QgsGeometry * mGeometry
pointer to geometry in binary WKB format
Definition: qgsfeature.h:304
Container of fields for a vector layer.
Definition: qgsfield.h:161
void setAttributes(const QgsAttributes &attrs)
Definition: qgsfeature.h:144
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
const QgsFields * fields() const
Get associated field map.
Definition: qgsfeature.h:233
const QgsAttributes & attributes() const
Definition: qgsfeature.h:142
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:309
QgsAttributes mAttributes
attributes accessed by field index
Definition: qgsfeature.h:298
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:320
bool mValid
Flag to indicate if this feature is valid.
Definition: qgsfeature.h:312
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:315
QgsAttributes & attributes()
Definition: qgsfeature.h:143
QgsFeatureId mFid
feature id
Definition: qgsfeature.h:295
Q_DECLARE_METATYPE(QgsExpression::Interval)
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:216