|
Quantum GIS API Documentation
master-ce49b66
|
00001 /*************************************************************************** 00002 qgsfeature.h - Spatial Feature Class 00003 -------------------------------------- 00004 Date : 09-Sep-2003 00005 Copyright : (C) 2003 by Gary E.Sherman 00006 email : sherman at mrcc.com 00007 *************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 00016 #ifndef QGSFEATURE_H 00017 #define QGSFEATURE_H 00018 00019 #include <QMap> 00020 #include <QString> 00021 #include <QVariant> 00022 #include <QList> 00023 #include <QHash> 00024 #include <QVector> 00025 #include <QSet> 00026 00027 class QgsGeometry; 00028 class QgsRectangle; 00029 class QgsFeature; 00030 class QgsFields; 00031 00032 // feature id class (currently 64 bit) 00033 #if 0 00034 #include <limits> 00035 00036 class QgsFeatureId 00037 { 00038 public: 00039 QgsFeatureId( qint64 id = 0 ) : mId( id ) {} 00040 QgsFeatureId( QString str ) : mId( str.toLongLong() ) {} 00041 QgsFeatureId &operator=( const QgsFeatureId &other ) { mId = other.mId; return *this; } 00042 QgsFeatureId &operator++() { mId++; return *this; } 00043 QgsFeatureId operator++( int ) { QgsFeatureId pId = mId; ++( *this ); return pId; } 00044 00045 bool operator==( const QgsFeatureId &id ) const { return mId == id.mId; } 00046 bool operator!=( const QgsFeatureId &id ) const { return mId != id.mId; } 00047 bool operator<( const QgsFeatureId &id ) const { return mId < id.mId; } 00048 bool operator>( const QgsFeatureId &id ) const { return mId > id.mId; } 00049 operator QString() const { return QString::number( mId ); } 00050 00051 bool isNew() const 00052 { 00053 return mId < 0; 00054 } 00055 00056 qint64 toLongLong() const 00057 { 00058 return mId; 00059 } 00060 00061 private: 00062 qint64 mId; 00063 00064 friend uint qHash( const QgsFeatureId &id ); 00065 }; 00066 00067 inline uint qHash( const QgsFeatureId &id ) 00068 { 00069 return qHash( id.mId ); 00070 } 00071 00072 #define FID_IS_NEW(fid) (fid).isNew() 00073 #define FID_TO_NUMBER(fid) (fid).toLongLong() 00074 #define FID_TO_STRING(fid) static_cast<QString>(fid) 00075 #define STRING_TO_FID(str) QgsFeatureId(str) 00076 #endif 00077 00078 // 64 bit feature ids 00079 #if 1 00080 typedef qint64 QgsFeatureId; 00081 #define FID_IS_NEW(fid) (fid<0) 00082 #define FID_TO_NUMBER(fid) static_cast<qint64>(fid) 00083 #define FID_TO_STRING(fid) QString::number( fid ) 00084 #define STRING_TO_FID(str) (str).toLongLong() 00085 #endif 00086 00087 // 32 bit feature ids 00088 #if 0 00089 typedef int QgsFeatureId; 00090 #define FID_IS_NEW(fid) (fid<0) 00091 #define FID_TO_NUMBER(fid) static_cast<int>(fid) 00092 #define FID_TO_STRING(fid) QString::number( fid ) 00093 #define STRING_TO_FID(str) (str).toLong() 00094 #endif 00095 00096 00097 // key = field index, value = field value 00098 typedef QMap<int, QVariant> QgsAttributeMap; 00099 00100 typedef QVector<QVariant> QgsAttributes; 00101 00102 class QgsField; 00103 typedef QMap<int, QgsField> QgsFieldMap; 00104 00105 00106 00107 00114 class CORE_EXPORT QgsFeature 00115 { 00116 public: 00118 QgsFeature( QgsFeatureId id = QgsFeatureId() ); 00119 00120 QgsFeature( const QgsFields& fields, QgsFeatureId id = QgsFeatureId() ); 00121 00123 QgsFeature( const QgsFeature & rhs ); 00124 00126 QgsFeature & operator=( QgsFeature const & rhs ); 00127 00129 ~QgsFeature(); 00130 00135 QgsFeatureId id() const; 00136 00141 void setFeatureId( QgsFeatureId id ); 00142 00143 const QgsAttributes& attributes() const { return mAttributes; } 00144 QgsAttributes& attributes() { return mAttributes; } 00145 void setAttributes( const QgsAttributes& attrs ) { mAttributes = attrs; } 00146 bool setAttribute( int field, const QVariant& attr ); 00147 void initAttributes( int fieldCount ); 00148 00150 void deleteAttribute( int field ); 00151 00157 bool isValid() const; 00158 00162 void setValid( bool validity ); 00163 00167 QgsGeometry *geometry() const; 00168 00173 QgsGeometry *geometryAndOwnership(); 00174 00177 void setGeometry( const QgsGeometry& geom ); 00178 00182 void setGeometry( QgsGeometry* geom ); 00183 00189 void setGeometryAndOwnership( unsigned char * geom, size_t length ); 00190 00194 void setFields( const QgsFields* fields ) { mFields = fields; } 00195 00199 const QgsFields* fields() const { return mFields; } 00200 00205 bool setAttribute( const QString& name, QVariant value ); 00206 00211 bool deleteAttribute( const QString& name ); 00212 00217 QVariant attribute( const QString& name ) const; 00218 00222 QVariant attribute( int fieldIdx ) const; 00223 00228 int fieldNameIndex( const QString& fieldName ) const; 00229 00230 private: 00231 00233 QgsFeatureId mFid; 00234 00236 QgsAttributes mAttributes; 00237 00242 QgsGeometry *mGeometry; 00243 00247 bool mOwnsGeometry; 00248 00250 bool mValid; 00251 00253 const QgsFields* mFields; 00254 00255 }; // class QgsFeature 00256 00257 // key = feature id, value = changed attributes 00258 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap; 00259 00260 // key = feature id, value = changed geometry 00261 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap; 00262 00263 typedef QSet<QgsFeatureId> QgsFeatureIds; 00264 00265 // key = field index, value = field name 00266 typedef QMap<int, QString> QgsFieldNameMap; 00267 00268 typedef QList<QgsFeature> QgsFeatureList; 00269 00270 Q_DECLARE_METATYPE( QgsFeatureList ); 00271 00272 #endif