QGIS API Documentation  2.11.0-Master
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 class QgsFeaturePrivate;
32 
33 // feature id class (currently 64 bit)
34 #if 0
35 #include <limits>
36 
37 class QgsFeatureId
38 {
39  public:
40  QgsFeatureId( qint64 id = 0 ) : mId( id ) {}
41  QgsFeatureId( QString str ) : mId( str.toLongLong() ) {}
42  QgsFeatureId &operator=( const QgsFeatureId &other ) { mId = other.mId; return *this; }
43  QgsFeatureId &operator++() { mId++; return *this; }
44  QgsFeatureId operator++( int ) { QgsFeatureId pId = mId; ++( *this ); return pId; }
45 
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  bool operator>( const QgsFeatureId &id ) const { return mId > id.mId; }
50  operator QString() const { return QString::number( mId ); }
51 
52  bool isNew() const
53  {
54  return mId < 0;
55  }
56 
57  qint64 toLongLong() const
58  {
59  return mId;
60  }
61 
62  private:
63  qint64 mId;
64 
65  friend uint qHash( const QgsFeatureId &id );
66 };
67 
69 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFeatureId& featureId );
71 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFeatureId& featureId );
72 
73 inline uint qHash( const QgsFeatureId &id )
74 {
75  return qHash( id.mId );
76 }
77 
78 #define FID_IS_NEW(fid) (fid).isNew()
79 #define FID_TO_NUMBER(fid) (fid).toLongLong()
80 #define FID_TO_STRING(fid) static_cast<QString>(fid)
81 #define STRING_TO_FID(str) QgsFeatureId(str)
82 #endif
83 
84 // 64 bit feature ids
85 #if 1
86 typedef qint64 QgsFeatureId;
87 #define FID_IS_NEW(fid) (fid<0)
88 #define FID_TO_NUMBER(fid) static_cast<qint64>(fid)
89 #define FID_TO_STRING(fid) QString::number( fid )
90 #define STRING_TO_FID(str) (str).toLongLong()
91 #endif
92 
93 // 32 bit feature ids
94 #if 0
95 typedef int QgsFeatureId;
96 #define FID_IS_NEW(fid) (fid<0)
97 #define FID_TO_NUMBER(fid) static_cast<int>(fid)
98 #define FID_TO_STRING(fid) QString::number( fid )
99 #define STRING_TO_FID(str) (str).toLong()
100 #endif
101 
102 
103 // key = field index, value = field value
105 
107 
108 class QgsField;
109 
110 #include "qgsfield.h"
111 
112 
119 class CORE_EXPORT QgsFeature
120 {
121  public:
122 
127 
132  QgsFeature( const QgsFields& fields, QgsFeatureId id = QgsFeatureId() );
133 
136  QgsFeature( const QgsFeature & rhs );
137 
140  QgsFeature & operator=( QgsFeature const & rhs );
141 
143  virtual ~QgsFeature();
144 
149  QgsFeatureId id() const;
150 
155  void setFeatureId( QgsFeatureId id );
156 
163  QgsAttributes attributes() const;
164 
170  void setAttributes( const QgsAttributes& attrs );
171 
179  bool setAttribute( int field, const QVariant& attr );
180 
184  void initAttributes( int fieldCount );
185 
191  void deleteAttribute( int field );
192 
198  bool isValid() const;
199 
204  void setValid( bool validity );
205 
220  QgsGeometry* geometry();
221 
233  const QgsGeometry* constGeometry() const;
234 
241  Q_DECL_DEPRECATED QgsGeometry *geometryAndOwnership();
242 
251  void setGeometry( const QgsGeometry& geom );
252 
263  void setGeometry( QgsGeometry* geom );
264 
275  void setGeometryAndOwnership( unsigned char * geom, size_t length );
276 
285  Q_DECL_DEPRECATED void setFields( const QgsFields* fields, bool initAttributes = false );
286 
295  void setFields( const QgsFields& fields, bool initAttributes = false );
296 
301  const QgsFields* fields() const;
302 
311  bool setAttribute( const QString& name, QVariant value );
312 
320  bool deleteAttribute( const QString& name );
321 
329  QVariant attribute( const QString& name ) const;
330 
338  QVariant attribute( int fieldIdx ) const;
339 
346  int fieldNameIndex( const QString& fieldName ) const;
347 
348  private:
349 
351 
352 }; // class QgsFeature
353 
355 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFeature& feature );
357 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFeature& feature );
358 
359 // key = feature id, value = changed attributes
361 
362 // key = feature id, value = changed geometry
364 
366 
367 // key = field index, value = field name
369 
371 
374 
375 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:368
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:363
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QMap< int, QVariant > QgsAttributeMap
Definition: qgsfeature.h:104
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:365
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:370
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
Container of fields for a vector layer.
Definition: qgsfield.h:173
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:75
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:119
QString number(int n, int base)
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:38
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:360
QVector< QVariant > QgsAttributes
Definition: qgsfeature.h:106
qint64 QgsFeatureId
Definition: qgsfeature.h:31
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsFeature &feature)
Reads a feature from stream in into feature.
Definition: qgsfeature.cpp:267
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out.
Definition: qgsfeature.cpp:250
Q_DECLARE_METATYPE(QgsExpression::Interval)
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:252