~ubuntu-branches/ubuntu/wily/qgis/wily

« back to all changes in this revision

Viewing changes to src/core/qgsfeature.h

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
12
12
 *   (at your option) any later version.                                   *
13
13
 *                                                                         *
14
14
 ***************************************************************************/
15
 
/* $Id: qgsfeature.h 6274 2006-12-18 06:06:48Z telwertowski $ */
 
15
/* $Id$ */
16
16
 
17
17
#ifndef QGSFEATURE_H
18
18
#define QGSFEATURE_H
19
19
 
20
 
//#include <geos.h>
21
 
#include <qstring.h>
22
 
#include <map>
23
 
#include <vector>
24
 
 
25
 
#include "qgis.h"
26
 
 
27
 
#include "qgsfeatureattribute.h"
28
 
#include "qgsgeometry.h"
29
 
//#include "qgspoint.h"
30
 
 
31
 
class QgsRect;
32
 
 
33
 
/** \class QgsFeature - Feature attribute class.
34
 
 * Encapsulates a single feature including id and field/value.
35
 
 *@author Gary E.Sherman
 
20
#include <QMap>
 
21
#include <QString>
 
22
#include <QVariant>
 
23
#include <QList>
 
24
 
 
25
class QgsGeometry;
 
26
class QgsRectangle;
 
27
class QgsFeature;
 
28
 
 
29
// key = field index, value = field value
 
30
typedef QMap<int, QVariant> QgsAttributeMap;
 
31
 
 
32
// key = feature id, value = changed attributes
 
33
typedef QMap<int, QgsAttributeMap> QgsChangedAttributesMap;
 
34
 
 
35
// key = feature id, value = changed geometry
 
36
typedef QMap<int, QgsGeometry> QgsGeometryMap;
 
37
 
 
38
// key = field index, value = field name
 
39
typedef QMap<int, QString> QgsFieldNameMap;
 
40
 
 
41
typedef QList<QgsFeature> QgsFeatureList;
 
42
 
 
43
/** \ingroup core
 
44
 * The feature class encapsulates a single feature including its id,
 
45
 * geometry and a list of field/values attributes.
 
46
 *
 
47
 * @author Gary E.Sherman
36
48
 */
37
 
 
38
 
