Skip to content

Commit 8bf9590

Browse files
committed
QgsFeature: use a vector of attributes instead of a map (for faster access)
1 parent 4bc60ff commit 8bf9590

File tree

99 files changed

+685
-809
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+685
-809
lines changed

python/core/diagram/qgsdiagram.sip

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ class QgsDiagram
66
public:
77
virtual ~QgsDiagram();
88
/**Draws the diagram at the given position (in pixel coordinates)*/
9-
virtual void renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position ) = 0;
9+
virtual void renderDiagram( const QgsAttributes& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position ) = 0;
1010
virtual QString diagramName() const = 0;
1111
/**Returns the size in map units the diagram will use to render.*/
12-
virtual QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s ) = 0;
12+
virtual QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s ) = 0;
1313
/**Returns the size in map units the diagram will use to render. Interpolate size*/
14-
virtual QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is ) = 0;
14+
virtual QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is ) = 0;
1515

1616
protected:
1717
/** Changes the pen width to match the current settings and rendering context

python/core/diagram/qgshistogramdiagram.sip

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class QgsHistogramDiagram: QgsDiagram
77
QgsHistogramDiagram();
88
~QgsHistogramDiagram();
99

10-
void renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position );
11-
QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s );
12-
QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is );
10+
void renderDiagram( const QgsAttributes& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position );
11+
QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s );
12+
QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is );
1313
QString diagramName() const;
1414
};

python/core/diagram/qgspiediagram.sip

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ class QgsPieDiagram: QgsDiagram
77
QgsPieDiagram();
88
~QgsPieDiagram();
99

10-
void renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position );
11-
QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s );
12-
QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is );
10+
void renderDiagram( const QgsAttributes& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position );
11+
QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s );
12+
QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is );
1313
QString diagramName() const;
1414
};

python/core/diagram/qgstextdiagram.sip

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class QgsTextDiagram: QgsDiagram
2020

2121
QgsTextDiagram();
2222
~QgsTextDiagram();
23-
void renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position );
24-
QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s );
25-
QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is );
23+
void renderDiagram( const QgsAttributes& att, QgsRenderContext& c, const QgsDiagramSettings& s, const QPointF& position );
24+
QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s );
25+
QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c, const QgsDiagramSettings& s, const QgsDiagramInterpolationSettings& is );
2626

2727
QString diagramName() const;
2828
};

python/core/qgsdiagramrendererv2.sip

+8-8
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,14 @@ class QgsDiagramRendererV2
119119
virtual ~QgsDiagramRendererV2();
120120

121121
/**Returns size of the diagram for feature f in map units. Returns an invalid QSizeF in case of error*/
122-
virtual QSizeF sizeMapUnits( const QgsAttributeMap& attributes, const QgsRenderContext& c );
122+
virtual QSizeF sizeMapUnits( const QgsAttributes& attributes, const QgsRenderContext& c );
123123

124124
virtual QString rendererName() const = 0;
125125

126126
/**Returns attribute indices needed for diagram rendering*/
127127
virtual QList<int> diagramAttributes() const = 0;
128128

129-
void renderDiagram( const QgsAttributeMap& att, QgsRenderContext& c, const QPointF& pos );
129+
void renderDiagram( const QgsAttributes& att, QgsRenderContext& c, const QPointF& pos );
130130

131131
void setDiagram( QgsDiagram* d );
132132
const QgsDiagram* diagram() const;
@@ -144,10 +144,10 @@ class QgsDiagramRendererV2
144144
* @param c render context
145145
* @param s out: diagram settings for the feature
146146
*/
147-
virtual bool diagramSettings( const QgsAttributeMap& att, const QgsRenderContext& c, QgsDiagramSettings& s ) = 0;
147+
virtual bool diagramSettings( const QgsAttributes& att, const QgsRenderContext& c, QgsDiagramSettings& s ) = 0;
148148

149149
/**Returns size of the diagram (in painter units) or an invalid size in case of error*/
150-
virtual QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c ) = 0;
150+
virtual QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c ) = 0;
151151

152152
/**Converts size from mm to map units*/
153153
void convertSizeToMapUnits( QSizeF& size, const QgsRenderContext& context ) const;
@@ -183,9 +183,9 @@ class QgsSingleCategoryDiagramRenderer : QgsDiagramRendererV2
183183
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
184184

185185
protected:
186-
bool diagramSettings( const QgsAttributeMap&, const QgsRenderContext& c, QgsDiagramSettings& s );
186+
bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );
187187

188-
QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c );
188+
QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c );
189189
};
190190

191191
class QgsLinearlyInterpolatedDiagramRenderer : QgsDiagramRendererV2
@@ -226,7 +226,7 @@ class QgsLinearlyInterpolatedDiagramRenderer : QgsDiagramRendererV2
226226
void writeXML( QDomElement& layerElem, QDomDocument& doc ) const;
227227

