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