QGIS API Documentation  2.99.0-Master (37c43df)
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 "qgsfeature_p.h"
18 #include "qgsfields.h"
19 #include "qgsgeometry.h"
20 #include "qgsrectangle.h"
21 
22 #include "qgsmessagelog.h"
23 
24 #include <QDataStream>
25 
26 /***************************************************************************
27  * This class is considered CRITICAL and any change MUST be accompanied with
28  * full unit tests in testqgsfeature.cpp.
29  * See details in QEP #17
30  ****************************************************************************/
31 
32 
34 {
35  QgsAttributeMap map;
36  for ( int idx = 0; idx < count(); ++idx )
37  {
38  QVariant v = at( idx );
39  if ( v.isValid() )
40  map.insert( idx, v );
41  }
42  return map;
43 }
44 
45 //
46 // QgsFeature
47 //
48 
50 {
51  d = new QgsFeaturePrivate( id );
52 }
53 
55 {
56  d = new QgsFeaturePrivate( id );
57  d->fields = fields;
58  initAttributes( d->fields.count() );
59 }
60 
62  : d( rhs.d )
63 {
64 }
65 
67 {
68  d = rhs.d;
69  return *this;
70 }
71 
72 bool QgsFeature::operator ==( const QgsFeature& other ) const
73 {
74  if ( d == other.d )
75  return true;
76 
77  if ( d->fid == other.d->fid
78  && d->valid == other.d->valid
79  && d->fields == other.d->fields
80  && d->attributes == other.d->attributes
81  && d->geometry.equals( other.d->geometry ) )
82  return true;
83 
84  return false;
85 }
86 
87 bool QgsFeature::operator!=( const QgsFeature& other ) const
88 {
89  return !( *this == other );
90 }
91 
93 {
94 }
95 
96 /***************************************************************************
97  * This class is considered CRITICAL and any change MUST be accompanied with
98  * full unit tests in testqgsfeature.cpp.
99  * See details in QEP #17
100  ****************************************************************************/
101 
103 {
104  return d->fid;
105 }
106 
108 {
109  d.detach();
110  d->attributes.remove( field );
111 }
112 
114 {
115  return d->geometry;
116 }
117 
118 /***************************************************************************
119  * This class is considered CRITICAL and any change MUST be accompanied with
120  * full unit tests in testqgsfeature.cpp.
121  * See details in QEP #17
122  ****************************************************************************/
123 
125 {
126  if ( id == d->fid )
127  return;
128 
129  d.detach();
130  d->fid = id;
131 }
132 
133 /***************************************************************************
134  * This class is considered CRITICAL and any change MUST be accompanied with
135  * full unit tests in testqgsfeature.cpp.
136  * See details in QEP #17
137  ****************************************************************************/
138 
140 {
141  if ( id == d->fid )
142  return;
143 
144  d.detach();
145  d->fid = id;
146 }
147 
149 {
150  return d->attributes;
151 }
152 
154 {
155  if ( attrs == d->attributes )
156  return;
157 
158  d.detach();
159  d->attributes = attrs;
160 }
161 
163 {
164  d.detach();
165  d->geometry = geometry;
166 }
167 
169 {
171 }
172 
173 /***************************************************************************
174  * This class is considered CRITICAL and any change MUST be accompanied with
175  * full unit tests in testqgsfeature.cpp.
176  * See details in QEP #17
177  ****************************************************************************/
178 
179 void QgsFeature::setFields( const QgsFields &fields, bool init )
180 {
181  d.detach();
182  d->fields = fields;
183  if ( init )
184  {
185  initAttributes( d->fields.count() );
186  }
187 }
188 
190 {
191  return d->fields;
192 }
193 
194 /***************************************************************************
195  * This class is considered CRITICAL and any change MUST be accompanied with
196  * full unit tests in testqgsfeature.cpp.
197  * See details in QEP #17
198  ****************************************************************************/
199 
201 {
202  return d->valid;
203 }
204 
205 void QgsFeature::setValid( bool validity )
206 {
207  if ( d->valid == validity )
208  return;
209 
210  d.detach();
211  d->valid = validity;
212 }
213 
215 {
216  return !d->geometry.isEmpty();
217 }
218 
219 void QgsFeature::initAttributes( int fieldCount )
220 {
221  d.detach();
222  d->attributes.resize( fieldCount );
223  QVariant* ptr = d->attributes.data();
224  for ( int i = 0; i < fieldCount; ++i, ++ptr )
225  ptr->clear();
226 }
227 
228 bool QgsFeature::setAttribute( int idx, const QVariant &value )
229 {
230  if ( idx < 0 || idx >= d->attributes.size() )
231  {
232  QgsMessageLog::logMessage( QObject::tr( "Attribute index %1 out of bounds [0;%2]" ).arg( idx ).arg( d->attributes.size() ), QString::null, QgsMessageLog::WARNING );
233  return false;
234  }
235 
236  d.detach();
237  d->attributes[idx] = value;
238  return true;
239 }
240 
241 /***************************************************************************
242  * This class is considered CRITICAL and any change MUST be accompanied with
243  * full unit tests in testqgsfeature.cpp.
244  * See details in QEP #17
245  ****************************************************************************/
246 
247 bool QgsFeature::setAttribute( const QString& name, const QVariant& value )
248 {
249  int fieldIdx = fieldNameIndex( name );
250  if ( fieldIdx == -1 )
251  return false;
252 
253  d.detach();
254  d->attributes[fieldIdx] = value;
255  return true;
256 }
257 
258 bool QgsFeature::deleteAttribute( const QString& name )
259 {
260  int fieldIdx = fieldNameIndex( name );
261  if ( fieldIdx == -1 )
262  return false;
263 
264  d.detach();
265  d->attributes[fieldIdx].clear();
266  return true;
267 }
268 
269 QVariant QgsFeature::attribute( int fieldIdx ) const
270 {
271  if ( fieldIdx < 0 || fieldIdx >= d->attributes.count() )
272  return QVariant();
273 
274  return d->attributes.at( fieldIdx );
275 }
276 
277 QVariant QgsFeature::attribute( const QString& name ) const
278 {
279  int fieldIdx = fieldNameIndex( name );
280  if ( fieldIdx == -1 )
281  return QVariant();
282 
283  return d->attributes.at( fieldIdx );
284 }
285 
286 /***************************************************************************
287  * This class is considered CRITICAL and any change MUST be accompanied with
288  * full unit tests in testqgsfeature.cpp.
289  * See details in QEP #17
290  ****************************************************************************/
291 
292 int QgsFeature::fieldNameIndex( const QString& fieldName ) const
293 {
294  return d->fields.lookupField( fieldName );
295 }
296 
297 /***************************************************************************
298  * This class is considered CRITICAL and any change MUST be accompanied with
299  * full unit tests in testqgsfeature.cpp.
300  * See details in QEP #17
301  ****************************************************************************/
302 
303 QDataStream& operator<<( QDataStream& out, const QgsFeature& feature )
304 {
305  out << feature.id();
306  out << feature.attributes();
307  if ( feature.hasGeometry() )
308  {
309  out << ( feature.geometry() );
310  }
311  else
312  {
314  out << geometry;
315  }
316  out << feature.isValid();
317  return out;
318 }
319 
320 QDataStream& operator>>( QDataStream& in, QgsFeature& feature )
321 {
324  bool valid;
325  QgsAttributes attr;
326  in >> id >> attr >> geometry >> valid;
327  feature.setFeatureId( id );
328  feature.setGeometry( geometry );
329  feature.setAttributes( attr );
330  feature.setValid( valid );
331  return in;
332 }
333 
334 uint qHash( const QgsFeature& key, uint seed )
335 {
336  uint hash = seed;
337  Q_FOREACH ( const QVariant& attr, key.attributes() )
338  {
339  hash ^= qHash( attr.toString() );
340  }
341 
342  hash ^= qHash( key.geometry().exportToWkt() );
343  hash ^= qHash( key.id() );
344 
345  return hash;
346 }
347 
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:200
QgsFeatureId id
Definition: qgsfeature.h:139
QgsAttributes attributes() const
Returns the feature&#39;s attributes.
QgsFields fields() const
Returns the field map associated with the feature.
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:179
QMap< int, QVariant > QgsAttributeMap
Definition: qgsfeature.h:44
virtual ~QgsFeature()
Destructor.
Definition: qgsfeature.cpp:92
QgsFeature & operator=(const QgsFeature &rhs)
Assignment operator.
Definition: qgsfeature.cpp:66
bool operator!=(const QgsFeature &other) const
Compares two features.
Definition: qgsfeature.cpp:87
uint qHash(const QgsFeature &key, uint seed)
Definition: qgsfeature.cpp:334
Container of fields for a vector layer.
Definition: qgsfields.h:36
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:78
void setAttributes(const QgsAttributes &attrs)
Sets the feature&#39;s attributes.
Definition: qgsfeature.cpp:153
bool setAttribute(int field, const QVariant &attr)
Set an attribute&#39;s value by field index.
Definition: qgsfeature.cpp:228
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:135
bool hasGeometry() const
Returns true if the feature has an associated geometry.
Definition: qgsfeature.cpp:214
QDataStream & operator>>(QDataStream &in, QgsFeature &feature)
Reads a feature from stream in into feature. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:320
void deleteAttribute(int field)
Deletes an attribute and its value.
Definition: qgsfeature.cpp:107
void setFeatureId(QgsFeatureId id)
Sets the feature ID for this feature.
Definition: qgsfeature.cpp:124
QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfeature.cpp:303
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:219
void seed(uint32_t value)
static void logMessage(const QString &message, const QString &tag=QString::null, MessageLevel level=WARNING)
add a message to the instance (and create it if necessary)
QgsAttributeMap toMap() const
Returns a QgsAttributeMap of the attribute values.
Definition: qgsfeature.cpp:33
QString exportToWkt(int precision=17) const
Exports the geometry to WKT.
void setId(QgsFeatureId id)
Sets the feature ID for this feature.
Definition: qgsfeature.cpp:139
QgsGeometry geometry() const
Returns the geometry associated with this feature.
Definition: qgsfeature.cpp:113
QgsFeature(QgsFeatureId id=QgsFeatureId())
Constructor for QgsFeature.
Definition: qgsfeature.cpp:49
bool operator==(const QgsFeature &other) const
Compares two features.
Definition: qgsfeature.cpp:72
QgsFeatureId id() const
Get the feature ID for this feature.
void setValid(bool validity)
Sets the validity of the feature.
Definition: qgsfeature.cpp:205
int fieldNameIndex(const QString &fieldName) const
Utility method to get attribute index from name.
Definition: qgsfeature.cpp:292
void clearGeometry()
Removes any geometry associated with the feature.
Definition: qgsfeature.cpp:168
void setGeometry(const QgsGeometry &geometry)
Set the feature&#39;s geometry.
Definition: qgsfeature.cpp:162
qint64 QgsFeatureId
Definition: qgsfeature.h:32
A vector of attributes.
Definition: qgsfeature.h:55
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:277
QgsAttributes attributes
Definition: qgsfeature.h:140