class QgsFeature {
39
 
 
 
49
class CORE_EXPORT QgsFeature
 
50
{
40
51
  public:
41
 
 
42
52
    //! Constructor
43
 
    QgsFeature();
44
 
    QgsFeature(int id, QString const & typeName = "" );
45
 
 
46
 
    /** create a copy of this feature in its uncommitted state.
47
 
        To do this, you also pass in a reference to the feature's
48
 
        layer's uncommitted attribute and geometry changes.
49
 
        The resulting feature will have those changes applied.
50
 
        
51
 
        This is useful in the cut/copy routine, where you'd
52
 
        want a copy of the "current" feature, not the on-disk feature.
53
 
     */
54
 
    QgsFeature( QgsFeature const & rhs,
55
 
                std::map<int,std::map<QString,QString> > & changedAttributes,
56
 
                std::map<int, QgsGeometry> & changedGeometries );
 
53
    QgsFeature( int id = 0, QString typeName = "" );
57
54
 
58
55
    /** copy ctor needed due to internal pointer */
59
56
    QgsFeature( QgsFeature const & rhs );
64
61
    //! Destructor
65
62
    ~QgsFeature();
66
63
 
67
 
    
 
64
 
68
65
    /**
69
66
     * Get the feature id for this feature
70
67
     * @return Feature id
71
68
     */
72
 
    int featureId() const;
 
69
    int id() const;
73
70
 
74
71
    /**
75
72
     * Set the feature id for this feature
76
73
     * @param id Feature id
77
74
     */
78
 
     void setFeatureId(int id);
 
75
    void setFeatureId( int id );
79
76
 
80
77
 
81
78
    /** returns the feature's type name
82
79
     */
83
 
     QString const & typeName() const;
 
80
    QString typeName() const;
84
81
 
85
82
 
86
83
    /** sets the feature's type name
87
84
     */
88
 
     void typeName( QString const & typeName );
 
85
    void setTypeName( QString typeName );
89
86
 
90
87
    /**
91
88
     * Get the attributes for this feature.
92
89
     * @return A std::map containing the field name/value mapping
93
90
     */
94
 
    const std::vector<QgsFeatureAttribute>& attributeMap();
95
 
 
96
 
    /** 
 
91
    const QgsAttributeMap& attributeMap() const;
 
92
 
 
93
    /**Sets all the attributes in one go*/
 
94
    void setAttributeMap( const QgsAttributeMap& attributeMap );
 
95
 
 
96
    /**
97
97
     * Add an attribute to the map
98
98
     */
99
 
    void addAttribute(QString const & field, QString const & value = "", bool numeric = false);
 
99
    void addAttribute( int field, QVariant attr );
100
100
 
101
101
    /**Deletes an attribute and its value*/
102
 
    void deleteAttribute(const QString& name);
 
102
    void deleteAttribute( int field );
103
103
 
104
104
    /**Changes an existing attribute value
105
 
       @param name attribute name
106
 
       @param newval new value*/
107
 
    void changeAttributeValue(const QString& name, const QString& newval);
108
 
 
109
 
    /**Changes an existing attribute name.  The value is unchanged.
110
 
       @param name attribute name
111
 
       @param newname new name*/
112
 
    void changeAttributeName(const QString& name, const QString& newname);
113
 
 
114
 
    /**
115
 
     * Get the fields for this feature
116
 
     * @return A std::map containing field position (index) and field name
117
 
     */
118
 
    const std::map<int, QString>& fields();
 
105
       @param field index of the field
 
106
       @param attr attribute name and value to be set */
 
107
    void changeAttribute( int field, QVariant attr );
119
108
 
120
109
    /**
121
110
     * Return the validity of this feature. This is normally set by
124
113
     */
125
114
    bool isValid() const;
126
115
 
127
 
    /** 
 
116
    /**
128
117
     * Set the validity of the feature.
129
118
     */
130
 
    void setValid(bool validity);
131
 
    
 
119
    void setValid( bool validity );
 
120
 
132
121
    /**
133
122
     * Return the dirty state of this feature.
134
123
     * Dirty is set if (e.g.) the feature's geometry has been modified in-memory.
135
124
     */
136
125
    bool isDirty() const;
137
126
 
138
 
    /** 
 
127
    /**
139
128
     * Reset the dirtiness of the feature.  (i.e. make clean)
140
129
     * You would normally do this after it's saved to permanent storage (e.g. disk, an ACID-compliant database)
141
130
     */
142
 
    void resetDirty();
143
 
    
 
131
    void clean();
 
132
 
144
133
    /**
145
134
     * Get the geometry object associated with this feature
146
135
     */
147
 
    QgsGeometry * geometry();
148
 
    
 
136
    QgsGeometry *geometry();
 
137
 
149
138
    /**
150
139
     * Get the geometry object associated with this feature
151
140
     * The caller assumes responsibility for the QgsGeometry*'s destruction.
152
141
     */
153
 
    QgsGeometry * geometryAndOwnership();
154
 
    
155
 
    /** gets the most recent in-memory version of the geometry (deprecated function in favour of geometry()) */
156
 
    unsigned char * getGeometry() const;
157
 
 
158
 
//     /** gets only the committed version of the geometry */
159
 
//     unsigned char * getCommittedGeometry() const;
160
 
//     
161
 
//     /** gets the most recent in-memory version of the geometry only
162
 
//         if it has been modified since committed (isDirty() == TRUE) */
163
 
//     unsigned char * getModifiedGeometry() const;
164
 
 
165
 
    size_t getGeometrySize() const;
166
 
 
167
 
/*    size_t getCommittedGeometrySize() const;
168
 
 
169
 
    size_t getModifiedGeometrySize() const;*/
170
 
    
171
 
    QString const& wellKnownText() const; 
 
142
    QgsGeometry *geometryAndOwnership();
172
143
 
173
144
    /** Set this feature's geometry from another QgsGeometry object (deep copy)
174
145
     */
175
 
    void setGeometry(QgsGeometry& geom);
176
 
    
177
 
    /** 
 
146
    void setGeometry( const QgsGeometry& geom );
 
147
 
 
148
    /** Set this feature's geometry (takes geometry ownership)
 
149
     */
 
150
    void setGeometry( QgsGeometry* geom );
 
151
 
 
152
    /**
178
153
     * Set this feature's geometry from WKB
179
154
     *
180
155
     * This feature assumes responsibility for destroying geom.
181
156
     */
182
 
    void setGeometryAndOwnership(unsigned char * geom, size_t length);
183
 
    
184
 
    /** Set bulk-modified WKB geometry 
185
 
        \note   this function assumes the Geometry is not committed. 
186
 
     */
187
 
/*    void setModifiedGeometry(unsigned char * geom, size_t length);*/
188
 
    
189
 
    /** Insert a new vertex before the given vertex number,
190
 
     *  ring and item (first number is index 0)
191
 
     *  Not meaningful for Point geometries
192
 
     */
193
 
//     bool insertVertexBefore(double x, double y, int beforeVertex = 0, int atRing = 0, int atItem = 0);
194
 
 
195
 
    /** Moves the vertex at the given position number,
196
 
     *  ring and item (first number is index 0)
197
 
     *  to the given coordinates
198
 
     */
199
 
/*    bool moveVertexAt(double x, double y, int atVertex = 0, int atRing = 0, int atItem = 0);*/
200
 
    
201
 
    /**
202
 
     *  Modifies x and y to indicate the location of
203
 
     *  the vertex at the given position number,
204
 
     *  ring and item (first number is index 0)
205
 
     *  to the given coordinates
206
 
     */
207
 
/*    bool vertexAt(double &x, double &y, int atVertex = 0, int atRing = 0, int atItem = 0) const;*/
208
 
 
209
 
//     /**Test for intersection with a rectangle (uses GEOS)*/
210
 
//     bool intersects(QgsRect* r) const;
211
 
 
212
 
    /**Returns the Vertex closest to a given point*/
213
 
//     QgsPoint closestVertex(const QgsPoint& point) const;
214
 
 
215
 
    /** Returns the line segment closest to the given point in beforeVertex, atRing and atItem
216
 
        Returns the SQUARE of the closest distance in minDist.
217
 
        Returns the closest point on the line segment to the given point
218
 
        
219
 
                
220
 
        TODO: point handling
221
 
        TODO: const correctness
222
 
     */
223
 
//    QgsPoint closestSegment(QgsPoint& point, 
224
 
//                            QgsPoint& segStart, QgsPoint& segStop,
225
 
//                            double& minSqrDist);
226
 
 
227
 
//     QgsPoint QgsFeature::closestSegmentWithContext(QgsPoint& point, 
228
 
//                                                    int& beforeVertex, int& atRing, int& atItem,
229
 
//                                                    double& minSqrDist);
230
 
//                             
231
 
//                             
232
 
     /**Returns the bounding box of this feature*/
233
 
     QgsRect boundingBox() const;
234
 
// 
235
 
     /** Creates a geos geometry from this features geometry. Note, that the returned object needs to be deleted.
236
 
         @note  This function is deprecated - use geometry()->geosGeometry() instead.
237
 
      */
238
 
     GEOS_GEOM::Geometry* geosGeometry() const;
 
157
    void setGeometryAndOwnership( unsigned char * geom, size_t length );
239
158
 
240
159
  private:
241
160
 
242
161
    //! feature id
243
162
    int mFid;
244
163
 
245
 
    //! std::map containing field name/value pairs
246
 
    std::vector<QgsFeatureAttribute> attributes;
247
 
 
248
 
    //! std::map containing the field index and name
249
 
    std::map<int, QString> fieldNames;
 
164
    /** map of attributes accessed by field index */
 
165
    QgsAttributeMap mAttributes;
250
166
 
251
167
    /** pointer to geometry in binary WKB format
252
168
 
253
169
       This is usually set by a call to OGRGeometry::exportToWkb()
254
170
     */
255
 
    QgsGeometry* mGeometry;
256
 
    
 
171
    QgsGeometry *mGeometry;
 
172
 
257
173
    /** Indicator if the mGeometry is owned by this QgsFeature.
258
174
        If so, this QgsFeature takes responsibility for the mGeometry's destruction.
259
 
     */ 
260
 
    bool mOwnsGeometry;   
261
 
 
262
 
//     /** pointer to modified (dirty / uncommitted) geometry in binary WKB format
263
 
//         This is only valid if isDirty().
264
 
//      */
265
 
//     unsigned char * modifiedGeometry;
266
 
//     
267
 
//     /** size of geometry */
268
 
//     size_t geometrySize;
269
 
// 
270
 
//     /** size of modified geometry */
271
 
//     size_t modifiedGeometrySize;
 
175
     */
 
176
    bool mOwnsGeometry;
272
177
 
273
178
    //! Flag to indicate if this feature is valid
 
179
    // TODO: still applies? [MD]
274
180
    bool mValid;
275
181
 
276
182
    //! Flag to indicate if this feature is dirty (e.g. geometry has been modified in-memory)
 
183
    // TODO: still applies? [MD]
277
184
    bool mDirty;
278
185
 
279
186
    /// feature type name
280
187
    QString mTypeName;
281
188
 
282
 
//     /**WKT representation of the geometry*/
283
 
//     mutable QString mWKT;
284
 
// 
285
 
//     /**Exports the current WKB to mWKT
286
 
//      @return true in case of success and false else*/
287
 
//     bool exportToWKT(unsigned char * geom) const;
288
 
//     bool exportToWKT() const;
289
 
// 
290
 
//     /** Squared distance from point to the given line segment 
291
 
//      *  TODO: Perhaps move this to QgsPoint
292
 
//      */
293
 
//     double distanceSquaredPointToSegment(QgsPoint& point,
294
 
//                                          double *x1, double *y1,
295
 
//                                          double *x2, double *y2,
296
 
//                                          QgsPoint& minDistPoint);
297
189
 
298
190
}; // class QgsFeature
299
191
 
 
192
 
300
193
#endif