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