Skip to content
Permalink
Browse files

Simplify overly complex code

  • Loading branch information
nyalldawson committed Oct 28, 2017
1 parent ab05fe7 commit 8a68a410bf275d832606275e6ab461a343b2a662
@@ -28,18 +28,8 @@ class QgsNetworkSpeedStrategy : QgsNetworkStrategy

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

%Docstring
Returns edge cost
:rtype: QVariant
%End

virtual QgsAttributeList requiredAttributes() const;
virtual QSet< int > requiredAttributes() const;

%Docstring
Returns list of the source layer attributes needed for cost calculation.
This method called by QgsGraphDirector.
:rtype: QgsAttributeList
%End

};

@@ -45,12 +45,11 @@ class QgsNetworkStrategy

virtual ~QgsNetworkStrategy();

virtual QgsAttributeList requiredAttributes() const;
virtual QSet< int > requiredAttributes() const;
%Docstring
Returns list of the source layer attributes needed for cost calculation.
This method called by QgsGraphDirector.
:return: list of required attributes
:rtype: QgsAttributeList
Returns a list of the source layer attributes needed for cost calculation.
This is method called by QgsGraphDirector.
:rtype: set of int
%End

virtual QVariant cost( double distance, const QgsFeature &f ) const = 0;
@@ -36,9 +36,9 @@ QVariant QgsNetworkSpeedStrategy::cost( double distance, const QgsFeature &f ) c
return QVariant( val );
}

QgsAttributeList QgsNetworkSpeedStrategy::requiredAttributes() const
QSet<int> QgsNetworkSpeedStrategy::requiredAttributes() const
{
QgsAttributeList l;
l.push_back( mAttributeId );
QSet< int > l;
l.insert( mAttributeId );
return l;
}
@@ -35,14 +35,8 @@ class ANALYSIS_EXPORT QgsNetworkSpeedStrategy : public QgsNetworkStrategy
*/
QgsNetworkSpeedStrategy( int attributeId, double defaultValue, double toMetricFactor );

//! Returns edge cost
QVariant cost( double distance, const QgsFeature &f ) const override;

/**
* Returns list of the source layer attributes needed for cost calculation.
* This method called by QgsGraphDirector.
*/
QgsAttributeList requiredAttributes() const override;
QSet< int > requiredAttributes() const override;

private:
int mAttributeId;
@@ -63,11 +63,10 @@ class ANALYSIS_EXPORT QgsNetworkStrategy
virtual ~QgsNetworkStrategy() = default;

/**
* Returns list of the source layer attributes needed for cost calculation.
* This method called by QgsGraphDirector.
* \returns list of required attributes
* Returns a list of the source layer attributes needed for cost calculation.
* This is method called by QgsGraphDirector.
*/
virtual QgsAttributeList requiredAttributes() const { return QgsAttributeList(); }
virtual QSet< int > requiredAttributes() const { return QSet< int >(); }

/**
* Returns edge cost
@@ -124,6 +124,20 @@ QString QgsVectorLayerDirector::name() const
return QStringLiteral( "Vector line" );
}

QgsAttributeList QgsVectorLayerDirector::requiredAttributes() const
{
QSet< int > attrs;

if ( mDirectionFieldId != -1 )
attrs.insert( mDirectionFieldId );

for ( const QgsNetworkStrategy *strategy : mStrategies )
{
attrs.unite( strategy->requiredAttributes() );
}
return attrs.toList();
}

void QgsVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, const QVector< QgsPointXY > &additionalPoints,
QVector< QgsPointXY > &snappedPoints, QgsFeedback *feedback ) const
{
@@ -155,7 +169,6 @@ void QgsVectorLayerDirector::makeGraph( QgsGraphBuilderInterface *builder, const
QgsFeatureIterator fit = mSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( QgsAttributeList() ) );

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

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

{
// fill attribute list 'la'
QgsAttributeList tmpAttr;
if ( mDirectionFieldId != -1 )
{
tmpAttr.push_back( mDirectionFieldId );
}

QList< QgsNetworkStrategy * >::const_iterator it;
QgsAttributeList::const_iterator it2;

for ( it = mStrategies.constBegin(); it != mStrategies.constEnd(); ++it )
{
QgsAttributeList tmp = ( *it )->requiredAttributes();
for ( it2 = tmp.constBegin(); it2 != tmp.constEnd(); ++it2 )
{
tmpAttr.push_back( *it2 );
}
}
std::sort( tmpAttr.begin(), tmpAttr.end() );

int lastAttrId = -1;
for ( it2 = tmpAttr.constBegin(); it2 != tmpAttr.constEnd(); ++it2 )
{
if ( *it2 == lastAttrId )
{
continue;
}

la.push_back( *it2 );

lastAttrId = *it2;
}
} // end fill attribute list 'la'

// begin graph construction
fit = mSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( la ) );
fit = mSource->getFeatures( QgsFeatureRequest().setSubsetOfAttributes( requiredAttributes() ) );
while ( fit.nextFeature( feature ) )
{
if ( feedback && feedback->isCanceled() )
@@ -87,6 +87,8 @@ class ANALYSIS_EXPORT QgsVectorLayerDirector : public QgsGraphDirector
QString mReverseDirectionValue;
QString mBothDirectionValue;
Direction mDefaultDirection;

QgsAttributeList requiredAttributes() const;
};

#endif // QGSVECTORLAYERDIRECTOR_H

0 comments on commit 8a68a41

Please sign in to comment.
You can’t perform that action at this time.