228228
protected:
229-
bool diagramSettings( const QgsAttributeMap&, const QgsRenderContext& c, QgsDiagramSettings& s );
229+
bool diagramSettings( const QgsAttributes&, const QgsRenderContext& c, QgsDiagramSettings& s );
230230

231-
QSizeF diagramSize( const QgsAttributeMap& attributes, const QgsRenderContext& c );
231+
QSizeF diagramSize( const QgsAttributes& attributes, const QgsRenderContext& c );
232232
};

python/core/qgsfeature.sip

+15-39
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// key = field index, value = field value
44
typedef QMap<int, QVariant> QgsAttributeMap;
55

6+
typedef QVector<QVariant> QgsAttributes;
7+
68
// key = feature id, value = changed attributes
79
typedef QMap<qint64, QMap<int, QVariant> > QgsChangedAttributesMap;
810

@@ -30,13 +32,12 @@ class QgsFeature
3032

3133
SIP_PYOBJECT __getitem__(int key);
3234
%MethodCode
33-
const QgsAttributeMap& attrMap = sipCpp->attributeMap();
34-
QgsAttributeMap::const_iterator it = attrMap.find(a0);
35-
if (it == attrMap.end())
35+
const QgsAttributes& attrs = sipCpp->attributes();
36+
if (a0 < 0 || a0 >= attrs.count())
3637
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
3738
else
3839
{
39-
QVariant* v = new QVariant(it.value());
40+
QVariant* v = new QVariant(attrs[a0]);
4041
sipRes = sipConvertFromInstance(v, sipClass_QVariant, Py_None);
4142
}
4243
%End
@@ -48,14 +49,14 @@ class QgsFeature
4849
PyErr_SetString(PyExc_KeyError, a0->toAscii());
4950
else
5051
{
51-
QVariant* v = new QVariant( sipCpp->attributeMap().value(fieldIdx) );
52+
QVariant* v = new QVariant( sipCpp->attribute(fieldIdx) );
5253
sipRes = sipConvertFromInstance(v, sipClass_QVariant, Py_None);
5354
}
5455
%End
5556

5657
void __setitem__(int key, QVariant value);
5758
%MethodCode
58-
sipCpp->addAttribute(a0, *a1);
59+
sipCpp->setAttribute(a0, *a1);
5960
%End
6061

6162
void __setitem__(const QString& key, QVariant value);
@@ -65,13 +66,13 @@ class QgsFeature
6566
PyErr_SetString(PyExc_KeyError, a0->toAscii());
6667
else
6768
{
68-
sipCpp->addAttribute(fieldIdx, *a1);
69+
sipCpp->setAttribute(fieldIdx, *a1);
6970
}
7071
%End
7172

7273
void __delitem__(int key);
7374
%MethodCode
74-
if (sipCpp->attributeMap().contains(a0))
75+
if (a0 >= 0 && a0 < sipCpp->attributes().count())
7576
sipCpp->deleteAttribute(a0);
7677
else
7778
PyErr_SetString(PyExc_KeyError, QByteArray::number(a0));
@@ -107,34 +108,15 @@ class QgsFeature
107108
*/
108109
void setFeatureId( qint64 id );
109110

110-
/**
111-
* Get the attributes for this feature.
112-
* @return A std::map containing the field name/value mapping
113-
*/
114-
const QMap<int, QVariant> & attributeMap() const;
115-
116-
117-
/**Sets all the attributes in one go*/
118-
void setAttributeMap( const QMap<int, QVariant> & attributeMap );
119-
120-
/** Clear attribute map
121-
* added in 1.5
122-
*/
123-
void clearAttributeMap();
124-
125-
/**
126-
* Add an attribute to the map
127-
*/
128-
void addAttribute( int field, QVariant attr );
111+
const QgsAttributes& attributes() const;
112+
//QgsAttributes& attributes();
113+
void setAttributes(const QgsAttributes& attrs);
114+
void setAttribute( int field, const QVariant& attr );
115+
void initAttributes( int fieldCount );
129116

130117
/**Deletes an attribute and its value*/
131118
void deleteAttribute( int field );
132119

133-
/**Changes an existing attribute value
134-
@param field index of the field
135-
@param attr attribute name and value to be set */
136-
void changeAttribute( int field, QVariant attr );
137-
138120
/**
139121
* Return the validity of this feature. This is normally set by
140122
* the provider to indicate some problem that makes the feature
@@ -188,13 +170,7 @@ class QgsFeature
188170
* Field map must be associated to make this work.
189171
* @note added in 2.0
190172
*/
191-
bool addAttribute( const QString& name, QVariant value );
192-
193-
/** Change a value of an attribute. Returns false if attribute name could not be converted to index.
194-
* Field map must be associated to make this work.
195-
* @note added in 2.0
196-
*/
197-
bool changeAttribute( const QString& name, QVariant value );
173+
bool setAttribute( const QString& name, QVariant value );
198174

