QGIS API Documentation  2.11.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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 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 
109 class CORE_EXPORT QgsAttributes : public QVector<QVariant>
110 {
111  public:
113  : QVector<QVariant>()
114  {}
115  QgsAttributes( int size )
116  : QVector<QVariant>( size )
117  {}
118  QgsAttributes( int size, const QVariant& v )
119  : QVector<QVariant>( size, v )
120  {}
121 
123  : QVector<QVariant>( v )
124  {}
125 
135  bool operator==( const QgsAttributes &v ) const
136  {
137  if ( size() != v.size() )
138  return false;
139  const QVariant* b = constData();
140  const QVariant* i = b + size();
141  const QVariant* j = v.constData() + size();
142  while ( i != b )
143  if ( !( *--i == *--j && i->isNull() == j->isNull() ) )
144  return false;
145  return true;
146  }
147 
148  inline bool operator!=( const QgsAttributes &v ) const { return !( *this == v ); }
149 };
150 
151 class QgsField;
152 
153 #include "qgsfield.h"
154 
155 
162 class CORE_EXPORT QgsFeature
163 {
164  public:
165 
170 
175  QgsFeature( const QgsFields& fields, QgsFeatureId id = QgsFeatureId() );
176 
179  QgsFeature( const QgsFeature & rhs );
180 
183  QgsFeature & operator=( QgsFeature const & rhs );
184 
186  virtual ~QgsFeature();
187 
192  QgsFeatureId id() const;
193 
198  void setFeatureId( QgsFeatureId id );
199 
206  QgsAttributes attributes() const;
207 
213  void setAttributes( const QgsAttributes& attrs );
214 
222  bool setAttribute( int field, const QVariant& attr );
223 
227  void initAttributes( int fieldCount );
228 
234  void deleteAttribute( int field );
235 
241  bool isValid() const;
242 
247  void setValid( bool validity );
248 
263  QgsGeometry* geometry();
264 
276  const QgsGeometry* constGeometry() const;
277 
284  Q_DECL_DEPRECATED QgsGeometry *geometryAndOwnership();
285 
294  void setGeometry( const QgsGeometry& geom );
295 
306  void setGeometry( QgsGeometry* geom );
307 
318  void setGeometryAndOwnership( unsigned char * geom, size_t length );
319 
328  Q_DECL_DEPRECATED void setFields( const QgsFields* fields, bool initAttributes = false );
329 
338  void setFields( const QgsFields& fields, bool initAttributes = false );
339 
344  const QgsFields* fields() const;
345 
354  bool setAttribute( const QString& name, QVariant value );
355 
363  bool deleteAttribute( const QString& name );
364 
372  QVariant attribute( const QString& name ) const;
373 
381  QVariant attribute( int fieldIdx ) const;
382 
389  int fieldNameIndex( const QString& fieldName ) const;
390 
391  private:
392 
394 
395 }; // class QgsFeature
396 
398 CORE_EXPORT QDataStream& operator<<( QDataStream& out, const QgsFeature& feature );
400 CORE_EXPORT QDataStream& operator>>( QDataStream& in, QgsFeature& feature );
401 
402 // key = feature id, value = changed attributes
404 
405 // key = feature id, value = changed geometry
407 
409 
410 // key = field index, value = field name
412 
414 
417 
418 #endif
QMap< int, QString > QgsFieldNameMap
Definition: qgsfeature.h:411
QMap< QgsFeatureId, QgsGeometry > QgsGeometryMap
Definition: qgsfeature.h:406
A rectangle specified with double values.
Definition: qgsrectangle.h:35
QMap< int, QVariant > QgsAttributeMap
Definition: qgsfeature.h:104
QgsAttributes(const QVector< QVariant > &v)
Definition: qgsfeature.h:122
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QSet< QgsFeatureId > QgsFeatureIds
Definition: qgsfeature.h:408
QList< QgsFeature > QgsFeatureList
Definition: qgsfeature.h:413
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:162
bool operator==(const QgsAttributes &v) const
Compares two vectors of attributes.
Definition: qgsfeature.h:135
QString number(int n, int base)
bool isNull() const
QgsAttributes(int size, const QVariant &v)
Definition: qgsfeature.h:118
const T * constData() const
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:38
QgsAttributes(int size)
Definition: qgsfeature.h:115
QMap< QgsFeatureId, QgsAttributeMap > QgsChangedAttributesMap
Definition: qgsfeature.h:403
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
bool operator!=(const QgsAttributes &v) const
Definition: qgsfeature.h:148
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out.
Definition: qgsfeature.cpp:250
A vector of attributes.
Definition: qgsfeature.h:109
int size() const
Q_DECLARE_METATYPE(QgsExpression::Interval)
uint qHash(const QgsPoint &p)
Definition: qgspoint.h:252