QGIS API Documentation  2.8.2-Wien
 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 
225  void setFields( const QgsFields* fields, bool initAttributes = false );
226 
231  const QgsFields* fields() const { return &mFields; }
232 
243  bool setAttribute( const QString& name, QVariant value );
244 
255  bool deleteAttribute( const QString& name );
256 
267  QVariant attribute( const QString& name ) const;
268 
277  QVariant attribute( int fieldIdx ) const;
278 
282  int fieldNameIndex( const QString& fieldName ) const;
283 
284  private:
285 
287  QgsFeatureId mFid;
288 
290  QgsAttributes mAttributes;
291 
296  QgsGeometry *mGeometry;
297 
301  bool mOwnsGeometry;
302 
304  bool mValid;
305 
307  QgsFields mFields;
308 
309 }; // class QgsFeature
310 
311 // key = feature id, value = changed attributes
312 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
313 
314 // key = feature id, value = changed geometry
315 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
316 
317 typedef QSet<QgsFeatureId> QgsFeatureIds;
318 
319 // key = field index, value = field name
320 typedef QMap<int, QString> QgsFieldNameMap;
321 
322 typedef QList<QgsFeature> QgsFeatureList;
323 
326 
327 #endif