199175
/** Remove an attribute value. Returns false if attribute name could not be converted to index.
200176
* Field map must be associated to make this work.

scripts/astyle.sh

100644100755
File mode changed.

src/analysis/interpolation/DualEdgeTriangulation.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -3134,6 +3134,7 @@ bool DualEdgeTriangulation::saveAsShapefile( const QString& fileName ) const
31343134
lineGeom.push_back( QgsPoint( p2->getX(), p2->getY() ) );
31353135
QgsGeometry* geom = QgsGeometry::fromPolyline( lineGeom );
31363136
edgeLineFeature.setGeometry( geom );
3137+
edgeLineFeature.initAttributes( 1 );
31373138

31383139
//attributes
31393140
QString attributeString;
@@ -3148,7 +3149,7 @@ bool DualEdgeTriangulation::saveAsShapefile( const QString& fileName ) const
31483149
attributeString = "structure line";
31493150
}
31503151
}
3151-
edgeLineFeature.addAttribute( 0, attributeString );
3152+
edgeLineFeature.setAttribute( 0, attributeString );
31523153

31533154
writer.addFeature( edgeLineFeature );
31543155
}

src/analysis/interpolation/qgsinterpolator.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,12 @@ int QgsInterpolator::cacheBaseData()
7777
{
7878
if ( !v_it->zCoordInterpolation )
7979
{
80-
QgsAttributeMap attMap = theFeature.attributeMap();
81-
QgsAttributeMap::const_iterator att_it = attMap.find( v_it->interpolationAttribute );
82-
if ( att_it == attMap.end() ) //attribute not found, something must be wrong (e.g. NULL value)
80+
QVariant attributeVariant = theFeature.attribute( v_it->interpolationAttribute );
81+
if ( !attributeVariant.isValid() ) //attribute not found, something must be wrong (e.g. NULL value)
8382
{
8483
continue;
8584
}
86-
attributeValue = att_it.value().toDouble( &attributeConversionOk );
85+
attributeValue = attributeVariant.toDouble( &attributeConversionOk );
8786
if ( !attributeConversionOk || qIsNaN( attributeValue ) ) //don't consider vertices with attributes like 'nan' for the interpolation
8887
{
8988
continue;

src/analysis/interpolation/qgstininterpolator.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,12 @@ int QgsTINInterpolator::insertData( QgsFeature* f, bool zCoord, int attr, InputT
184184
bool attributeConversionOk = false;
185185
if ( !zCoord )
186186
{
187-
QgsAttributeMap attMap = f->attributeMap();
188-
QgsAttributeMap::const_iterator att_it = attMap.find( attr );
189-
if ( att_it == attMap.end() ) //attribute not found, something must be wrong (e.g. NULL value)
187+
QVariant attributeVariant = f->attribute( attr );
188+
if ( !attributeVariant.isValid() ) //attribute not found, something must be wrong (e.g. NULL value)
190189
{
191190
return 3;
192191
}
193-
attributeValue = att_it.value().toDouble( &attributeConversionOk );
192+
attributeValue = attributeVariant.toDouble( &attributeConversionOk );
194193
if ( !attributeConversionOk || qIsNaN( attributeValue ) ) //don't consider vertices with attributes like 'nan' for the interpolation
195194
{
196195
return 4;

src/analysis/network/qgslinevectorlayerdirector.cpp

+12-20
Original file line numberDiff line numberDiff line change
@@ -280,29 +280,21 @@ void QgsLineVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, c
280280
vl->select( la );
281281
while ( vl->nextFeature( feature ) )
282282
{
283-
QgsAttributeMap attr = feature.attributeMap();
284283
int directionType = mDefaultDirection;
285-
QgsAttributeMap::const_iterator it;
284+
286285
// What direction have feature?
287-
for ( it = attr.constBegin(); it != attr.constEnd(); ++it )
286+
QString str = feature.attribute( mDirectionFieldId ).toString();
287+
if ( str == mBothDirectionValue )
288288
{
289-
if ( it.key() != mDirectionFieldId )
290-
{
291-
continue;
292-
}
293-
QString str = it.value().toString();
294-
if ( str == mBothDirectionValue )
295-
{
296-
directionType = 3;
297-
}
298-
else if ( str == mDirectDirectionValue )
299-
{
300-
directionType = 1;
301-
}
302-
else if ( str == mReverseDirectionValue )
303-
{
304-
directionType = 2;
305-
}
289+
directionType = 3;
290+
}
291+
else if ( str == mDirectDirectionValue )
292+
{
293+
directionType = 1;
294+
}
295+
else if ( str == mReverseDirectionValue )
296+
{
297+
directionType = 2;
306298
}
307299

308300
// begin features segments and add arc to the Graph;

0 commit comments

Comments
 (0)