Quantum GIS API Documentation  1.8
src/core/qgsfeature.h
Go to the documentation of this file.
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 
00025 class QgsGeometry;
00026 class QgsRectangle;
00027 class QgsFeature;
00028 
00029 // feature id class (currently 64 bit)
00030 #if 0
00031 #include <limits>
00032 
00033 class QgsFeatureId
00034 {
00035   public:
00036     QgsFeatureId( qint64 id = 0 ) : mId( id ) {}
00037     QgsFeatureId( QString str ) : mId( str.toLongLong() ) {}
00038     QgsFeatureId &operator=( const QgsFeatureId &other ) { mId = other.mId; return *this; }
00039     QgsFeatureId &operator++() { mId++; return *this; }
00040     QgsFeatureId operator++( int ) { QgsFeatureId pId = mId; ++( *this ); return pId; }
00041 
00042     bool operator==( const QgsFeatureId &id ) const { return mId == id.mId; }
00043     bool operator!=( const QgsFeatureId &id ) const { return mId != id.mId; }
00044     bool operator<( const QgsFeatureId &id ) const { return mId < id.mId; }
00045     bool operator>( const QgsFeatureId &id ) const { return mId > id.mId; }
00046     operator QString() const { return QString::number( mId ); }
00047 
00048     bool isNew() const
00049     {
00050       return mId < 0;
00051     }
00052 
00053     qint64 toLongLong() const
00054     {
00055       return mId;
00056     }
00057 
00058   private:
00059     qint64 mId;
00060 
00061     friend uint qHash( const QgsFeatureId &id );
00062 };
00063 
00064 inline uint qHash( const QgsFeatureId &id )
00065 {
00066   return qHash( id.mId );
00067 }
00068 
00069 #define FID_IS_NEW(fid)     (fid).isNew()
00070 #define FID_TO_NUMBER(fid)  (fid).toLongLong()
00071 #define FID_TO_STRING(fid)  static_cast<QString>(fid)
00072 #define STRING_TO_FID(str)  QgsFeatureId(str)
00073 #endif
00074 
00075 // 64 bit feature ids
00076 #if 1
00077 typedef qint64 QgsFeatureId;
00078 #define FID_IS_NEW(fid)     (fid<0)
00079 #define FID_TO_NUMBER(fid)  static_cast<qint64>(fid)
00080 #define FID_TO_STRING(fid)  QString::number( fid )
00081 #define STRING_TO_FID(str)  (str).toLongLong()
00082 #endif
00083 
00084 // 32 bit feature ids
00085 #if 0
00086 typedef int QgsFeatureId;
00087 #define FID_IS_NEW(fid)     (fid<0)
00088 #define FID_TO_NUMBER(fid)  static_cast<int>(fid)
00089 #define FID_TO_STRING(fid)  QString::number( fid )
00090 #define STRING_TO_FID(str)  (str).toLong()
00091 #endif
00092 
00093 
00094 // key = field index, value = field value
00095 typedef QMap<int, QVariant> QgsAttributeMap;
00096 
00097 
00104 class CORE_EXPORT QgsFeature
00105 {
00106   public:
00108     QgsFeature( QgsFeatureId id = QgsFeatureId(), QString typeName = "" );
00109 
00111     QgsFeature( QgsFeature const & rhs );
00112 
00114     QgsFeature & operator=( QgsFeature const & rhs );
00115 
00117     ~QgsFeature();
00118 
00123     QgsFeatureId id() const;
00124 
00129     void setFeatureId( QgsFeatureId id );
00130 
00131 
00134     QString typeName() const;
00135 
00136 
00139     void setTypeName( QString typeName );
00140 
00145     const QgsAttributeMap& attributeMap() const;
00146 
00148     void setAttributeMap( const QgsAttributeMap& attributeMap );
00149 
00153     void clearAttributeMap();
00154 
00158     void addAttribute( int field, QVariant attr );
00159 
00161     void deleteAttribute( int field );
00162 
00166     void changeAttribute( int field, QVariant attr );
00167 
00173     bool isValid() const;
00174 
00178     void setValid( bool validity );
00179 
00184     bool isDirty() const;
00185 
00190     void clean();
00191 
00195     QgsGeometry *geometry();
00196 
00201     QgsGeometry *geometryAndOwnership();
00202 
00205     void setGeometry( const QgsGeometry& geom );
00206 
00209     void setGeometry( QgsGeometry* geom );
00210 
00216     void setGeometryAndOwnership( unsigned char * geom, size_t length );
00217 
00218   private:
00219 
00221     QgsFeatureId mFid;
00222 
00224     QgsAttributeMap mAttributes;
00225 
00230     QgsGeometry *mGeometry;
00231 
00235     bool mOwnsGeometry;
00236 
00238     // TODO: still applies? [MD]
00239     bool mValid;
00240 
00242     // TODO: still applies? [MD]
00243     bool mDirty;
00244 
00246     QString mTypeName;
00247 
00248 
00249 }; // class QgsFeature
00250 
00251 // key = feature id, value = changed attributes
00252 typedef QMap<QgsFeatureId, QgsAttributeMap> QgsChangedAttributesMap;
00253 
00254 // key = feature id, value = changed geometry
00255 typedef QMap<QgsFeatureId, QgsGeometry> QgsGeometryMap;
00256 
00257 typedef QSet<QgsFeatureId> QgsFeatureIds;
00258 
00259 // key = field index, value = field name
00260 typedef QMap<int, QString> QgsFieldNameMap;
00261 
00262 typedef QList<QgsFeature> QgsFeatureList;
00263 
00264 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines