QGIS API Documentation  master-6227475
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 "qgsfield.h"
00018 #include "qgsgeometry.h"
00019 #include "qgsrectangle.h"
00020 
00021 #include "qgsmessagelog.h"
00022 
00027 QgsFeature::QgsFeature( QgsFeatureId id )
00028     : mFid( id )
00029     , mGeometry( 0 )
00030     , mOwnsGeometry( 0 )
00031     , mValid( false )
00032     , mFields( 0 )
00033 {
00034   // NOOP
00035 }
00036 
00037 QgsFeature::QgsFeature( const QgsFields &fields, QgsFeatureId id )
00038     : mFid( id )
00039     , mGeometry( 0 )
00040     , mOwnsGeometry( 0 )
00041     , mValid( false )
00042     , mFields( &fields )
00043 {
00044   initAttributes( fields.count() );
00045 }
00046 
00047 QgsFeature::QgsFeature( QgsFeature const & rhs )
00048     : mFid( rhs.mFid )
00049     , mAttributes( rhs.mAttributes )
00050     , mGeometry( 0 )
00051     , mOwnsGeometry( false )
00052     , mValid( rhs.mValid )
00053     , mFields( rhs.mFields )
00054 {
00055 
00056   // copy embedded geometry
00057   if ( rhs.mGeometry )
00058   {
00059     setGeometry( *rhs.mGeometry );
00060   }
00061 }
00062 
00063 
00064 QgsFeature & QgsFeature::operator=( QgsFeature const & rhs )
00065 {
00066   if ( &rhs == this )
00067     return *this;
00068 
00069   mFid =  rhs.mFid;
00070   mAttributes =  rhs.mAttributes;
00071   mValid =  rhs.mValid;
00072   mFields = rhs.mFields;
00073 
00074   // make sure to delete the old geometry (if exists)
00075   if ( mGeometry && mOwnsGeometry )
00076     delete mGeometry;
00077 
00078   mGeometry = 0;
00079   mOwnsGeometry = false;
00080 
00081   if ( rhs.mGeometry )
00082     setGeometry( *rhs.mGeometry );
00083 
00084   return *this;
00085 } // QgsFeature::operator=( QgsFeature const & rhs )
00086 
00087 
00088 
00090 QgsFeature::~QgsFeature()
00091 {
00092   // Destruct the attached geometry only if we still own it.
00093   if ( mOwnsGeometry && mGeometry )
00094     delete mGeometry;
00095 }
00096 
00101 QgsFeatureId QgsFeature::id() const
00102 {
00103   return mFid;
00104 }
00105 
00107 void QgsFeature::deleteAttribute( int field )
00108 {
00109   mAttributes.remove( field );
00110 }
00111 
00112 
00113 QgsGeometry *QgsFeature::geometry() const
00114 {
00115   return mGeometry;
00116 }
00117 
00118 QgsGeometry *QgsFeature::geometryAndOwnership()
00119 {
00120   mOwnsGeometry = false;
00121 
00122   return mGeometry;
00123 }
00124 
00125 
00126 
00129 void QgsFeature::setFeatureId( QgsFeatureId id )
00130 {
00131   mFid = id;
00132 }
00133 
00134 
00135 void QgsFeature::setGeometry( const QgsGeometry& geom )
00136 {
00137   setGeometry( new QgsGeometry( geom ) );
00138 }
00139 
00140 void QgsFeature::setGeometry( QgsGeometry* geom )
00141 {
00142   // Destruct the attached geometry only if we still own it, before assigning new one.
00143   if ( mOwnsGeometry && mGeometry )
00144   {
00145     delete mGeometry;
00146     mGeometry = 0;
00147   }
00148 
00149   mGeometry = geom;
00150   mOwnsGeometry = true;
00151 }
00152 
00155 void QgsFeature::setGeometryAndOwnership( unsigned char *geom, size_t length )
00156 {
00157   QgsGeometry *g = new QgsGeometry();
00158   g->fromWkb( geom, length );
00159   setGeometry( g );
00160 }
00161 
00162 
00163 bool QgsFeature::isValid() const
00164 {
00165   return mValid;
00166 }
00167 
00168 void QgsFeature::setValid( bool validity )
00169 {
00170   mValid = validity;
00171 }
00172 
00173 void QgsFeature::initAttributes( int fieldCount )
00174 {
00175   mAttributes.resize( fieldCount );
00176   QVariant* ptr = mAttributes.data();
00177   for ( int i = 0; i < fieldCount; ++i, ++ptr )
00178     ptr->clear();
00179 }
00180 
00181 
00182 bool QgsFeature::setAttribute( int idx, const QVariant &value )
00183 {
00184   if ( idx < 0 || idx >= mAttributes.size() )
00185   {
00186     QgsMessageLog::logMessage( QObject::tr( "Attribute index %1 out of bounds [0;%2[" ).arg( idx ).arg( mAttributes.size() ), QString::null, QgsMessageLog::WARNING );
00187     return false;
00188   }
00189 
00190   mAttributes[idx] = value;
00191   return true;
00192 }
00193 
00194 bool QgsFeature::setAttribute( const QString& name, QVariant value )
00195 {
00196   int fieldIdx = fieldNameIndex( name );
00197   if ( fieldIdx == -1 )
00198     return false;
00199 
00200   mAttributes[fieldIdx] = value;
00201   return true;
00202 }
00203 
00204 bool QgsFeature::deleteAttribute( const QString& name )
00205 {
00206   int fieldIdx = fieldNameIndex( name );
00207   if ( fieldIdx == -1 )
00208     return false;
00209 
00210   mAttributes[fieldIdx].clear();
00211   return true;
00212 }
00213 
00214 QVariant QgsFeature::attribute( int fieldIdx ) const
00215 {
00216   if ( fieldIdx < 0 || fieldIdx >= mAttributes.count() )
00217     return QVariant();
00218   return mAttributes[fieldIdx];
00219 }
00220 
00221 
00222 QVariant QgsFeature::attribute( const QString& name ) const
00223 {
00224   int fieldIdx = fieldNameIndex( name );
00225   if ( fieldIdx == -1 )
00226     return QVariant();
00227 
00228   return mAttributes[fieldIdx];
00229 }
00230 
00231 int QgsFeature::fieldNameIndex( const QString& fieldName ) const
00232 {
00233   if ( !mFields )
00234     return -1;
00235 
00236   for ( int i = 0; i < mFields->count(); ++i )
00237   {
00238     if ( QString::compare( mFields->at( i ).name(), fieldName, Qt::CaseInsensitive ) == 0 )
00239     {
00240       return i;
00241     }
00242   }
00243   return -1;
00244 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines