QGIS API Documentation  2.0.1-Dufour
 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 
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 
225  void setFields( const QgsFields* fields, bool initAttributes = false );
226 
230  const QgsFields* fields() const { return mFields; }
231 
243  bool setAttribute( const QString& name, QVariant value );
244 
255  bool deleteAttribute( const QString& name );
256 
268  QVariant attribute( const QString& name ) const;
269 
279  QVariant attribute( int fieldIdx ) const;
280 
286  int fieldNameIndex( const QString& fieldName ) const;
287 
288  private:
289 
292 
295 
301 
306 
308  bool mValid;
309 
312 
313 }; // class QgsFeature
314 
315 // key = feature id, value = changed attributes
316 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
317 
318 // key = feature id, value = changed geometry
319 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
320 
321 typedef QSet<QgsFeatureId> QgsFeatureIds;
322 
323 // key = field index, value = field name
324 typedef QMap<int, QString> QgsFieldNameMap;
325 
326 typedef QList<QgsFeature> QgsFeatureList;
327 
329 
330 #endif