QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsfeature.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfeature.cpp - Spatial Feature Implementation
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 #include "qgsfeature.h"
17 #include "qgsfield.h"
18 #include "qgsgeometry.h"
19 #include "qgsrectangle.h"
20 
21 #include "qgsmessagelog.h"
22 
28  : mFid( id )
29  , mGeometry( 0 )
30  , mOwnsGeometry( 0 )
31  , mValid( false )
32  , mFields( 0 )
33 {
34  // NOOP
35 }
36 
38  : mFid( id )
39  , mGeometry( 0 )
40  , mOwnsGeometry( 0 )
41  , mValid( false )
42  , mFields( &fields )
43 {
44  initAttributes( fields.count() );
45 }
46 
48  : mFid( rhs.mFid )
49  , mAttributes( rhs.mAttributes )
50  , mGeometry( 0 )
51  , mOwnsGeometry( false )
52  , mValid( rhs.mValid )
53  , mFields( rhs.mFields )
54 {
55 
56  // copy embedded geometry
57  if ( rhs.mGeometry )
58  {
59  setGeometry( *rhs.mGeometry );
60  }
61 }
62 
63 
65 {
66  if ( &rhs == this )
67  return *this;
68 
69  mFid = rhs.mFid;
71  mValid = rhs.mValid;
72  mFields = rhs.mFields;
73 
74  // make sure to delete the old geometry (if exists)
75  if ( mGeometry && mOwnsGeometry )
76  delete mGeometry;
77 
78  mGeometry = 0;
79  mOwnsGeometry = false;
80 
81  if ( rhs.mGeometry )
82  setGeometry( *rhs.mGeometry );
83 
84  return *this;
85 } // QgsFeature::operator=( QgsFeature const & rhs )
86 
87 
88 
91 {
92  // Destruct the attached geometry only if we still own it.
93  if ( mOwnsGeometry && mGeometry )
94  delete mGeometry;
95 }
96 
102 {
103  return mFid;
104 }
105 
108 {
109  mAttributes.remove( field );
110 }
111 
112 
114 {
115  return mGeometry;
116 }
117 
119 {
120  mOwnsGeometry = false;
121 
122  return mGeometry;
123 }
124 
125 
126 
130 {
131  mFid = id;
132 }
133 
134 
136 {
137  setGeometry( new QgsGeometry( geom ) );
138 }
139 
141 {
142  // Destruct the attached geometry only if we still own it, before assigning new one.
143  if ( mOwnsGeometry && mGeometry )
144  {
145  delete mGeometry;
146  mGeometry = 0;
147  }
148 
149  mGeometry = geom;
150  mOwnsGeometry = true;
151 }
152 
155 void QgsFeature::setGeometryAndOwnership( unsigned char *geom, size_t length )
156 {
157  QgsGeometry *g = new QgsGeometry();
158  g->fromWkb( geom, length );
159  setGeometry( g );
160 }
161 
162 void QgsFeature::setFields( const QgsFields* fields, bool init )
163 {
164  mFields = fields;
165  if ( init )
166  {
167  initAttributes( fields->count() );
168  }
169 }
170 
171 
173 {
174  return mValid;
175 }
176 
177 void QgsFeature::setValid( bool validity )
178 {
179  mValid = validity;
180 }
181 
182 void QgsFeature::initAttributes( int fieldCount )
183 {
184  mAttributes.resize( fieldCount );
185  QVariant* ptr = mAttributes.data();
186  for ( int i = 0; i < fieldCount; ++i, ++ptr )
187  ptr->clear();
188 }
189 
190 
191 bool QgsFeature::setAttribute( int idx, const QVariant &value )
192 {
193  if ( idx < 0 || idx >= mAttributes.size() )
194  {
195  QgsMessageLog::logMessage( QObject::tr( "Attribute index %1 out of bounds [0;%2]" ).arg( idx ).arg( mAttributes.size() ), QString::null, QgsMessageLog::WARNING );
196  return false;
197  }
198 
199  mAttributes[idx] = value;
200  return true;
201 }
202 
203 bool QgsFeature::setAttribute( const QString& name, QVariant value )
204 {
205  int fieldIdx = fieldNameIndex( name );
206  if ( fieldIdx == -1 )
207  return false;
208 
209  mAttributes[fieldIdx] = value;
210  return true;
211 }
212 
213 bool QgsFeature::deleteAttribute( const QString& name )
214 {
215  int fieldIdx = fieldNameIndex( name );
216  if ( fieldIdx == -1 )
217  return false;
218 
219  mAttributes[fieldIdx].clear();
220  return true;
221 }
222 
223 QVariant QgsFeature::attribute( int fieldIdx ) const
224 {
225  if ( fieldIdx < 0 || fieldIdx >= mAttributes.count() )
226  return QVariant();
227  return mAttributes[fieldIdx];
228 }
229 
230 
231 QVariant QgsFeature::attribute( const QString& name ) const
232 {
233  int fieldIdx = fieldNameIndex( name );
234  if ( fieldIdx == -1 )
235  return QVariant();
236 
237  return mAttributes[fieldIdx];
238 }
239 
240 int QgsFeature::fieldNameIndex( const QString& fieldName ) const
241 {
242  if ( !mFields )
243  return -1;
244 
245  for ( int i = 0; i < mFields->count(); ++i )
246  {
247  if ( QString::compare( mFields->at( i ).name(), fieldName, Qt::CaseInsensitive ) == 0 )
248  {
249  return i;
250  }
251  }
252  return -1;
253 }