Quantum GIS API Documentation  1.8
src/core/qgsfeature.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                 qgsfeature.cpp - Spatial Feature Implementation
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 #include "qgsfeature.h"
00017 #include "qgsgeometry.h"
00018 #include "qgsrectangle.h"
00019 
00024 QgsFeature::QgsFeature( QgsFeatureId id, QString typeName )
00025     : mFid( id )
00026     , mGeometry( 0 )
00027     , mOwnsGeometry( 0 )
00028     , mValid( false )
00029     , mDirty( 0 )
00030     , mTypeName( typeName )
00031 {
00032   // NOOP
00033 }
00034 
00035 QgsFeature::QgsFeature( QgsFeature const & rhs )
00036     : mFid( rhs.mFid )
00037     , mAttributes( rhs.mAttributes )
00038     , mGeometry( 0 )
00039     , mOwnsGeometry( false )
00040     , mValid( rhs.mValid )
00041     , mDirty( rhs.mDirty )
00042     , mTypeName( rhs.mTypeName )
00043 {
00044 
00045   // copy embedded geometry
00046   if ( rhs.mGeometry )
00047   {
00048     setGeometry( *rhs.mGeometry );
00049   }
00050 }
00051 
00052 
00053 QgsFeature & QgsFeature::operator=( QgsFeature const & rhs )
00054 {
00055   if ( &rhs == this )
00056     return *this;
00057 
00058   mFid =  rhs.mFid;
00059   mDirty =  rhs.mDirty;
00060   mAttributes =  rhs.mAttributes;
00061   mValid =  rhs.mValid;
00062   mTypeName = rhs.mTypeName;
00063 
00064   // make sure to delete the old geometry (if exists)
00065   if ( mGeometry && mOwnsGeometry )
00066     delete mGeometry;
00067 
00068   mGeometry = 0;
00069   mOwnsGeometry = false;
00070 
00071   if ( rhs.mGeometry )
00072     setGeometry( *rhs.mGeometry );
00073 
00074   return *this;
00075 } // QgsFeature::operator=( QgsFeature const & rhs )
00076 
00077 
00078 
00080 QgsFeature::~QgsFeature()
00081 {
00082   // Destruct the attached geometry only if we still own it.
00083   if ( mOwnsGeometry && mGeometry )
00084     delete mGeometry;
00085 }
00086 
00091 QgsFeatureId QgsFeature::id() const
00092 {
00093   return mFid;
00094 }
00095 
00100 const QgsAttributeMap& QgsFeature::attributeMap() const
00101 {
00102   return mAttributes;
00103 }
00104 
00106 void QgsFeature::setAttributeMap( const QgsAttributeMap& attributes )
00107 {
00108   mAttributes = attributes;
00109 }
00110 
00112 void QgsFeature::clearAttributeMap()
00113 {
00114   mAttributes.clear();
00115 }
00116 
00120 void QgsFeature::addAttribute( int field, QVariant attr )
00121 {
00122   mAttributes.insert( field, attr );
00123 }
00124 
00126 void QgsFeature::deleteAttribute( int field )
00127 {
00128   mAttributes.remove( field );
00129 }
00130 
00131 
00132 void QgsFeature::changeAttribute( int field, QVariant attr )
00133 {
00134   mAttributes[field] = attr;
00135 }
00136 
00137 QgsGeometry *QgsFeature::geometry()
00138 {
00139   return mGeometry;
00140 }
00141 
00142 QgsGeometry *QgsFeature::geometryAndOwnership()
00143 {
00144   mOwnsGeometry = false;
00145 
00146   return mGeometry;
00147 }
00148 
00149 
00150 
00153 void QgsFeature::setFeatureId( QgsFeatureId id )
00154 {
00155   mFid = id;
00156 }
00157 
00158 
00159 QString QgsFeature::typeName() const
00160 {
00161   return mTypeName;
00162 } // QgsFeature::typeName
00163 
00164 
00165 
00168 void QgsFeature::setTypeName( QString typeName )
00169 {
00170   mTypeName = typeName;
00171 } // QgsFeature::typeName
00172 
00173 
00174 void QgsFeature::setGeometry( const QgsGeometry& geom )
00175 {
00176   setGeometry( new QgsGeometry( geom ) );
00177 }
00178 
00179 void QgsFeature::setGeometry( QgsGeometry* geom )
00180 {
00181   // Destruct the attached geometry only if we still own it, before assigning new one.
00182   if ( mOwnsGeometry && mGeometry )
00183   {
00184     delete mGeometry;
00185     mGeometry = 0;
00186   }
00187 
00188   mGeometry = geom;
00189   mOwnsGeometry = true;
00190 }
00191 
00194 void QgsFeature::setGeometryAndOwnership( unsigned char *geom, size_t length )
00195 {
00196   QgsGeometry *g = new QgsGeometry();
00197   g->fromWkb( geom, length );
00198   setGeometry( g );
00199 }
00200 
00201 
00202 bool QgsFeature::isValid() const
00203 {
00204   return mValid;
00205 }
00206 
00207 void QgsFeature::setValid( bool validity )
00208 {
00209   mValid = validity;
00210 }
00211 
00212 bool QgsFeature::isDirty() const
00213 {
00214   return mDirty;
00215 }
00216 
00217 void QgsFeature::clean()
00218 {
00219   mDirty = false;
00220 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines