QGIS API Documentation  2.12.0-Lyon
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 "qgsfield.h"
19 #include "qgsgeometry.h"
20 #include "qgsrectangle.h"
21 
22 #include "qgsmessagelog.h"
23 
24 #include <QDataStream>
25 
27 {
28  d = new QgsFeaturePrivate( id );
29 }
30 
32 {
33  d = new QgsFeaturePrivate( id );
34  d->fields = fields;
35  initAttributes( d->fields.count() );
36 }
37 
39  : d( rhs.d )
40 {
41 }
42 
44 {
45  d = rhs.d;
46  return *this;
47 }
48 
50 {
51 }
52 
54 {
55  return d->fid;
56 }
57 
58 void QgsFeature::deleteAttribute( int field )
59 {
60  d.detach();
61  d->attributes.remove( field );
62 }
63 
65 {
66  d.detach();
67  return d->geometry;
68 }
69 
71 {
72  return d->geometry;
73 }
74 
76 {
77  d.detach();
78  d->ownsGeometry = false;
79 
80  return d->geometry;
81 }
82 
84 {
85  if ( id == d->fid )
86  return;
87 
88  d.detach();
89  d->fid = id;
90 }
91 
93 {
94  return d->attributes;
95 }
96 
98 {
99  if ( attrs == d->attributes )
100  return;
101 
102  d.detach();
103  d->attributes = attrs;
104 }
105 
107 {
108  setGeometry( new QgsGeometry( geom ) );
109 }
110 
112 {
113  // we do a little bit of trickery here to avoid an unnecessary deep copy
114  // of the existing geometry by the detach function
115  // (since we are replacing the geometry anyway)
116 
117  //first, store the old ownsGeometry status
118  QgsFeaturePrivate* old_d = d.data();
119  bool ownedGeom = d->ownsGeometry;
120 
121  //then set owns geometry to false before the detach, so that the deep copy
122  //is not made
123  d->ownsGeometry = false;
124  d.detach();
125 
126  //restore ownsGeometry setting if a detach was made
127  if ( old_d != d.data() )
128  {
129  old_d->ownsGeometry = ownedGeom;
130  }
131  else if ( ownedGeom )
132  {
133  delete d->geometry;
134  }
135 
136  d->geometry = geom;
137  d->ownsGeometry = true;
138 }
139 
142 void QgsFeature::setGeometryAndOwnership( unsigned char *geom, size_t length )
143 {
144  QgsGeometry *g = new QgsGeometry();
145  g->fromWkb( geom, length );
146  setGeometry( g );
147 }
148 
149 void QgsFeature::setFields( const QgsFields* fields, bool init )
150 {
151  setFields( *fields, init );
152 }
153 
154 void QgsFeature::setFields( const QgsFields &fields, bool init )
155 {
156  d.detach();
157  d->fields = fields;
158  if ( init )
159  {
160  initAttributes( d->fields.count() );
161  }
162 }
163 
165 {
166  return &( d->fields );
167 }
168 
169 
171 {
172  return d->valid;
173 }
174 
175 void QgsFeature::setValid( bool validity )
176 {
177  if ( d->valid == validity )
178  return;
179 
180  d.detach();
181  d->valid = validity;
182 }
183 
184 void QgsFeature::initAttributes( int fieldCount )
185 {
186  d.detach();
187  d->attributes.resize( fieldCount );
188  QVariant* ptr = d->attributes.data();
189  for ( int i = 0; i < fieldCount; ++i, ++ptr )
190  ptr->clear();
191 }
192 
193 
194 bool QgsFeature::setAttribute( int idx, const QVariant &value )
195 {
196  if ( idx < 0 || idx >= d->attributes.size() )
197  {
198  QgsMessageLog::logMessage( QObject::tr( "Attribute index %1 out of bounds [0;%2]" ).arg( idx ).arg( d->attributes.size() ), QString::null, QgsMessageLog::WARNING );
199  return false;
200  }
201 
202  d.detach();
203  d->attributes[idx] = value;
204  return true;
205 }
206 
207 bool QgsFeature::setAttribute( const QString& name, const QVariant& value )
208 {
209  int fieldIdx = fieldNameIndex( name );
210  if ( fieldIdx == -1 )
211  return false;
212 
213  d.detach();
214  d->attributes[fieldIdx] = value;
215  return true;
216 }
217 
219 {
220  int fieldIdx = fieldNameIndex( name );
221  if ( fieldIdx == -1 )
222  return false;
223 
224  d.detach();
225  d->attributes[fieldIdx].clear();
226  return true;
227 }
228 
229 QVariant QgsFeature::attribute( int fieldIdx ) const
230 {
231  if ( fieldIdx < 0 || fieldIdx >= d->attributes.count() )
232  return QVariant();
233 
234  return d->attributes.at( fieldIdx );
235 }
236 
237 
239 {
240  int fieldIdx = fieldNameIndex( name );
241  if ( fieldIdx == -1 )
242  return QVariant();
243 
244  return d->attributes.at( fieldIdx );
245 }
246 
247 int QgsFeature::fieldNameIndex( const QString& fieldName ) const
248 {
249  return d->fields.fieldNameIndex( fieldName );
250 }
251 
253 {
254  out << feature.id();
255  out << feature.attributes();
256  if ( feature.constGeometry() )
257  {
258  out << *( feature.constGeometry() );
259  }
260  else
261  {
262  QgsGeometry geometry;
263  out << geometry;
264  }
265  out << feature.isValid();
266  return out;
267 }
268 
270 {
271  QgsFeatureId id;
272  QgsGeometry* geometry = new QgsGeometry();
273  bool valid;
274  QgsAttributes attr;
275  in >> id >> attr >> *geometry >> valid;
276  feature.setFeatureId( id );
277  feature.setGeometry( geometry );
278  feature.setAttributes( attr );
279  feature.setValid( valid );
280  return in;
281 }
QgsFeatureId id() const
Get the feature ID for this feature.
Definition: qgsfeature.cpp:53
bool isValid() const
Returns the validity of this feature.
Definition: qgsfeature.cpp:170
int fieldNameIndex(const QString &fieldName) const
Utility method to get attribute index from name.
Definition: qgsfeature.cpp:247
virtual ~QgsFeature()
Destructor.
Definition: qgsfeature.cpp:49
Container of fields for a vector layer.
Definition: qgsfield.h:177
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:76
void setAttributes(const QgsAttributes &attrs)
Sets the feature's attributes.
Definition: qgsfeature.cpp:97
bool setAttribute(int field, const QVariant &attr)
Set an attribute's value by field index.
Definition: qgsfeature.cpp:194
The feature class encapsulates a single feature including its id, geometry and a list of field/values...
Definition: qgsfeature.h:176
QString tr(const char *sourceText, const char *disambiguation, int n)
QDataStream & operator>>(QDataStream &in, QgsFeature &feature)
Reads a feature from stream in into feature.
Definition: qgsfeature.cpp:269
void setGeometry(const QgsGeometry &geom)
Set this feature's geometry from another QgsGeometry object.
Definition: qgsfeature.cpp:106
void deleteAttribute(int field)
Deletes an attribute and its value.
Definition: qgsfeature.cpp:58
void setFeatureId(QgsFeatureId id)
Sets the feature ID for this feature.
Definition: qgsfeature.cpp:83
QgsAttributes attributes() const
Returns the feature's attributes.
Definition: qgsfeature.cpp:92
QDataStream & operator<<(QDataStream &out, const QgsFeature &feature)
Writes the feature to stream out.
Definition: qgsfeature.cpp:252
void initAttributes(int fieldCount)
Initialize this feature with the given number of fields.
Definition: qgsfeature.cpp:184
const QgsFields * fields() const
Returns the field map associated with the feature.
Definition: qgsfeature.cpp:164
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)
QgsFeature & operator=(QgsFeature const &rhs)
Assignment operator.
Definition: qgsfeature.cpp:43
Q_DECL_DEPRECATED 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:149
QgsFeature(QgsFeatureId id=QgsFeatureId())
Constructor for QgsFeature.
Definition: qgsfeature.cpp:26
void clear()
QgsGeometry * geometry()
Get the geometry object associated with this feature.
Definition: qgsfeature.cpp:64
void setValid(bool validity)
Sets the validity of the feature.
Definition: qgsfeature.cpp:175
QVariant attribute(const QString &name) const
Lookup attribute value from attribute name.
Definition: qgsfeature.cpp:238
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...
const QgsGeometry * constGeometry() const
Gets a const pointer to the geometry object associated with this feature.
Definition: qgsfeature.cpp:70
qint64 QgsFeatureId
Definition: qgsfeature.h:31
Q_DECL_DEPRECATED QgsGeometry * geometryAndOwnership()
Get the geometry object associated with this feature, and transfer ownership of the geometry to the c...
Definition: qgsfeature.cpp:75
A vector of attributes.
Definition: qgsfeature.h:109
void setGeometryAndOwnership(unsigned char *geom, size_t length)
Set this feature's geometry from WKB.
Definition: qgsfeature.cpp:142