Skip to content

Commit 8a68a41

Browse files
committed
Simplify overly complex code
1 parent ab05fe7 commit 8a68a41

File tree

7 files changed

+29
-67
lines changed

7 files changed

+29
-67
lines changed

python/analysis/network/qgsnetworkspeedstrategy.sip

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,8 @@ class QgsNetworkSpeedStrategy : QgsNetworkStrategy
2828

2929
virtual QVariant cost( double distance, const QgsFeature &f ) const;
3030

31-
%Docstring
32-
Returns edge cost
33-
:rtype: QVariant
34-
%End
35-
36-
virtual QgsAttributeList requiredAttributes() const;
31+
virtual QSet< int > requiredAttributes() const;
3732

38-
%Docstring
39-
Returns list of the source layer attributes needed for cost calculation.
40-
This method called by QgsGraphDirector.
41-
:rtype: QgsAttributeList
42-
%End
4333

4434
};
4535

python/analysis/network/qgsnetworkstrategy.sip

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,11 @@ class QgsNetworkStrategy
4545

4646
virtual ~QgsNetworkStrategy();
4747

48-
virtual QgsAttributeList requiredAttributes() const;
48+
virtual QSet< int > requiredAttributes() const;
4949
%Docstring
50-
Returns list of the source layer attributes needed for cost calculation.
51-
This method called by QgsGraphDirector.
52-
:return: list of required attributes
53-
:rtype: QgsAttributeList
50+
Returns a list of the source layer attributes needed for cost calculation.
51+
This is method called by QgsGraphDirector.
52+
:rtype: set of int
5453
%End
5554

5655
virtual QVariant cost( double distance, const QgsFeature &f ) const = 0;

src/analysis/network/qgsnetworkspeedstrategy.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ QVariant QgsNetworkSpeedStrategy::cost( double distance, const QgsFeature &f ) c
3636
return QVariant( val );
3737
}
3838

39-
QgsAttributeList QgsNetworkSpeedStrategy::requiredAttributes() const
39+
QSet<int> QgsNetworkSpeedStrategy::requiredAttributes() const
4040
{
41-
QgsAttributeList l;
42-
l.push_back( mAttributeId );
41+
QSet< int > l;
42+
l.insert( mAttributeId );
4343
return l;
4444
}

src/analysis/network/qgsnetworkspeedstrategy.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,8 @@ class ANALYSIS_EXPORT QgsNetworkSpeedStrategy : public QgsNetworkStrategy
3535
*/
3636
QgsNetworkSpeedStrategy( int attributeId, double defaultValue, double toMetricFactor );
3737

38-
//! Returns edge cost
3938
QVariant cost( double distance, const QgsFeature &f ) const override;
40-
41-
/**
42-
* Returns list of the source layer attributes needed for cost calculation.
43-
* This method called by QgsGraphDirector.
44-
*/
45-
QgsAttributeList requiredAttributes() const override;
39+
QSet< int > requiredAttributes() const override;
4640

4741
private:
4842
int mAttributeId;

src/analysis/network/qgsnetworkstrategy.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,10 @@ class ANALYSIS_EXPORT QgsNetworkStrategy
6363
virtual ~QgsNetworkStrategy() = default;
6464

6565
/**
66-
* Returns list of the source layer attributes needed for cost calculation.
67-
* This method called by QgsGraphDirector.
68-
* \returns list of required attributes
66+
* Returns a list of the source layer attributes needed for cost calculation.
67+
* This is method called by QgsGraphDirector.
6968
*/
70-
virtual QgsAttributeList requiredAttributes() const { return QgsAttributeList(); }
69+
virtual QSet< int > requiredAttributes() const { return QSet< int >(); }
7170

7271
/**
7372
* Returns edge cost

src/analysis/network/qgsvectorlayerdirector.cpp

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,20 @@ QString QgsVectorLayerDirector::name() const
124124
return QStringLiteral( "Vector line" );
125125
}
126126

127+
QgsAttributeList QgsVectorLayerDirector::requiredAttributes() const
128+
{
129+
QSet< int > attrs;
130+
131+
if ( mDirectionFieldId != -1 )
132+
attrs.insert( mDirectionFieldId );
133+
134+
for ( const QgsNetworkStrategy *strategy : mStrategies )
135+
{
136+
attrs.unite( strategy->requiredAttributes() );
137+
}
138+
return attrs.toList();
139+
}
140+
127141
void QgsVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, const QVector< QgsPointXY > &additionalPoints,
128142
QVector< QgsPointXY > &snappedPoints, QgsFeedback *feedback ) const
129143
{
@@ -155,7 +169,6 @@ void QgsVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, const
155169
QgsFeatureIterator fit = mSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() ) );
156170

157171
// begin: tie points to the graph
158-
QgsAttributeList la;
159172
QgsFeature feature;
160173
while ( fit.nextFeature( feature ) )
161174
{
@@ -244,43 +257,8 @@ void QgsVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, const
244257

245258
std::sort( pointLengthMap.begin(), pointLengthMap.end(), TiePointInfoCompare );
246259

247-
{
248-
// fill attribute list 'la'
249-
QgsAttributeList tmpAttr;
250-
if ( mDirectionFieldId != -1 )
251-
{
252-
tmpAttr.push_back( mDirectionFieldId );
253-
}
254-
255-
QList< QgsNetworkStrategy * >::const_iterator it;
256-
QgsAttributeList::const_iterator it2;
257-
258-
for ( it = mStrategies.constBegin(); it != mStrategies.constEnd(); ++it )
259-
{
260-
QgsAttributeList tmp = ( *it )->requiredAttributes();
261-
for ( it2 = tmp.constBegin(); it2 != tmp.constEnd(); ++it2 )
262-
{
263-
tmpAttr.push_back( *it2 );
264-
}
265-
}
266-
std::sort( tmpAttr.begin(), tmpAttr.end() );
267-
268-
int lastAttrId = -1;
269-
for ( it2 = tmpAttr.constBegin(); it2 != tmpAttr.constEnd(); ++it2 )
270-
{
271-
if ( *it2 == lastAttrId )
272-
{
273-
continue;
274-
}
275-
276-
la.push_back( *it2 );
277-
278-
lastAttrId = *it2;
279-
}
280-
} // end fill attribute list 'la'
281-
282260
// begin graph construction
283-
fit = mSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( la ) );
261+
fit = mSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( requiredAttributes() ) );
284262
while ( fit.nextFeature( feature ) )
285263
{
286264
if ( feedback && feedback->isCanceled() )

src/analysis/network/qgsvectorlayerdirector.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ class ANALYSIS_EXPORT QgsVectorLayerDirector : public QgsGraphDirector
8787
QString mReverseDirectionValue;
8888
QString mBothDirectionValue;
8989
Direction mDefaultDirection;
90+
91+
QgsAttributeList requiredAttributes() const;
9092
};
9193

9294
#endif // QGSVECTORLAYERDIRECTOR_H

0 commit comments

Comments
 (0)