QGIS API Documentation  2.5.0-Master
 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;
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 }
QgsFeatureId id() const
Get the feature id for this feature.
Definition: qgsfeature.cpp:100
const QString & name() const
Gets the name of the field.
Definition: qgsfield.cpp:58
bool isValid() const
Return the validity of this feature.
Definition: qgsfeature.cpp:171
int fieldNameIndex(const QString &fieldName) const
Utility method to get attribute index from name.
Definition: qgsfeature.cpp:239
~QgsFeature()
Destructor.
Definition: qgsfeature.cpp:89
QgsGeometry * geometry() const
Get the geometry object associated with this feature.
Definition: qgsfeature.cpp:112
QgsGeometry * mGeometry
pointer to geometry in binary WKB format
Definition: qgsfeature.h:304
Container of fields for a vector layer.
Definition: qgsfield.h:163
bool setAttribute(int field, const QVariant &attr)
Set an attribute by id.
Definition: qgsfeature.cpp:190
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfield.h:213
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:113
static void logMessage(QString message, QString tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
void setGeometry(const QgsGeometry &geom)
Set this feature's geometry from another QgsGeometry object (deep copy)
Definition: qgsfeature.cpp:134
void deleteAttribute(int field)
Deletes an attribute and its value.
Definition: qgsfeature.cpp:106
void setFeatureId(QgsFeatureId id)
Set the feature id for this feature.
Definition: qgsfeature.cpp:128
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:181
const QgsFields * fields() const
Get associated field map.
Definition: qgsfeature.h:233
int count() const
Return number of items.
Definition: qgsfield.h:200
QgsFeature & operator=(QgsFeature const &rhs)
assignment operator needed due to internal pointer
Definition: qgsfeature.cpp:63
void setFields(const QgsFields *fields, bool initAttributes=false)
Assign a field map with the feature to allow attribute access by attribute name.
Definition: qgsfeature.cpp:161
QgsFeature(QgsFeatureId id=QgsFeatureId())
Constructor.
Definition: qgsfeature.cpp:27
bool mOwnsGeometry
Indicator if the mGeometry is owned by this QgsFeature.
Definition: qgsfeature.h:309
void setValid(bool validity)
Set the validity of the feature.
Definition: qgsfeature.cpp:176
QgsAttributes mAttributes
attributes accessed by field index
Definition: qgsfeature.h:298
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:230
bool mValid
Flag to indicate if this feature is valid.
Definition: qgsfeature.h:312
void fromWkb(unsigned char *wkb, size_t length)
Set the geometry, feeding in the buffer containing OGC Well-Known Binary and the buffer's length...
qint64 QgsFeatureId
Definition: qgsfeature.h:30
QgsFields mFields
Optional field map for name-based attribute lookups.
Definition: qgsfeature.h:315
QgsGeometry * geometryAndOwnership()
Get the geometry object associated with this feature The caller assumes responsibility for the QgsGeo...
Definition: qgsfeature.cpp:117
QgsFeatureId mFid
feature id
Definition: qgsfeature.h:295
void setGeometryAndOwnership(unsigned char *geom, size_t length)
Set this feature's geometry from WKB.
Definition: qgsfeature.cpp:154
#define tr(sourceText)